リストを使用して数字を常にローマ字で固定することは可能ですか?

リストを使用して数字を常にローマ字で固定することは可能ですか?

私のタグは次のとおりです:

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

ここに画像の説明を入力してください


編集:が直立フォントで表示されない問題は、とを1つのユニットとして解析する(どちらもコードを持っているため)という0事実によるものです。listings0,または他の)。次にフックは数値としてOutputOther解析しようとしますが、これは失敗し、期待されるフォントが得られません。0,

回避策として、コマンドに次の行を追加できます\lstset

literate={,}{{\char`\,}}{1}

出力

ここに画像の説明を入力してください

これにより、パーサーはコンマを読み取る前に中断します。この特殊なケースでは、コンマはキーワードやその他の特殊な構文要素の一部ではないため、副作用はありません。残念ながら、listings新しい構文要素の処理を追加することが難しくなるため、ここで 1 つの問題を回避すると、別の問題が発生することがよくあります。

関連情報