Open ghost opened 5 years ago
I have also run into a similar problem. I can use $\LaTeX$ in my source for the HTML version of my Jupyter Book text, but it chokes with " You can't use `\spacefactor' in math mode" if I try to compile that to pdf using pdflatex because \LaTeX is not a math mode command. (Wrapping it in \mbox{} does not seem to work in HTML mode.)
If I could detect that it was MathJax, then I could use $\LaTeX$ and otherwise just use $\mbox{LaTeX}$ or similar.
You can certainly define this macro yourselves, as in
\newcommand{\ifmathjax}[2]{#1}
and you can include that in your MathJax configuration using
MathJax = {
tex: {
macros: {
ifmathjax: ['#1', 2]
}
}
}
so that it is always defined. Do the similar definition in your latex macros file, but using #2
instead. Wouldn't that do what you need?
Thanks for the suggestion. That is not a good solution from my perspective, though. I am writing in Jupyter Notebooks and deploying them to HTML and PDF (via LaTeX) using Jupyter Book. I don't see any good solution to easily make this work everywhere, across all my different files and deployments.
The simple solution seems to be if MathJax provides a \ifmathjax command.
I don't see any good solution to easily make this work everywhere, across all my different files and deployments.
It looks to me that jupyter book already provides a convenient way to add macro to both the HTML and LaTeX pipelines via the _config.py
file. It looks like you could use
sphynx:
config:
mathjax3_config:
TeX:
Macros:
"ifmathjax": ["#1", 2]
latex_elements:
preamble: |
\newcommand\ifmathjax[2]{#2}
to make it work in both your HTML and PDF output. (This example comes from the documentation, but that might have been copied from the older mathjax2_config
; you might need to use lower-case tex
and macros
for MathJax version 3, unless Jupyter is changing the case for you.)
As for the original notebook itself, it seems you can use the custom.js
file to define the macro. Depending on how and when MathJax v3 is loaded, you might be able to simply do
MathJax.tex2mml('\\newcommand{\\ifmathjax}[2]{#1}');
in custom.js
. It may be that you need something more sophisticated:
if (window.MathJax) {
if (MathJax.tex) {
if (!MathJax.tex.macros) MathJax.tex.macros = {};
MathJax.tex.macros.iflatex = ['#1', 2];
} else {
MathJax.tex2mml('\\newcommand{\\ifmathjax}[2]{#1}');
}
} else {
window.MathJax = {
tex: {
macros: {
ifmathjax: ['#1', 2]
}
}
}
}
I don't use Jupyter notebook myself, so haven't tested this.
The simple solution seems to be if MathJax provides a \ifmathjax command.
Note that that would only be available where MathJax is used, so you would still need to do the definition for the LaTeX workflow. Since you are already doing that, it seems to make sense to do the MathJax definition in your _config.py
as well.
Thank you -- this is very helpful!
Great! Let us know if this works for you, or if you had to make any changes to get it to work.
It is not possible to share symbol definition files between full LaTeX and programs using MathJax as a renderer (e.g. previews in VSCode LaTeX plugins). For example, MathJax chokes up at the following definition of the symbol for weak* convergence:
This is the case even if
\makeatother
,\makeatletter
, and use of@
are removed; in the first case all renderings are full of\makeatother
gibberish, in the second case only\weaktostar
renders full of gibberish.For rendering in MathJax it would be sufficient to
However, this is a suboptimal rendering for actual LaTeX output. So it would be good if there was a
\ifmathjax{…MathJax content…}{…LaTeX content…}
macro, and/or, considering also the use of KaTeX by other applications (which could implement the same mechanism), a more general\iffulllatex
macro .