To escape many _ in LaTeX efficiently

女生的网名这么多〃 提交于 2019-12-06 17:07:20

问题


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.


回答1:


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.




回答2:


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|



回答3:


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.



回答4:


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

\verb|word_a_a_a_a_a_b_c_dd|



回答5:


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.




回答6:


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}


来源:https://stackoverflow.com/questions/1346512/to-escape-many-in-latex-efficiently

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!