我的標籤如下:
\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
使得添加新語法元素的處理變得困難,因此解決這裡的一個問題通常會在那裡引發另一個問題。