3

I wonder how to get a space between two macros in an edef statement. My goal is to save a file name of the form FirstName LastName to a macro, and later use that macro in an includegraphics command.

The problem is that everything I tried returns an undesired result.

\edef\PictureName{\FirstName \LastName}    ➞ "FirstNameLastName"
\edef\PictureName{\FirstName{} \LastName}  ➞ "FirstName{} LastName"
\edef\PictureName{{\FirstName} \LastName}  ➞ "{FirstName} LastName"
\edef\PictureName{\FirstName\ \LastName}   ➞ "FirstName\ LastName"

So either no space is stored in \PictureName or additional parentheses or a backslash are stored. How can I do this correctly?

1
  • 1
    \edef\PictureName{\FirstName\space\LastName} Commented Nov 27, 2019 at 13:08

2 Answers 2

4

The issue you see is the usual “TeX ignores spaces after control sequences”, however here it can't be fixed as you would when producing typeset output. In that case, any method that separates the space character from the control sequence (and will produce a space, of course) in the input will work:

\FirstName{} \LastName
\FirstName{ }\LastName
{\FirstName} \LastName
\FirstName\ \LastName
\FirstName\space\LastName
\@firstofone{\FirstName} \LastName % assuming the usual definition of \@firstofone

however here you are in an expansion-only context, and in this case you need something a space token or something that expands to a space token, while separating it from the control sequence. The methods with braces and with control space don't work here because braces don't expand, so they are left untouched by \edef's expansion. They do work when typesetting because in that context they (form a group and) disappear. The control space \ also doesn't expand, so \edef leaves it alone.

Both Plain and LaTeX define \def\space{ }, so you can do:

\edef\PictureName{\FirstName\space\LastName}

which, in the expansion-only context of \edef, will expand \FirstName (without even looking at \space just yet), and then it will expand \space to a space token, and then expand \LastName.

The \@firstofone method will also work because \@firstofone also works by (one) expansion:

\edef\PictureName{\@firstofone{\FirstName} \LastName}

this will expand \@firstofone{\FirstName}, which becomes \FirstName, which then expands as well. Then the space token is seen, and then \LastName is expanded. See this related question: Conversion of space characters into space tokens

1

For the sake of having fun
let's have two assignments, not just one.
You can play the game
by "recycling" \PictureName:

E.g.:

\def\PictureName{ \LastName}%
\edef\PictureName{\FirstName\PictureName}%

Or:

\def\PictureName{ }%
\edef\PictureName{\FirstName\PictureName\LastName}%

Or:

\def\PictureName#1{%
  \edef\PictureName{\FirstName#1\LastName}%
}%
\PictureName{ }%

Or:

\def\PictureName#1{#1}%
\edef\PictureName{\PictureName{\FirstName} \LastName}%

Or:

\def\PictureName#1{#1}%
\edef\PictureName{\FirstName\PictureName{ \LastName}}%

Or:

\def\PictureName#1{#1}%
\edef\PictureName{\FirstName\PictureName{} \LastName}%

Or:

\def\PictureName#1{#1}%
\edef\PictureName{\FirstName\PictureName{ }\LastName}%

Or:

\def\PictureName!{}%
\edef\PictureName{\FirstName\PictureName! \LastName}%

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .