To escape many _ in LaTeX efficiently
Asked Answered
E

7

23

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.

Eschar answered 28/8, 2009 at 11:47 Comment(0)
J
31

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.

Justino answered 28/8, 2009 at 11:51 Comment(1)
Thank you for pointing that out! I will use this package in my next procject, since my current code breaks because it contains a lot of underscores espaces by \_.Locate
S
27

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|
Sateen answered 28/8, 2009 at 11:52 Comment(1)
A problem is that this doesn't work inside many things, like \item[\verb|here|] is illegal. See this answer for an alternative tex.stackexchange.com/a/2252/15665 I use \usepackage[Q=yes,pverb-linebreak=no]{examplep} and \item[\Q{this_works}]Kaczmarek
V
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.
Vanwinkle answered 27/1, 2010 at 1:11 Comment(1)
Only issue here is that if you define the color of your urls elsewhere in the preamble, this will make them colored.Degeneracy
P
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}
Pistoleer answered 14/7, 2019 at 14:33 Comment(2)
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}}}} Distasteful
@Distasteful 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}Pistoleer
T
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|
Thou answered 27/1, 2010 at 1:13 Comment(0)
Y
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.

Yuyuan answered 28/8, 2009 at 11:53 Comment(0)
G
0

Most methods above fail when the text comes from another macro, because one does not want to list the macro name. The \url{} command works, but if one does not want a link generated, then one can use also \nolinkurl{} from the hyperref package.

For example you could use the macro \jobname to print the base name of your main LaTeX file. This will print some funny characters in place of the underscores, if the filename contains underscores. To print the filename properly, you could wrap the macro \jobname in \nolinkurl.

For example, if the filename is "my_project.tex" you could:

  1. add \usepackage{hyperref} to the preamble.
  2. use \nolinkurl{\jobname} to print the filename "my_project" in your document.

Admittedly, this is not a good answer to the original question on how to escape many underscores, since you won't be able to put a long text in \nolinkurl.

Guileful answered 16/4 at 12:6 Comment(1)
Please add some code to show how to implement your solution.Ermaermanno

© 2022 - 2024 — McMap. All rights reserved.