[texhax] Use of \symbol{`\_} versus \symbol{'137} to produce \verb+_+ in moving arguments.

Heiko Oberdiek heiko.oberdiek at googlemail.com
Wed Feb 25 17:34:46 CET 2015


Hello,

On 25.02.2015 15:33, Ulrike Fischer wrote:
> Am Mon, 23 Feb 2015 15:02:51 -0500 schrieb Beuthe, Thomas:
>  
>> This one does not work:
>> \chapter{\texttt{A\symbol{`\_}B}}
>  
>> and this one works:
>> \chapter{\texttt{A\symbol{'137}B}}
>  
>> So I seem to have solved my problem, but I would like
>> to know why. 
> 
> Because \_ is a robust command but \symbol is not

"\symbol" *is* robust, it just expands to "\char#1\relax"
Both "\char" and "\relax" are unexpandable commands and does
not break in expansion contexts.

> and when wandering
> to the header you get something like "\char`\protect\_\relax" which
> obviously can explode if \protect is something the primitive \char
> doesn't expect at this place.

Yes, this is the problem, the argument, which uses the vanilla TeX
notation with backtick to get a number together with LaTeX's
protection mechanism. In this case "\_" should not change at all
to avoid breaking the "`" notation.
As workaround, "\_" could be redefined
in e-TeX via "\protected\def".

> Use \protect\symbol{`\_} to avoid such problems.

This will not help, because the argument, not "\symbol"
breaks.

But the argument can be given in a more robust way:

    \symbol{\number`\_}

Then the argument would be expanded to a robust
plain number:

    \char95\relax

> Or switch to
> T1-encoding to get rid of the whole problem:
> 
> \documentclass[12pt]{report}
> \usepackage{fancyhdr}
> \pagestyle{fancy}
> \usepackage[T1]{fontenc}
> \begin{document}
> \tableofcontents
> \chapter{\texttt{A\_B A\textunderscore B}}
> \verb+ A_B+
> abc a\newpage abc 
> \end{document}

The trick is here the robustness of "\textunderscore", defined
by LaTeX's NFSS2e commands. It also works for OT1, but
T1 is the better encoding here, because it contains the
underscore as glyph and is not constructed as poor man's
solution by a rule as in the OT1 encoding.

Yours sincerely
  Heiko Oberdiek


More information about the texhax mailing list