TransWikia.com

Inside of a newtcbinputlisting how can I change the color of the line number as well as specify line numbers from an existing script?

TeX - LaTeX Asked on May 5, 2021

I am working on code-highlighting using pygments through the minted package.

I am wondering how I can change the color of the line number text on the left side of the highlighted code block to use with different styles. When I use a darker style, the column text color is still black and I want to change it depending on the style. I can change the col background and tcb background color just fine.

Here is the output:
enter image description here

Here is my preamble that references the code highlighting:

%%% ====================================================================
%%% 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
}

In my main .tex file, here is how I call the command:

pythoninput{code/python/Code1.py}

The second question I have is how do I specify line numbers using a custom newtcbinputlisting command?
I would like to replace the following tex script:

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

with something similar to:

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

So that I don’t have to use mdframed and can simply use the tcbcolorbox with all of the options I have specified.

Thanks!

One Answer

If you look at minted package documentation in CTAN (or your computer), you will see that linenos option enables line numbers and that the aspect of these line numbers is hard-coded in command thefancyVerbLine. You can change it introducing and adapting following code in your preamble

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

When the minted options are fixed in your listings style, you can add options with minted options app hook.

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}

enter image description here

Correct answer by Ignasi on May 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP