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

Beuthe, Thomas thomas.beuthe at cnl.ca
Mon Feb 23 21:02:51 CET 2015


UNRESTRICTED | ILLIMITÉ

 

Since \verb cannot be used in moving arguments like \chapter and \section

it becomes difficult to produce an exact analog of the underscore character

in a tt font in these situations.

 

A viable alternative is to use \symbol as an alternative.

 

A detailed discussion of the pros and cons of using this approach has already

taken place here:

 

http://compgroups.net/comp.text.tex/-verb+.+-different-from-texttt/1911038

 

This and other discussion led me to use the \texttt{A\symbol{`\_}B}}

type of solution to produce a "verb-like" texttt output for the underscore

in section headings, since the \verb environment itself cannot be used

in section headings.

 

This solution produces secondary difficulties however, as hinted to in

above referenced discussion.

 

For example, I wanted to include the section headings as part of a

footer, but the use of the \leftmark and \rightmark

causes the fancy head and footer package problems due to the

implicit protect in the \symbol, and doing the following

type of thing:

 

\fancyfoot[R]{\leftmark}

 

causes an error of the following type:

 

! Improper alphabetic constant.

<to be read again> 

                   \protect 

 

as a result.

 

Seems there's an implicit protect in there.

This was also hinted at in the above reference.

 

There are a number of alternatives that can be used:

 

  {\tt \string _}  % descend into plain TeX

  {\tt \char`\_} % descend into plain TeX

  {\tt \symbol{'137}} % official LaTeX, Lamport p 116

  {\tt \symbol{`\_}} % not official LaTeX, but it works!

 

Since last one messes up things for the \rightmark,

I tried the \symbol{'137} symbol alternative, and it seems

to work.

 

Here are two minimal working examples:

 

This one does not work:

 

\documentclass[12pt]{report}

\usepackage{fancyhdr}

\begin{document}

 

\pagestyle{fancy}

 

\chapter{\texttt{A\symbol{`\_}B}}

 

test

 

\newpage

 

test

 

\end{document}

 

 

and this one works:

 

\documentclass[12pt]{report}

\usepackage{fancyhdr}

\begin{document}

 

\pagestyle{fancy}

 

\chapter{\texttt{A\symbol{'137}B}}

 

test

 

\newpage

 

test

 

\end{document}

 

 

So I seem to have solved my problem, but I would like

to know why. Is the \symbol{'137} form simpler and

therefore works because it is expanded earlier?

 

If anyone could provide a short explanation,

I think it would be helpful both to me and

everyone else who might contemplate this type

of a solution in the future.

 

Thanks!

 

Thomas

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://tug.org/pipermail/texhax/attachments/20150223/525629e8/attachment.html>


More information about the texhax mailing list