1

I am writing some API documentation, and since I don't like to repeat myself I would like to make some commands and call those for parts that get repeated a lot. However, I am having trouble with characters that need to be escaped in some commands, while they can't be escaped in labels.

I have the following command:

\newcommand{\CommandHeader}[1]
{
    \subsection{#1}
    \label{command:#1}

    \subsubsection*{Name}
    #1
}

This works file when I call the command without any special characters

\CommandHeader{ping}

But when I call it with escaped characters, it will result in errors:

\CommandHeader{get\_file} % Gives missing/extra \endcsname errors
\CommandHeader{get_file} % Gives errors everywhere else

So my question is, how do I create a command to which I can pass a name with escaped characters, that works with both types of commands. Also, why is there a difference between these commands anyway?

  • 1
    Welcome to TeX.SX! Do you need other special characters or just _? – egreg Jan 29 '16 at 13:23
  • I would simply define the macro to have two arguments, one for the name and one for the label. More advanced would be a setup with an optional argument which you can use when things have to be escaped. – Johannes_B Jan 29 '16 at 13:24
  • @egreg Just _ for now, but since it's API documentation I can imagine this issue coming up with other characters at some point. So I would prefer a generic solution. – Rick de Water Jan 29 '16 at 13:29
  • The solution in the answer to this question should be easily adaptable. – Benjamin Jan 29 '16 at 13:52
  • 1
    the usage in \label and \section is completely different, while it is possible to make things work (especially for _ which is easier than accented letters) it is probably a mistake to do so and the arguments should be kept separate. The argument of \section is a string to be typeset in the output, the argument to \label is essentially an internal command name used for cross referencing so you can not use \_ for the same reason you can not use \_ in the middle of a counter or environment name. Also unrelated but you are missing % from several lines in your definition. – David Carlisle Jan 29 '16 at 14:07
  • Is there any news here? – Johannes_B May 01 '16 at 17:21

0 Answers0