2

The problem is that I have created a variable containing a text with some itemize environment. Now, for some reasons, I want to store the content of it in another variable. But it's not working (see the MWE)

MNWE

\documentclass{article}
\begin{document}
\def\coucou{\begin{itemize}
\item  aa
\item bb
\end{itemize}}
\coucou
\def\aa {zz}
\expandafter\xdef\csname persocmd\aa\endcsname{\coucou} %%Not working
\end{document}
4
  • Please don't use the minimal class. It's not really well designed
    – user31729
    Nov 25 '15 at 15:51
  • Ok I changed it to article
    – Guuk
    Nov 25 '15 at 15:52
  • You can't use \def in an \edef or \xdef
    – user31729
    Nov 25 '15 at 15:53
  • For example \let\anothervariable=\coucou
    – wipet
    Nov 25 '15 at 17:59
2

If you want to make a macro equivalent to another one, use \let (with \global if you want so):

\documentclass{article}
\begin{document}

\def\coucou{\begin{itemize}
  \item  aa
  \item bb
  \end{itemize}}

\coucou

\def\aa{zz}
\expandafter\let\csname persocmd\aa\endcsname\coucou

\persocmdzz
\end{document}

If you want a global definition, just add \global:

\global\expandafter\let\csname persocmd\aa\endcsname\coucou

(note that \global triggers expansion of the following token, so it will correctly apply to \let).

Note that the proposed

\expandafter\gdef\csname persocmd\aa\endcsname{\coucou}

will change the meaning also of \persocmdzz if the definition of \coucou changes.

For instance

\expandafter\gdef\csname persocmd\aa\endcsname{\coucou}
\def\coucou{foo}
\persocmdzz

would print “foo” and not the itemized list.

One might force expansion of \coucou with

\expandafter\gdef\csname persocmd\aa\expandafter\endcsname\expandafter{\coucou}

but it's evident that the \let method is much easier.

Instead

\expandafter\let\csname persocmd\aa\endcsname\coucou
\def\coucou{foo}
\persocmdzz

would print the itemized list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.