12

I start by defining a command to store the string "Hello":

\newcommand{\textstring}{Hello}

I would like to append the string " world" but unfortunately this code causes an error:

\renewcommand{\textstring}{\textstring world}
David Underhill
  • 15,896
  • 7
  • 53
  • 61
flaflamm
  • 145
  • 1
  • 1
  • 6

4 Answers4

12

You can accomplish this by using \expandafter. For example:

% redefine \textstring by appending " world" to it
\expandafter\def\expandafter\textstring\expandafter{\textstring { }world}

If you don't use \expandafter then you end up with a recursion problem. You can read more about it here.

David Underhill
  • 15,896
  • 7
  • 53
  • 61
  • This method works fine (even when the appended text contains commands). But now I had another problem with it: when I use \textstring within my text it shows "Hello world" but I have to use it in \endnotetext[\value{enumi}]{\textstring} which generate an empty endnote. – flaflamm Jun 27 '10 at 13:33
3

Used the input from this question to generate

\edef\history{ }
\newcommand{\historyAdd}[1]{\edef\history{\history{}#1 }}
\newcommand{\historyAddEcho}[1]{#1\historyAdd{#1}}

The history was: 
\historyAddEcho{Hi brian}
\historyAdd{you idiot}
\historyAddEcho{how are you?}

\lipsum[3]

The real history was: \history

(sorry brian, but this was the most illustrative example I could think of)

The scructure can help you create a simple todo list with something like:

\lipsum[1]

\historyAdd{\\work more with: }
\section{\historyAddEcho{Introduction}}

\lipsum[1]

\historyAdd{\\work more with the text on page \thepage}
\lipsum[1]

\section{ToDo:}
\history

Hope this can help someone out there trying to concat strings for this purpose.

Neon
  • 293
  • 1
  • 2
  • 8
3

Similar to David Underhill's answer is the following

\newcommand{\textstring}{Hello}
\makeatletter
\g@addto@macro\textstring{ world}
\makeatother

The g@addto@macro macro achieves the same effect, and may produce slightly more readable code (especially if your code is in a package/style, or if you're already in a \makeatletter & \makeatother situation)

drfrogsplat
  • 2,577
  • 3
  • 19
  • 28
2

The problem is that this overwrites the definition of \textstring, rather than referencing the old one. In order to append, the standard way is to use the TeX command \edef, which expands the definition before assigning something. Thus, if you have

\def\textstring{Hello} % Or with \newcommand
\edef\textstring{\textstring{} world}

LaTeX will change the right-hand side of the \edef into Hello world, and then reassign that to \textstring, which is what you want. Instead, in your current version, the \newcommand doesn't expand the right-hand side, so when you use \textstring, it expands to \textstring world, which itself expands to \textstring world world, which itself expands to… you get the idea.

Antal Spector-Zabusky
  • 36,191
  • 7
  • 77
  • 140
  • This method seems to have problems when the text to add contains commands, it generated many errors. – flaflamm Jun 27 '10 at 13:33