[Tex/LaTex] How to concatenate strings into a single command

macrosstrings

I'd like to concatenate several parts of math text into a single command, and i read a great solution here on this site, allowing me to create this working example of what i mean :

\documentclass[a4paper,10pt]{report}
\usepackage[francais]{babel}

\def\reptemp{I love rain}
\global\edef\reptemp{\reptemp, but not too much.}

\begin{document}
\reptemp
\end{document} 

Unfortunately for some reason it refuses to work with more complex things in it, for example

\documentclass[a4paper,10pt]{report}
\usepackage[francais]{babel}

\def\reptemp{I {\it love} rain}
\global\edef\reptemp{\reptemp, but \bf{not} too much.}

\begin{document}
\reptemp
\end{document} 

produce the following error message

! Missing control sequence inserted.
<inserted text> 
                \inaccessible 
l.16 \reptemp
?

Of course i'd like to concatenate complex things by this method (environment, enumerate, etc) and i wonder if its possible.

Best Answer

What you want to achieve can be done in several ways. First of all let me start with a small hint: The commands \bf and it are obsolete and they don't have an argument. Please see this answer: Despite using backslash dollar sign, error persists


First of all you have to decide whether to save the contents in a token or a command. The differences and some explanation to your question are well explained by the great answer of Joseph Wright. Is \toks necessary to define \g@addto@macro? Can token registers be avoided in generel?


Using a command:

Approach 1: using \g@addto@macro:

\g@addto@macro is defined in the LaTeX Kernel. Based on the protected sign @ you have to use \makeatletter ... \makeatother, see: What do \makeatletter and \makeatother do?

\documentclass[a4paper,10pt]{report}
\makeatletter
\def\reptemp{I \textit{love} rain}
\g@addto@macro\reptemp{, but \textbf{not} too much.}
\makeatother
\begin{document}
\reptemp
\end{document} 

Approach 2: using package etoolbox:

The package etoolbox defines some helper macros to add new information to a given command. The material can be added before and after the command. Egreg provides a more robust package with the same functionality. The package is called xpatch. He also provides a more powerful package with is declared as experimental version, the package regexpatch.

Here an example with etoolbox:

\documentclass[a4paper,10pt]{report}
\usepackage{etoolbox}
\def\reptemp{I \textit{love} rain}
\appto\reptemp{, but \textbf{not} too much.}
\begin{document}
\reptemp
\end{document} 

The package etoolbox provides more useful functionality and can be loaded with xpatch too.


A special and new modern way is the usage of macros of the project LaTeX3 and the language expl3. Here you can work with token register as well as sequences. The range is really big. The usage of the new syntax needs some special handling which is explained in the short manual expl3. Of course you will find a lot of related questions here at tex.stackexchange.