在 LaTeX 中有效地转义许多 _

发布于 2024-08-03 16:37:37 字数 181 浏览 2 评论 0原文

如何在不使用 \_ 的情况下转义 _?

这是问题的示例

word_a_a_a_a_a_b_c_dd

有一个函数可以用于此目的。 然而,我记不起它的名字了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

太阳哥哥 2024-08-10 16:37:37

您是否正在考虑 underscore 包,它重新定义了下划线符号,以便您不必在文本模式下转义它?请参阅此处

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.

幽梦紫曦~ 2024-08-10 16:37:37

除了逐字逐句我不知道。

逐字环境:

\begin{verbatim}
  word_a_a_a_a_a_b_c_dd
\end{verbatim}

内联:

\verb|word_a_a_a_a_a_b_c_dd|

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|
为你鎻心 2024-08-10 16:37:37

我无法使用 underscore 包,因此我使用了 url 包:

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

...

Foo \url{word_a_a_a_a_a_b_c_dd} bar.

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.
掩于岁月 2024-08-10 16:37:37

有趣的是,这是一个针对编程问题的问答网站,但还没有人建议编程。

您可以定义自己的命令来替换下划线标记:

\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}

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}
音盲 2024-08-10 16:37:37

通常在这种情况下您需要等宽字体,因此您可以使用:

\verb|word_a_a_a_a_a_b_c_dd|

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

\verb|word_a_a_a_a_a_b_c_dd|
梦晓ヶ微光ヅ倾城 2024-08-10 16:37:37

您可能还会想到 lstlistingverbatim 环境,它们通常用于显示代码 - 可以包含下划线。然而,这些环境的作用不仅仅是“转义”下划线。

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.

掩耳倾听 2024-08-10 16:37:37

当文本来自另一个宏时,上述大多数方法都会失败,因为人们不想列出宏名称。 \url{} 命令有效,但如果不想生成链接,则还可以使用 hyperref 包中的 \nolinkurl{}。

例如,您可以使用宏 \jobname 来打印主 LaTeX 文件的基本名称。如果文件名包含下划线,这将打印一些有趣的字符来代替下划线。要正确打印文件名,您可以将宏 \jobname 包装在 \nolinkurl 中。

例如,如果文件名是“my_project.tex”,您可以:

  1. 将 \usepackage{hyperref} 添加到序言中。
  2. 使用 \nolinkurl{\jobname} 打印文档中的文件名“my_project”。

诚然,这对于关于如何转义许多下划线的原始问题来说并不是一个好的答案,因为您无法在 \nolinkurl 中放置长文本。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文