22

How can you escape _ without the use of \_?

This is the example of the question

word_a_a_a_a_a_b_c_dd

There is one function which you can use for this. However, I cannot remember its name.

Mnementh
  • 50,487
  • 48
  • 148
  • 202
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

6 Answers6

30

Are you thinking of the underscore package, which redefines the underscore symbol so that you don't have to escape it in text mode? See here.

talz
  • 1,004
  • 9
  • 22
John Feminella
  • 303,634
  • 46
  • 339
  • 357
26

Other than verbatim I wouldn't know.

Verbatim environment:

\begin{verbatim}
  word_a_a_a_a_a_b_c_dd
\end{verbatim}

Inline:

\verb|word_a_a_a_a_a_b_c_dd|
Martijn
  • 5,471
  • 4
  • 37
  • 50
  • 1
    A problem is that this doesn't work inside many things, like `\item[\verb|here|]` is illegal. See this answer for an alternative http://tex.stackexchange.com/a/2252/15665 I use `\usepackage[Q=yes,pverb-linebreak=no]{examplep}` and `\item[\Q{this_works}]` – Evgeni Sergeev Aug 01 '13 at 03:44
14

I couldn't get the underscore package to work, so I used the url package:

\usepackage{url}
\urlstyle{sf}  % or rm, depending on your font

...

Foo \url{word_a_a_a_a_a_b_c_dd} bar.
Vebjorn Ljosa
  • 17,438
  • 13
  • 70
  • 88
4

It's funny how this is a question and answer site for programming questions but nobody has suggested programming yet.

You could define your own command which replaces the underscore tokens:

\documentclass{article}

\newcommand{\filename}[1]{\emph{\replunderscores{#1}}}

\makeatletter
% \expandafter for the case that the filename is given in a command
\newcommand{\replunderscores}[1]{\expandafter\@repl@underscores#1_\relax}

\def\@repl@underscores#1_#2\relax{%
    \ifx \relax #2\relax
        % #2 is empty => finish
        #1%
    \else
        % #2 is not empty => underscore was contained, needs to be replaced
        #1%
        \textunderscore
        % continue replacing
        % #2 ends with an extra underscore so I don't need to add another one
        \@repl@underscores#2\relax
    \fi
}
\makeatother


\begin{document}
    \filename{__init__.py}
\end{document}
jakun
  • 624
  • 5
  • 13
  • Thank you for this answer, this is precisely what I needed. I wanted to define a command to add an URL in a footnote but keeping a regular font. `\newcommand{\footurl}[1]{\footnote{\href{#1}{\replunderscores{#1}}}} ` – Tom Feb 15 '22 at 10:24
  • 1
    @Tom I'm glad if my answer has helped you. Please keep in mind, though, that URLs can contain other characters where this approach does not work like `%` or `#`. So you may not always get around changing catcodes and simply speaking that does not work inside of arguments. The url package makes it easy to define a macro containing the URL where this is possible and then use the macro inside of the footnote without changing fonts if you wish so: `\urlstyle{same} ... \urldef{\myurl}\url{some%url#} ... \footnote{see \myurl}` – jakun Mar 08 '22 at 17:30
3

Typically you want a monospaced font in such situations, so you can use this:

\verb|word_a_a_a_a_a_b_c_dd|
Peter
  • 127,331
  • 53
  • 180
  • 211
1

You may also be thinking of the lstlisting or verbatim environments, which are commonly used to display code - which can contain underscores. However, these environments do a lot more than just "escape" underscores.

Eamon Nerbonne
  • 47,023
  • 20
  • 101
  • 166