4

I try to expand a macro containing a string with another string. It is in the document and thus I want to avoid using @.

I tried the following using etoolbox

\expandafter\def\fileAcronym\democodefile
\appto\fileAcronym{-acronym}

It does not work. The \demcocodefile is not expanded (thus the \expandafter).

4

What you're expanding is \fileAcronym and not \democodefile, since \expandafter expands whatever comes immediately after the next token. You could use

\expandafter\def\expandafter\fileAcronym\expandafter{\democodefile}
\appto\fileAcronym{-acronym}

but it may be more concise (since you're only using strings) to rather use

\edef\fileAcronym{\democodefile-acronym}
5

Assuming that \democodefile expands to the string you want to append something to, creating \fileAcronym that expands to the augmented string, the easiest way is to say

\let\fileAcronym\democodefile
\appto\fileAcronym{-acronym}

Without \appto:

\expandafter\def\expandafter\fileAcronym\expandafter{\democodefile-acronym}

With less characters

\edef\fileAcronym{\unexpanded\expandafter{\democodefile}-acronym}

(\unexpanded\expandafter can be replaced by \expandonce, if you already load etoolbox).


An “abstract” version:

\newcommand{\augmentstring}[3]{%
    \edef#1{\unexpanded\expandafter{#2}\unexpanded{#3}}%
}

With that code in the preamble you can say, in the document,

\augmentstring{\fileAcronym}{\democode}{-acronym}

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.