例如,使用时\includegraphics{my_picture.png}
无需转义文件名中的下划线。我正在尝试制作自己的命令来显示 Python 代码,但 Python 标识符经常带有下划线,因此我想避免在每个下划线前输入反斜杠。可以吗?
以下是一个例子:
\documentclass{article}
\usepackage{xcolor}
\usepackage{soul}
\definecolor{Light}{gray}{.90}
\sethlcolor{Light}
\newcommand{\pythonCode}[1]{\mbox{\hl{\texttt{#1}}}}
\chardef\_=`_% See: https://tex.stackexchange.com/q/48632/24422
\begin{document}
\section{\pythonCode{\_NumberDict}}
An instance of this class acts like an array of numbers with
generalized (non-integer) indices. A value of zero is assumed
for undefined entries. \pythonCode{\_NumberDict} instances support addition,
and subtraction with other \pythonCode{\_NumberDict} instances, and multiplication
and division by scalars.
\end{document}
我怎样才能修改\pythonCode
宏以便我可以输入\pythonCode{_NumberDict}
而不是\pythonCode{\_NumberDict}
?
答案1
对于这种简单的情况,我会使用\detokenize
:
\documentclass{article}
\usepackage{xcolor}
\definecolor{Light}{gray}{.90}
\DeclareRobustCommand{\pythonCode}[1]{%
\begingroup
\setlength{\fboxsep}{1pt}%
\colorbox{Light}{\ttfamily\vphantom{Ay}\detokenize{#1}}%
\endgroup
}
\begin{document}
\section{\pythonCode{_NumberDict}}
\pythonCode{_NumberDict}
\pythonCode{_NumberDicty}
\end{document}