Update
Thanks to answer below by Carl Woll, I've put my notebook where it contains all the settings and code needed to use for better Latex formatting if others want to use it. This will save you the time having to type all this.
To use, simply download and open the notebook and do evaluate->Notebook the very first thing after you start Mathematica.
Now in any other notebooks when you do TeXForm[]
it will use the new formatting which will make the Latex generated better looking for the special functions and for the inverse trig functions.
I've added many special functions, but there are still more to add, feel free to add more yourself if you need others not listed.
Here is the notebook
I have been using the great answer by @CarlWoll in Is it possible to change/customize some conversions done by TeXForm? for sometime. Now in V 13.1 I found some problems. (I am not quite sure if this problem was there all the time, and I just never had an example ode which showed the problem, or it is due to upgrading to version 13.1).
But now, using the same code to convert some solutions to Latex, the Latex generated from TeXForm
do not compile using TeXLive
.
I will give below all the code from the answer above in one place to reproduce the problem, and the Latex error it generates.
I hope there is a solution to this, as without this, the Latex generated directly is not as good. I use Carl's fix to change special functions names to make them easy to read in the latex.
Here is MWE. This below is the same exact code by Carl from the above answer.
Initial /: Verbatim[TagSetDelayed][Initial[sym_], lhs_, rhs_] :=
With[{new = Block[{sym}, TagSetDelayed[sym, lhs, rhs];
First@Language`ExtendedDefinition[sym, "ExcludedContexts" -> {}]],
protect = Unprotect[sym]}, sym;
Replace[new, Rule[values_, n : Except[{}]] :> (values[sym] = Prepend[values[sym], n]), {2}];
Protect@protect;]
System`Convert`TeXFormDump`maketex[s_String] /; ! StringMatchQ[s, "\"" ~~ ___ ~~ "\""] &&
SyntaxQ[s, TeXForm] := Replace[s, {n_ /; StringMatchQ[n, NumberString] :> n,
w_?wordQ :> "\\operatorname{" <> w <> "}"}]
wordQ[s_String] := Length@StringSplit[s, WordBoundary] == 1
Initial[Convert`TeX`ExpressionToTeX] /:
Convert`TeX`ExpressionToTeX[e__] /; ! TrueQ@$TeX :=
Block[{$TeX = True}, Convert`TeX`ExpressionToTeX[e]]
Initial[EllipticF]/:MakeBoxes[EllipticF[a_,b_],TraditionalForm]/;$TeX:=MakeBoxes[Defer[EllipticF][a,b],TraditionalForm];
With the above evaluated, the following shows the problem
sol = DSolve[(a/2 - 6*y[x]^2)*y'[x]^2 + (-b - a*y[x] + 4*y[x]^3)* y''[x] == 0, y[x], x]
TeXForm[sol]
\operatorname{Solve}\left[\frac{2 \sqrt{\frac{y(x)-\operatorname{Root}\left[4 #1^3-#1 a-b\&,1\right]}{\operatorname{Root}\left[4 #1^3-#1 a-b\&,3\right]-\operatorname{Root}\left[4 #1^3-#1 a-b\&,1\right]}}
\sqrt{\frac{y(x)-\operatorname{Root}\left[4 #1^3-#1 a-b\&,2\right]}{\operatorname{Root}\left[4 #1^3-#1 a-b\&,3\right]-\operatorname{Root}\left[4 #1^3-#1 a-b\&,2\right]}} \left(y(x)-\operatorname{Root}\left[4 #1^3-#1
a-b\&,3\right]\right) \operatorname{EllipticF}\left(\sin ^{-1}\left(\sqrt{\frac{\operatorname{Root}\left[4 #1^3-#1 a-b\&,3\right]-y(x)}{\operatorname{Root}\left[4 #1^3-#1 a-b\&,3\right]-\operatorname{Root}\left[4
#1^3-#1 a-b\&,2\right]}}\right),\frac{\operatorname{Root}\left[4 #1^3-#1 a-b\&,2\right]-\operatorname{Root}\left[4 #1^3-#1 a-b\&,3\right]}{\operatorname{Root}\left[4 #1^3-#1 a-b\&,1\right]-\operatorname{Root}\left[4
#1^3-#1 a-b\&,3\right]}\right)}{c_1 \sqrt{2 a y(x)+2 b-8 y(x)^3} \sqrt{\frac{y(x)-\operatorname{Root}\left[4 #1^3-#1 a-b\&,3\right]}{\operatorname{Root}\left[4 #1^3-#1 a-b\&,2\right]-\operatorname{Root}\left[4 #1^3-#1
a-b\&,3\right]}}}=x+c_2,y(x)\right]
After copying the above to my latex editor
\documentclass[12pt]{article}
\usepackage{amsmath}
\begin{document}
\[
\operatorname{Solve}\left[\frac{2 \sqrt{\frac{y(x)-\operatorname{Root}\left[4 #1^3-#1 a-b\&,1\right]}{\operatorname{Root}\left[4 #1^3-#1 a-b\&,3\right]-\operatorname{Root}\left[4 #1^3-#1 a-b\&,1\right]}}
\sqrt{\frac{y(x)-\operatorname{Root}\left[4 #1^3-#1 a-b\&,2\right]}{\operatorname{Root}\left[4 #1^3-#1 a-b\&,3\right]-\operatorname{Root}\left[4 #1^3-#1 a-b\&,2\right]}} \left(y(x)-\operatorname{Root}\left[4 #1^3-#1
a-b\&,3\right]\right) \operatorname{EllipticF}\left(\sin ^{-1}\left(\sqrt{\frac{\operatorname{Root}\left[4 #1^3-#1 a-b\&,3\right]-y(x)}{\operatorname{Root}\left[4 #1^3-#1 a-b\&,3\right]-\operatorname{Root}\left[4
#1^3-#1 a-b\&,2\right]}}\right),\frac{\operatorname{Root}\left[4 #1^3-#1 a-b\&,2\right]-\operatorname{Root}\left[4 #1^3-#1 a-b\&,3\right]}{\operatorname{Root}\left[4 #1^3-#1 a-b\&,1\right]-\operatorname{Root}\left[4
#1^3-#1 a-b\&,3\right]}\right)}{c_1 \sqrt{2 a y(x)+2 b-8 y(x)^3} \sqrt{\frac{y(x)-\operatorname{Root}\left[4 #1^3-#1 a-b\&,3\right]}{\operatorname{Root}\left[4 #1^3-#1 a-b\&,2\right]-\operatorname{Root}\left[4 #1^3-#1
a-b\&,3\right]}}}=x+c_2,y(x)\right]
\]
\end{document}
and compiling it, gives the error
>lualatex foo.tex
This is LuaHBTeX, Version 1.15.1 (TeX Live 2023/dev)
restricted system commands enabled.
(./foo.tex
LaTeX2e <2022-06-01> patch level 5
L3 programming layer <2022-07-04>
(/usr/local/texlive/2022/texmf-dist/tex/latex/base/article.cls
Document Class: article 2021/10/04 v1.4n Standard LaTeX document class
(/usr/local/texlive/2022/texmf-dist/tex/latex/base/size12.clo))
(/usr/local/texlive/2022/texmf-dist/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(/usr/local/texlive/2022/texmf-dist/tex/latex/amsmath/amstext.sty
(/usr/local/texlive/2022/texmf-dist/tex/latex/amsmath/amsgen.sty))
(/usr/local/texlive/2022/texmf-dist/tex/latex/amsmath/amsbsy.sty)
(/usr/local/texlive/2022/texmf-dist/tex/latex/amsmath/amsopn.sty))
(/usr/local/texlive/2022/texmf-dist/tex/latex/l3backend/l3backend-luatex.def)
(./foo.aux) (/usr/local/texlive/2022/texmf-dist/tex/latex/base/ts1cmr.fd)
! You can't use `macro parameter character #' in math mode.
<argument> y(x)-\operatorname {Root}\left [4 ##
1^3-##1 a-b\&,1\right ]
l.18 a-b\&,3\right]}}}
=x+c_2,y(x)\right]
?
The problem is in #
being used in math mode. Compare the above latex with the one that would have been generated before, (ie without using the above modification)
sol=DSolve[(a/2-6*y[x]^2)*y'[x]^2+(-b-a*y[x]+4*y[x]^3)*y''[x]==0,y[x],x];
TeXForm[sol]
gives
\text{Solve}\left[\frac{2 \sqrt{\frac{y(x)-\text{Root}\left[4 \text{$\#$1}^3-\text{$\#$1} a-b\&,1\right]}{\text{Root}\left[4 \text{$\#$1}^3-\text{$\#$1} a-b\&,3\right]-\text{Root}\left[4 \text{$\#$1}^3-\text{$\#$1}
a-b\&,1\right]}} \sqrt{\frac{y(x)-\text{Root}\left[4 \text{$\#$1}^3-\text{$\#$1} a-b\&,2\right]}{\text{Root}\left[4 \text{$\#$1}^3-\text{$\#$1} a-b\&,3\right]-\text{Root}\left[4 \text{$\#$1}^3-\text{$\#$1}
a-b\&,2\right]}} \left(y(x)-\text{Root}\left[4 \text{$\#$1}^3-\text{$\#$1} a-b\&,3\right]\right) F\left(\sin ^{-1}\left(\sqrt{\frac{\text{Root}\left[4 \text{$\#$1}^3-a
\text{$\#$1}-b\&,3\right]-y(x)}{\text{Root}\left[4 \text{$\#$1}^3-a \text{$\#$1}-b\&,3\right]-\text{Root}\left[4 \text{$\#$1}^3-a \text{$\#$1}-b\&,2\right]}}\right)|\frac{\text{Root}\left[4 \text{$\#$1}^3-a
\text{$\#$1}-b\&,2\right]-\text{Root}\left[4 \text{$\#$1}^3-a \text{$\#$1}-b\&,3\right]}{\text{Root}\left[4 \text{$\#$1}^3-a \text{$\#$1}-b\&,1\right]-\text{Root}\left[4 \text{$\#$1}^3-a
\text{$\#$1}-b\&,3\right]}\right)}{c_1 \sqrt{2 a y(x)+2 b-8 y(x)^3} \sqrt{\frac{y(x)-\text{Root}\left[4 \text{$\#$1}^3-\text{$\#$1} a-b\&,3\right]}{\text{Root}\left[4 \text{$\#$1}^3-\text{$\#$1}
a-b\&,2\right]-\text{Root}\left[4 \text{$\#$1}^3-\text{$\#$1} a-b\&,3\right]}}}=x+c_2,y(x)\right]
The above compiles OK. Notice all the #
are now inside \text{$\#$1}
and so do not give error when compiled.
Question is: How can use Carl fix without having this problem in the Latex generated?
V 13.1 on windows 10.