3

This code works:

\documentclass{article}
\usepackage{expl3}
\usepackage{color}
\setlength\parindent{0pt}
\begin{document}
\ExplSyntaxOn

\tl_new:N \_text
\tl_set:Nn \text {1}
\tl_set:Nx \text {1{\text}}

\text
\ExplSyntaxOff
\end{document}

prints:

11

But when I try to use another macro inside \tl_set:Nx:

\documentclass{article}
\usepackage{expl3}
\usepackage{color}
\setlength\parindent{0pt}
\begin{document}
\ExplSyntaxOn

\tl_new:N \_text
\tl_set:Nn \text {1}
\tl_set:Nx \text {1{\text}}

%this line breaks the compiler
\tl_set:Nx \text {\textcolor{red}{\text}}

\text
\ExplSyntaxOff
\end{document}

Instead of printing '11' in red it gives this error:

! TeX capacity exceeded, sorry [input stack size=5000].
\@@mpstopdf@@unprotect ... \@@mpstopdf@@unprotect 
                                                   \unprotect \let \@@mpstopd... l.34     \def\unprotect

What's wrong?

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
user4035
  • 5,035
  • 6
  • 40
  • 57

1 Answers1

7

You are trying to x-type expand (\edef) a LaTeX2e 'fragile' command (see What is the difference between Fragile and Robust commands?). You should never use \edef on such a command. Instead, you have to use LaTeX2e's \protected@edef:

\protected@edef\text{\textcolor{red}{\text}}

The LaTeX2e kernel does not require e-TeX, so none of the core commands are e-TeX \protected from expansion. In LaTeX3 terms, \textcolor is not an expandable command.

Not directly relevant here, but I would not call a command \text as this is used by the AMS bundle (specifically amstext).

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • Do I need extra packages to run your code? It doesn't work. Prints: You can't use a prefix withthe character @'.` – user4035 Apr 16 '13 at 15:08
  • 1
    @user4035 As \protected@edef contains an @, you need to use \makeatletter. See http://tex.stackexchange.com/questions/8351/what-do-makeatletter-and-makeatother-do for more on why! – Joseph Wright Apr 16 '13 at 15:10