How to use '_' without escaping it (with the underscore package)

2,392 views
Skip to first unread message

Gary F.

unread,
Jun 9, 2012, 4:39:35 PM6/9/12
to latexus...@googlegroups.com

I am working on a document that refers to many "C" variable names, and most of those names have one/more underscores (_'s) in them.  LaTeX wants to treat them as a subscript operator, unless they're escaped.  There is an "underscore" package which eases the pain somewhat, but there are still important contexts where apparently the '_' must be written as '\_'.  Attached is a working example, which demonstrates several problem-free usages of an un-escaped '_'.

One problem area however appears to be the use of a bare '_' within a section tittle.  For example, this change results in a latex compilation error.

< \subsection{{\texttt VAR\_WITH\_UNDERSCORE}}
---
> \subsection{{\texttt VAR_WITH_UNDERSCORE}}

The underscore package reference defines a hook for explicitly defining commands that it should filter for the presence of bare _'s.  Tried that:

> \newcommand{\UnderscoreCommands}{
>      \do\section \do\subsection
>      \do\subsubsection \do\subsubsubsection
>      \do\paragraph \do\subparagraph
> }


but it did not improve the situation.  The latex compilation just failed in a different way.

A few questions:
1. Is there a way to use the underscore package (alone or in combination with other packages and LaTeX code) to support the use of un-escaped _'s within sectioning commands?
2. As an alternative, is it possible to define a command, called \var{<C_variable>} that will automatically escape the _'s within the argument, and apply a suitable monospace font? For example:
  \newcommand{\var}[1]{\texttt{\un_escape{\verb|#1|}}}
where \un_escape performs a character-by-character substitution of _ to \_?
3. Unrelated: I had a heck of a time getting rid of "duplicate" definition warnings for page references, and the fiddling with \pagenumbering didn't help much.  In its current form it is fine and gives the result that I was expecting, except for page numbers on the index listing page.  Perhaps there is way to disable those?
4. Any other suggestions for improving use and style are appreciated.

thanks,
- Gary


test_underscore.tex
test_underscore.pdf

Peter Flynn

unread,
Jun 9, 2012, 8:31:02 PM6/9/12
to latexus...@googlegroups.com
On Sat, Jun 9, 2012 at 9:39 PM, Gary F. <gary....@gmail.com> wrote:

I am working on a document that refers to many "C" variable names, and most of those names have one/more underscores (_'s) in them.  LaTeX wants to treat them as a subscript operator, unless they're escaped.  There is an "underscore" package which eases the pain somewhat, but there are still important contexts where apparently the '_' must be written as '\_'.  Attached is a working example, which demonstrates several problem-free usages of an un-escaped '_'.

One problem area however appears to be the use of a bare '_' within a section tittle. 

You could use the url package, and treat all variablenames as URLs, eg
\subsection{\url{VAR_WITH_UNDERSCORE}}
(so long as you're not using hyperref)

Or just \protect\verb+var_with_underscore+

Have a look at the documentation for the url package

///Peter

Reply all
Reply to author
Forward
0 new messages