我的标签如下:
\documentclass{book}
\usepackage{xcolor}
\usepackage{textcomp,listings}%
\lstnewenvironment{python}[1][]{%
\lstset{%
mathescape=false,%
language=python,%
basicstyle=\ttfamily\normalsize,%
otherkeywords={*,\{, \} },%
keywordstyle=\color{black},%
stringstyle=\color{black},%
showstringspaces=false,%
emph={class, pass, in, for, while, if, is, elif, else, not, and, or,%
def, print, exec, break, continue, return},%
emphstyle=\color{black}\bfseries,%
emph={[3]True, False, None, self},%
emphstyle=[2]\color{black!10},%
emph={[3]from, import, as},%
emphstyle=[3]\color{black},%
upquote=true,%
morecomment=[s]{"""}{"""},%
commentstyle=\color{gray}\slshape,%
aboveskip=12pt,belowskip=12pt,xleftmargin=-2pt,xrightmargin=3pt,framexleftmargin=20pt,framextopmargin=1pt,%
rulesepcolor=\color{gray},#1%
}}{}%
\begin{document}
\begin{python}
In [7]: np.searchsorted(X, 0.5)
Out[7]: 4998210 # This is 1 for test 1000
\end{python}
\begin{python}
In [7]: np.searchsorted(X, 0.5) # left edge 1234567890
from scipy import stats
dist stats.uniform (0, 2) # left edge 0, width 2
\end{python}
\end{document}
它运行良好,没有问题。请确认我需要的arabic numerals (i.e., 0 to 9)
应该始终存在roman
,请参阅屏幕截图:
这个问题能修复吗?请指教...
答案1
这是一个利用在列表:仅包含关键字的彩色数字。
我们需要OutputOther
对该解决方案中的钩子进行一些更改,因为在这种情况下,无论是否在注释中,您都希望覆盖默认样式。新的钩子如下所示:
\lst@AddToHook{OutputOther}{%
\lst@ifparsenumbers
\expandafter\@hook@ifnumber\the\lst@token\@end {%
\let\orig@thestyle=\lst@thestyle
\def\lst@thestyle{\orig@thestyle\lst@numbersstyle}%
}{}%
\fi
}
\lstset
此外,还必须向您的命令添加以下几行:
parsenumbers=true,
numbersstyle=\upshape
listings
无论在输入中的何处解析数字,numbersstyle
都会将其添加到当前活动样式中。
完整示例代码:
\documentclass{book}
\usepackage{xcolor}
\usepackage{textcomp,listings}%
\makeatletter
%%% Copied from https://tex.stackexchange.com/a/500690/23765
% Some conditional tests
\def\@genericif#1{#1\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi}
\def\@ifdigit#1{\@genericif{\ifnum1<1\noexpand#1\relax}}
\def\@ifempty#1{\@genericif{\if\relax\detokenize{#1}\relax}}
% The main parsing macros
\def\parse@num#1{%
\@ifempty{#1}%
{\parse@num@false}%
{\@genericif{\parsesign}%
{\parse@num@sign#1{}\@end}%
{\parse@num@dig#1{}\@end}%
}%
}
% Parse sign
\def\parse@num@sign#1#2\@end{%
\@genericif{\ifx\parse@num@minus#1}%
{\@ifempty{#2}{\parse@num@false}{\parse@num@dig#2\@end}}%
{\@genericif{\ifx\parse@num@plus#1}%
{\@ifempty{#2}{\parse@num@false}{\parse@num@dig#2\@end}}%
{\parse@num@dig#1#2\@end}%
}%
}
% Parse first digit
\def\parse@num@dig#1#2\@end{%
\@ifdigit{#1}%
{\@ifempty{#2}{\parse@num@true}{\parse@num@digs#2\@end}}%
{\parse@num@false}%
}
% Parse optional following digits
\def\parse@num@digs#1#2\@end{%
\@ifdigit{#1}{%
\@ifempty{#2}%
{\parse@num@true}%
{\parse@num@digs#2\@end}%
}{%
\@genericif{\parsefloat}{%
\@genericif{\ifx\parse@num@point#1}%
{\@ifempty{#2}{\parse@num@false}{\parse@num@decs#2\@end}}%
{\parse@num@false}%
}{\parse@num@false}%
}%
}
% Parse decimal places
\def\parse@num@decs#1#2\@end{%
\@ifdigit{#1}{%
\@ifempty{#2}%
{\parse@num@true}%
{\parse@num@decs#2\@end}%
}{\parse@num@false}%
}
% User interface
\newcommand\ifnumber[4][]{%
\begingroup
\let\parsesign=\iftrue
\let\parsefloat=\iftrue
\let\parse@num@minus=-%
\let\parse@num@plus=+%
\let\parse@num@point=.%
#1%
\def\parse@num@true{\endgroup#3}%
\def\parse@num@false{\endgroup#4}%
\parse@num{#2}%
}
%%% Additions to the listings package
\lst@Key{numbersstyle}{}{\def\lst@numbersstyle{#1}}
\lst@Key{parsenumbers}{false}[t]{\lstKV@SetIf{#1}\lst@ifparsenumbers}
\lst@AddToHook{OutputOther}{%
\lst@ifparsenumbers
\expandafter\@hook@ifnumber\the\lst@token\@end {%
\let\orig@thestyle=\lst@thestyle
\def\lst@thestyle{\orig@thestyle\lst@numbersstyle}%
}{}%
\fi
}
\def\@hook@ifnumber#1#2\@end{%
\@genericif{\ifx\lst@nolig#1}%
{\@hook@ifnumber@{#2}}%
{\@hook@ifnumber@{#1#2}}%
}
\def\@hook@ifnumber@{%
\ifnumber[\expandafter\let\expandafter\parse@num@minus\csname lst@um-\endcsname]%
}
\makeatother
\lstnewenvironment{python}[1][]{%
\lstset{%
mathescape=false,%
language=python,%
basicstyle=\ttfamily\normalsize,%
otherkeywords={*,\{, \} },%
keywordstyle=\color{black},%
stringstyle=\color{black},%
showstringspaces=false,%
emph={class, pass, in, for, while, if, is, elif, else, not, and, or,%
def, print, exec, break, continue, return},%
emphstyle=\color{black}\bfseries,%
emph={[3]True, False, None, self},%
emphstyle=[2]\color{black!10},%
emph={[3]from, import, as},%
emphstyle=[3]\color{black},%
upquote=true,%
morecomment=[s]{"""}{"""},%
commentstyle=\color{gray}\slshape,%
aboveskip=12pt,belowskip=12pt,xleftmargin=-2pt,xrightmargin=3pt,framexleftmargin=20pt,framextopmargin=1pt,%
rulesepcolor=\color{gray},#1,
parsenumbers=true,
numbersstyle=\upshape
}}{}%
\begin{document}
\begin{python}
In [7]: np.searchsorted(X, 0.5)
Out[7]: 4998210 # This is 1 for test 1000
"""
Some text 1234
"""
\end{python}
\end{document}
编辑:无法以直立字体显示的问题0
是由于将和listings
解析为一个单元(因为两者都有代码0
,
数字或者其他)。OutputOther
然后钩子尝试将其解析0,
为数字,但解析失败,因此无法提供预期的字体。
作为一种解决方法,您可以将以下行添加到\lstset
命令中:
literate={,}{{\char`\,}}{1}
输出
这会使解析器在读取逗号之前中断。在这种特殊情况下,应该没有副作用,因为逗号不应该是关键字或其他特殊语法元素的一部分。不幸的是,这使得listings
添加新语法元素的处理变得困难,因此在这里解决一个问题通常会引发另一个问题。