jfbercher / jupyter_latex_envs

(Some) LaTeX environments for Jupyter
Other
112 stars 28 forks source link

Interactions between nbconvert, latexdefs, and "Download As" #13

Open defjaf opened 7 years ago

defjaf commented 7 years ago

Hi,

The specific problem I want to solve is that I really want to use the Cyrillic letter "Sha" -- Ш -- in a notebook and have it rendered in a latex version. This led me down several pathways.

There are various ways that the latex in the notebook gets rendered. First is the actual notebook version, which reads latexdefs.tex but (I think) doesn't use the actual latex engine for the rendering. This means, for example, that we can use unicode characters interspersed in latex and it appears in the notebook.

Then there's nbconvert --to latex_with_lenvs which also reads latexdefs.tex, but will balk at unicode in the latex source. It is possible to set up specific other unicode characters to use via DeclareMathSymbol and the like, but this needs code in the latex pre-amble (but latexdefs.tex is input after \begin{document}).

Finally, there's "File > Download As > PDF via latex" which ignores latexdefs entirely, and much of the rest of the innovations of jupyter_latex_envs, including, for example, \author{} and \maketitle which means that these don't get rendered correctly.

Is there any way to reconcile these? I think that there are -- at least -- two separate problems here: one is the fact that latexdefs.tex can't include preamble code, and the other is that the "Download As" pathway is completely separate from jupyter_latex_envs.

jfbercher commented 7 years ago

@defjaf Yes, I use myself a def\sha{ш} which works in the notebook but not in the pdf version. The nbextension do not use latex at all, and since the browser directly support unicode, we can do that (in the browser).
Indeed, overcoming that would need to add some code in the preamble. I could add the possibility for users to inject some code, using eg a file similar to latexdefs, eg preamb_code, with the auxilliary issue that this is specific to the latex engine and will not be interpreted in the browser.

Perhaps that the most direct and efficient solution for you is to edit the default template(or create a new one) to add the code you need in the preamble. For the jupyter nbconvert --to latex_with_lenvs conversion, the default template is thmsInNb_article.tplx which is located in the templates subdirectory of the latex_envs directory. The exact location depends on your system and the way you installed the extension. For me, it is here: ~/.local/lib/python3.5/site-packages/latex_envs/templates/thmsInNb_article.tplx In this file, you should add your code in the ((* block commands *)) section. If you want to add your own template, put it in the directory and call the conversion as jupyter nbconvert --to latex_with_lenvs --template newtemplate.tplx. Note that if you use a \def like the one above for \sha you would need to remove it before compiling in order to avoid conflict between the two definitions.

Yes, the download asoption corresponds to the standard latex support in the notebook. I may overwrite it when the nbextension is present. This is probably something to do (or at least document).