3

Looking for an answer to this question I got with pdflatex an error related to the expansion of \$ with expl3. For example :

\documentclass{article}

\ExplSyntaxOn
\NewDocumentCommand{\test}{ m }
  {
    \tl_set:Ne \l_tmpa_tl { #1 }
    \tl_use:N \l_tmpa_tl
  }
\ExplSyntaxOff

\begin{document}

\newcommand*{\mystring}{Special: Five (5) bananas for \$1.50.}

\test{\mystring}

\end{document}

raises

! TeX capacity exceeded, sorry [input stack size=10000].
\font@name ->
             \OT1/cmr/m/n/10 
l.15 \test{\mystring}
                            
!  ==> Fatal error occurred, no output PDF file produced!

The code compile without error with lualatex or xelatex, or if \$ is removed.

Why?

2
  • 1
    This is an illustrative example that life would be simpler if we used exclusively Unicode engines and did not support non-Unicode. Then this problem would not exist at all.
    – wipet
    Apr 15 at 5:24
  • 1
    @wipet for \$ yes but it's always possible to come up with some command that isn't safe in edef or expanded and you need to either not use edef or use a \protected definition for the content. The issue never completely goes. Apr 15 at 8:07

1 Answer 1

4

You are using the default OT1 encoding in which \$ is not safe in an expansion context such as an e argument as it has to set up new fonts to switch between £ and $ which share the same position in OT1.

Either don't use the e argument or define your \mystring to be \protected so that it does not expand in e arguments, or use an encoding in which \$ is safe in an expansion context.

So one of these choices:

\documentclass{article}

\ExplSyntaxOn
\NewDocumentCommand{\test}{ m }
  {
    \tl_set:Nn \l_tmpa_tl { #1 }
    \tl_use:N \l_tmpa_tl
  }
\ExplSyntaxOff

\begin{document}

\newcommand{\mystring}{Special: Five (5) bananas for \$1.50.}

\test{\mystring}

\end{document}

or

\documentclass{article}

\ExplSyntaxOn
\NewDocumentCommand{\test}{ m }
  {
    \tl_set:Ne \l_tmpa_tl { #1 }
    \tl_use:N \l_tmpa_tl
  }
\ExplSyntaxOff

\begin{document}

\NewDocumentCommand\mystring {} {Special: Five (5) bananas for \$1.50.}

\test{\mystring}

\end{document}

or

\documentclass{article}

\usepackage[T1]{fontenc}

\ExplSyntaxOn
\NewDocumentCommand{\test}{ m }
  {
    \tl_set:Ne \l_tmpa_tl { #1 }
    \tl_use:N \l_tmpa_tl
  }
\ExplSyntaxOff

\begin{document}

\newcommand{\mystring}{Special: Five (5) bananas for \$1.50.}

\test{\mystring}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .