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

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