First off: I do not understand \expandafter and \noexpand completely which is probably the main problem. I have found a question that explains what it does, but still I cannot seem to make it work.
I simply want to print the name of a command using \csname like this:
\def\str{String}
Command: \csname str\endcsname.\par
End command.
This prints the contents of \str (String), but I want to prevent the last expansion. I thought this would work:
Command: \expandafter\noexpand\csname str\endcsname.\par
This would first expand \csname to \str, and then \noexpand should prohibit the next expansion. But apparently, this gives a blank output, which is actually very strange.
So I want to expand \csname, but not \str.
EDIT: Few things to consider I did not know they were important (or just did not thought through):
- It's for writing to a file.
- It also has to work for non-
\csnamebased commands.
Thus the code changes to:
\immediate\write\file{Command: \expandafter\noexpand\csname str\endcsname}
Which works (as egreg said), but does not work with
\immediate\write\file{Command: \expandafter\noexpand\str}
This is needed because I need to make a command that shows the command. In total it should look like this:
\newcommand\writecmd[1]{
\immediate\write\file{\expandafter\noexpand#1}
}
\def\str{Blahblah}
\writecmd{\str}
\writecmd{\csname str\endcsname}
This outputs
Blahblah
\str
which is almost what I needed. If there is a workaround, that's fine too. Mind that the command \writecmd is in my program a little bit more extensive.
\expandafter\noexpandmethod would be good for writing in a file; the command name would be followed by a space, which is not the case for\expandafter\string. – egreg Jul 24 '12 at 09:40\csnameis there or just the command.. I'll update my question. – Didii Jul 24 '12 at 09:47