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.
Best Answer
This can also be achieved in LaTeX:
You can either use
\csname ... \endcsname
to construct a control sequence, or use\@nameuse{...}
. The latter, of course, requires to be placed with the appropriate\makeatletter
...\makeatother
scope. See What do\makeatletter
and\makeatother
do? For the former, see the reference question What exactly do\csname
and\endcsname
do?A reference from the TeX Book (Chapter 7 How TeX Reads What You Type, p 40):
The above all use the provided kernel functionality. However,
etoolbox
as a package provides similar constructions as wrappers for\@nameuse
(and\@namedef
). See section 3.1.1 Macro Definitions (p 5) of theetoolbox
documentation. In particular it provides\csuse{...}
.