# How to use a macro inside \tl_set:Nx

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?

-
By the way: Internal commands are getting two leading underscores: \tl_new:N \__text –  Marco Daniel Apr 16 '13 at 15:03

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).
Do I need extra packages to run your code? It doesn't work. Prints: You can't use a prefix with the character @'. –  user4035 Apr 16 '13 at 15:08
@user4035 As \protected@edef contains an @, you need to use \makeatletter`. See tex.stackexchange.com/questions/8351/… for more on why! –  Joseph Wright Apr 16 '13 at 15:10