0

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!

1

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

2
  • Thank you! As a follow-up question, is it possible to add that portion of code inside of the \newtcbinputlisting so that you can have multiple unique languages being highlighted? For example \pythonDARK and \pythonLIGHT and be able to switch between them if need be. Mar 3 at 22:09
  • @ChristopherCreveling I'm not sure to understand what "portion of code" you want to keep or change. You can declare tcolorbox styles like in TiKZ (look at tex.stackexchange.com/q/180898/1952). This way you can input the desired style as the optional parameter in \pythoninput to change listing design,
    – Ignasi
    Mar 4 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.