Double subscript error from auto-generated latex

subscriptssyntax

This auto-generated latex from sagemath software generates error Double subscript

I wanted to report this to sagemath, but wanted to check first why this is showing up

Inside sagemath on Linux, I type

sage: var('_C0 _C1 _C2 _C3 _C4 _C5 _C6 _C7 _C8 _C9 a b c d e f g h k p q x y ');
sage: integrand=(_C3*x^2-_C4)*(3*_C3*x^2+3*_C4+x)/x/((_C3*x^2+_C0*x+_C4)/(_C3*x^2+_C1*x+_C4))^(1/2)/(_C3^2*x^4+2*_C3*_C4*x^2+_C4^2-x^2);

sage: latex(integrand)

\frac{{\left(3 \, _{C_{3}} x^{2} + 3 \, _{C_{4}} + x\right)} {\left(_{C_{3}} x^{2} - _{C_{4}}\right)}}{{\left(_{C_{3}}^{2} x^{4} + 2 \, _{C_{3}} _{C_{4}} x^{2} + _{C_{4}}^{2} - x^{2}\right)} x \sqrt{\frac{_{C_{3}} x^{2} + _{C_{0}} x + _{C_{4}}}{_{C_{3}} x^{2} + _{C_{1}} x + _{C_{4}}}}}

Then I copy the above latex and compile it

\documentclass[12pt]{article}
\usepackage{amsmath}
\begin{document}
\[
\frac{{\left(3 \, _{C_{3}} x^{2} + 3 \, _{C_{4}} + x\right)} {\left(_{C_{3}} x^{2} - _{C_{4}}\right)}}{{\left(_{C_{3}}^{2} x^{4} + 2 \, _{C_{3}} _{C_{4}} x^{2} + _{C_{4}}^{2} - x^{2}\right)} x \sqrt{\frac{_{C_{3}} x^{2} + _{C_{0}} x + _{C_{4}}}{_{C_{3}} x^{2} + _{C_{1}} x + _{C_{4}}}}}
\]

\end{document}

Using lualatex foo2.tex gives

(./foo2.aux) (/usr/local/texlive/2021/texmf-dist/tex/latex/base/ts1cmr.fd)
! Double subscript.
<argument> {\left (_{C_{3}}^{2} x^{4} + 2 \, _{C_{3}} _
                                            {C_{4}} x^{2} + _{C_{4}}^{2} - x^{2
l.10 ...}}{_{C_{3}} x^{2} + _{C_{1}} x + _{C_{4}}}}}

?

My question is: Do you think this is an error in sagemath latex generation, where it should have been more smart in handling the input? Since the input is valid code inside sagemath itself, and can be processed. It is only when generating the Latex representation I get this error.

How should the latex generated be to avoid this error?

reference

why-am-i-getting-a-double-subscript-error

Using This is LuaHBTeX, Version 1.13.2 (TeX Live 2021)

Update

As recommend by answer below, a feature ticket was added to sagemath to improve the latex it generated

Best Answer

Converting my comments to an answer:

The source of the problem is ...+ 2 \, _{C_{3}} _{C_{4}} which seems to originate from this section of your input: +2*_C3*_C4. Apparently, the presence of underscores in sagetex variable names is valid syntax. However, it is a poor choice, given the special meaning of _ in TeX.

If I go in and manually change the offending _{C_{4} to {_{C_{4}} (adding extra braces), it compiles but does not produce a reasonable looking output. All C variables are presented in subscript form, so that, at the beginning of the denominator, the _{C_{3}}^{2} typesets with the 2 above the C_{3} (properly set in LaTeX, but meaningless as math).

\documentclass{article}
\begin{document}
\[
\frac{{\left(3 \, _{C_{3}} x^{2} + 3 \, _{C_{4}} + x\right)} 
{\left(_{C_{3}} x^{2} - _{C_{4}}\right)}}{{\left(_{C_{3}}^{2} x^{4} + 
2 \, _{C_{3}} {_{C_{4}}} x^{2} + _{C_{4}}^{2} - x^{2}\right)} x 
\sqrt{\frac{_{C_{3}} x^{2} + _{C_{0}} x + _{C_{4}}}{_{C_{3}} x^{2} + 
_{C_{1}} x + _{C_{4}}}}}
\]
\end{document}

enter image description here

The OP indicates that the input was also autogenerated. My point, however, is that someone, somewhere, chose to use underscores as part of variable names. That was the big mistake .

If you were able to make sagetex convert all underscores in variable names to \_ when converting to LaTeX, the output would at least make sense at that point, though I still find the presence of the underscores unnatural. This would be more of a feature request rather than a bug report.

\documentclass{article}
\begin{document}
\[
\frac{{\left(3 \, \_{C_{3}} x^{2} + 3 \, \_{C_{4}} + x\right)} 
{\left(\_{C_{3}} x^{2} - \_{C_{4}}\right)}}{{\left(\_{C_{3}}^{2} x^{4} + 
2 \, \_{C_{3}} \_{C_{4}} x^{2} + \_{C_{4}}^{2} - x^{2}\right)} x 
\sqrt{\frac{\_{C_{3}} x^{2} + \_{C_{0}} x + \_{C_{4}}}{\_{C_{3}} x^{2} + 
\_{C_{1}} x + \_{C_{4}}}}}
\]
\end{document}

enter image description here

In the long run, however, I would advise refraining from the use of underscores in sagetex variable names.