LaTeXML icon indicating copy to clipboard operation
LaTeXML copied to clipboard

handle empty case in Tokens::stripBraces

Open dginev opened this issue 11 months ago • 2 comments

Fixes #2452

The classic loops in Tokens::stripBraces were missing an edge case where the braces held nothing - and an empty Tokens would be the appropriate response.

Interestingly, the bug reported in #2452 was absent from the Rust port, as I had used Rust's Iterator abstraction (with peekable), which safely avoids the need of index arithmetic.

dginev avatar Dec 24 '24 01:12 dginev

I also added the report from the issue as a new test here, to spot any regressions (e.g. during porting)

dginev avatar Jan 09 '25 14:01 dginev

Btw, there is a semi-related curiosity that I recently learned thanks to Norbert Preining, about there being a maximum number of strips in usepackage braces. If you look at the following example:

\documentclass{article}
\usepackage{{{{{{graphicx}}}}}} % works
\usepackage{{{{{{{graphicx}}}}}}} % fails

\begin{document}
oops.
\end{document}

pdflatex will successfully unwrap 6 braces, but will keep the seventh and try to load {graphicx}.sty in the second case. I consider it a feature that latexml does not emulate that accurately, and would succeed with both 6 and 7 braces. I also hope this is rarely used by real authors...

dginev avatar Jan 09 '25 20:01 dginev

This is scary stuff since, in real TeX/LaTeX, the brace stripping is almost always an artifact of exactly what sequence of helper macros with and without passed arguments being RE-braced (as {#1}). So, you generally just feel Lucky when these corner cases work out. That said, what you've done seems straightforward and correct; and if some error shows up, it's probably more about errors cancelling, than about introducing any. So...

brucemiller avatar Mar 13 '25 18:03 brucemiller