# double subscript error from auto-generated latex

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

• The source of the problem is ...+ 2 \, _{C_{3}} _{C_{4}} which seems to originate from this section of your input: +2*_C3*_C4. But I don't know sagemath. Does the underscore signify something special in the sagemath input? If that is your choice in variable naming, you have chosen poorly, given the special meaning of _ in TeX Commented Aug 25, 2021 at 16:42
• @StevenB.Segletes underscore in start of variable name is OK. I have no choice in this, as this input itself is also autogenerated integrand. But the point is _C works OK as symbol inside sagemath and can be used as any other variable. The problem comes when generating Latex. I agree that underscore in start of variable name is not recommended. But this is how the input is at this time. My question is really, if the Latex generated is faulty itself and if I should report this as sagemath error or if there is something else going on. Commented Aug 25, 2021 at 16:53
• 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). The real issue here is not sagetex, unless they FORCE you to use the underscore in variable names. You say it is autogenerated, but someone, somewhere, chose to use underscores as part of variable names. Big mistake. Commented Aug 25, 2021 at 17:03
• @StevenB.Segletes Ok, thanks. I already asked the originator of the input used in this example, to avoid _ in start of their variable names to avoid any such issue in first place and will not then report this to sagemath as bad Latex. Commented Aug 25, 2021 at 17:12
• The only possible request to sagetex would be that if a variable name contains an underscore, they convert it to \_ when converting it to LaTeX. That way it would not present as a subscript, but as a leading underscore. Not a bug report, per se, but more of a feature request. Commented Aug 25, 2021 at 17:32

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}


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}


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