Has anyone figured out how to use a LaTeX package (like cancel) in a markdown cell?

I have been using jupyter notebook to keep homework from an applied physics class. I’m then downloading the documents to PDF by way of LaTeX. In the current rather messy algebra I’m entering I would really like to use the cancel package (see Strikethrough in LaTeX - Jan Söhlke). Of course I want it to work in the markdown cell and pass through nbconvert to tex and then pdf.

If I download the notebook to LaTeX and manually add \usepackage{cancel} to the document preamble and then use something like \cancel{(\cos^2{\psi} + \sin^2{\psi})} in the document, it works fine, I get a nice strikeout across the trig identity. But if I try using it in the markdown cell it isn’t understood.

I have several user defined commands that are built in a markdown cell at the top of the notebook. These look like this:

<div hidden>
$\gdef\adj#1{#1^{\dagger}}$
\vskip-\parskip
\vskip-\baselineskip
</div>

and they work fine. The \adj{A} command in a markdown cell correctly renders with a superscript dagger in both the markdown cell and the pdf file. I tried adding

<div hidden>
$\usepackage{cancel}$
\vskip-\parskip
\vskip-\baselineskip
</div>

but that didn’t work. The markdown cell displays \cancel in red. The \usepackage{cancel} command does get added to the tex file but it is in the document not the preamble.

Ad Astra,

Jack B. Lyle

1 Like

Per https://discourse.jupyter.org/t/nbconvert-6-0-release/5974 the templates for nbconvert are found in Jupyter’s data directory under nbconvert/templates.

In /home/jlyle/.local/share/jupyter/nbconvert/templates/latex is a base.tex.j2 file that contains recognizable LaTeX code. Adding a \usepackage{cancel} line in that file adds the command to the preamble, and the cancel displays nicely in the pdf. But it does not display in the notebook itself. How do I get it to display in the notebook? I suspect I have to configure Mathjax somehow. There is a note in /home/jlyle/.local/share/jupyter/nbextensions/load_tex_macros/readme.md that says “Simply put your latex macros in a file named latexdefs.tex, in the same directory as your notebook.”

I tried adding the latexdef.tex file containing:

\documentclass{article}
\usepackage{cancel}
\newcommand{\dog}{DOG!}
\begin{document}
\end{document}

I then turned on the LoadTexMacros switch in the notebook extension tab. The new \dog command works but \cancel does not.

1 Like

You actually managed to find the answer to one of the questions that I have. I too would love to use packages from CTAN.

Here’s my Stack Overflow question:

I opened a feature request issue Support for CTAN LaTeX packages · Issue #11701 · jupyterlab/jupyterlab · GitHub

1 Like

So the trick of adding hidden macros at the top of the document is useful, and works on someone else’s system. A how to document is probably in order. I’m just keeping a text file of the macros I frequently use and pasting it into the top cell of each new notebook. That works but isn’t exactly ideal. I would like to tell JN to load the macros when creating a new notebook without the manual copy and paste. That might be something like a template.

The trick does not work in JupyterLab. The typeset equations displayed in JL do not correctly display the macros.

In JN the trick exports correctly to LaTeX but not to HTML. I haven’t tried the other download options.

So it seems that adding things to latex preamble still does not work. If it does, this would be fantastic. Because it means you can almost write any LaTeX document from jupyter.