在 \newtcbinputlisting 中,如何更改行号的颜色以及从现有脚本中指定行号?

在 \newtcbinputlisting 中,如何更改行号的颜色以及从现有脚本中指定行号?

我正在通过 minted 包使用 pygments 进行代码突出显示。

我想知道如何更改突出显示的代码块左侧的行号文本的颜色以使用不同的样式。当我使用较暗的样式时,列文本颜色仍然是黑色,我想根据样式更改它。我可以很好地更改 col 背景和 tcb 背景颜色。

输出如下: 在此处输入图片描述

以下是我引用代码高亮的序言:

%%% ====================================================================
%%% Custom code highlighting using Minted and tikz
%%% ====================================================================

\usepackage{tikz}

%%% ====================================================================
%%% Specific colors used for code highlighting
%%% ====================================================================

\definecolor{pythoncodebg}{rgb}{0.99,1,0.99} % light green

% TCB colorbox to put higlighted minted code from pygments inside
\usepackage{tcolorbox}
\tcbuselibrary{minted,skins,breakable}

\usepackage{minted}
\usemintedstyle[python]{default} % Specific color scheme if I invoke \inputminted{python}{path_to_code.py}

%%% ====================================================================
%%% Custom code highlighting using Minted
%%% ====================================================================

% Python normalsize
\newtcbinputlisting{\pythoninput}[2][]{%
    listing file={#2},
    minted language=python,
    minted style=default,
    minted options={
        fontsize=\normal,
        linenos,
        numbersep=1mm,
        breaklines=true,
    },% <-- put other minted options inside the brackets
    overlay={%
        \begin{tcbclipinterior}
            \fill[gray!25] (frame.south west) rectangle ([xshift=5mm]frame.north west);
        \end{tcbclipinterior}
    },
    colback=pythoncodebg,
    colframe=black!70,
    before skip=5pt plus 2pt,
    breakable,
    enhanced,% <-- put other tcolorbox options here
    listing only,#1
}

在我的主 .tex 文件中,我是这样调用命令的:

\pythoninput{code/python/Code1.py}

我的第二个问题是如何使用自定义 \newtcbinputlisting 命令指定行号?我想替换以下 tex 脚本:

\begin{mdframed}[backgroundcolor=bgp]   
    \inputminted[fontsize=\tiny,firstline=12,lastline=35]{python}{./Code/Python/QuadStiffnessShort_Axi.py}
\end{mdframed}

类似于:

\pythoninput[firstline=12,lastline=35]{code/python/Code1.py}

这样我就不必使用 mdframed,而可以简单地使用带有我指定的所有选项的 tcbcolorbox。

谢谢!

答案1

如果你看看minted包裹在 CTAN(或您的计算机)中,您将看到该linenos选项启用了行号,并且这些行号的方面在命令中是硬编码的\thefancyVerbLine。您可以在序言中引入和调整以下代码来更改它

\renewcommand{\theFancyVerbLine}{\sffamily
    \textcolor[rgb]{0.5,0.5,1.0}{\scriptsize
        \oldstylenums{\arabic{FancyVerbLine}}}}

minted options列表样式固定后,您可以使用minted options app钩子添加选项。

\documentclass{article}
%%% ====================================================================
%%% Custom code highlighting using Minted and tikz
%%% ====================================================================

\usepackage{tikz}

%%% ====================================================================
%%% Specific colors used for code highlighting
%%% ====================================================================

\definecolor{pythoncodebg}{rgb}{0.99,1,0.99} % light green

% TCB colorbox to put higlighted minted code from pygments inside
\usepackage[most, minted]{tcolorbox}
%\tcbuselibrary{minted,skins,breakable,hooks}

%\usepackage{minted} %already charged with tcblibrary minted
\usemintedstyle[python]{default} % Specific color scheme if I invoke \inputminted{python}{path_to_code.py}

%%% ====================================================================
%%% Custom code highlighting using Minted
%%% ====================================================================

% Python normalsize
\newtcbinputlisting{\pythoninput}[2][]{%
    listing file={#2},
    minted language=python,
    minted style=default,
    minted options={
        fontsize=\normalsize,
        linenos,
        numbersep=1mm,
        breaklines=true,
    },% <-- put other minted options inside the brackets
    overlay={%
        \begin{tcbclipinterior}
            \fill[gray!25] (frame.south west) rectangle ([xshift=5mm]frame.north west);
        \end{tcbclipinterior}
    },
    colback=pythoncodebg,
    colframe=black!70,
    before skip=5pt plus 2pt,
    breakable,
    enhanced,% <-- put other tcolorbox options here
    listing only,#1,
    }

\renewcommand{\theFancyVerbLine}{\ttfamily
    \textcolor[rgb]{1,0,0}{\small{\arabic{FancyVerbLine}}}}

\begin{document}

\pythoninput{PythonCode1.py}

\pythoninput[minted options app={firstline=2,lastline=3}, colback=purple!10]{PythonCode1.py}
\end{document}

在此处输入图片描述

相关内容