[Tex/LaTex] minted line number overlapped by tcolorbox frame

mintedtcolorbox

I want to number my code lines. I am using a tcolorbox around the minted environment.

\documentclass[11pt,letterpaper]{article}
\usepackage{minted}
\usepackage{tcolorbox}

\BeforeBeginEnvironment{minted}
     {\begin{tcolorbox}[breakable, enhanced]}

\AfterEndEnvironment{minted}
   {\end{tcolorbox}}

\begin{document}
\begin{minted}[linenos, breaklines=true, breakbefore=., fontsize=\footnotesize]{c}
#include <stdio.h>

void main ()
{
    printf("hello world");
    printf("hello world");
    printf("hello world");
    printf("hello world");
}
\end{minted}

\end{document}

Here is the output:

enter image description here

I want to avoid the overlapping between the tcolorbox frame and the line numbers.

Best Answer

You can change the default value for minted parameter numsep. It fixes the distance between numbers and lines, using a lower value move numbers to the right of border line.

By the way, as an alternative to minted environment, you can use a \newtcblisting (or \NewTCBListing) where minted is declared a listing engine. Loading minted tcolorboxlibrary, already loads minted package.

\documentclass[11pt,letterpaper]{article}
%\usepackage{minted}
\usepackage[most, minted]{tcolorbox}

\newtcblisting{myminted}{%
    listing engine=minted,
    minted language=c,
    listing only,
    breakable,
    enhanced,
    minted options = {
        linenos, 
        breaklines=true, 
        breakbefore=., 
        fontsize=\footnotesize, 
        numbersep=2mm
    },
    overlay={%
        \begin{tcbclipinterior}
            \fill[gray!25] (frame.south west) rectangle ([xshift=4mm]frame.north west);
        \end{tcbclipinterior}
    }   
}

\BeforeBeginEnvironment{minted}
     {\begin{tcolorbox}[breakable, enhanced]}

\AfterEndEnvironment{minted}
   {\end{tcolorbox}}

\begin{document}
\begin{minted}[linenos, breaklines=true, breakbefore=., fontsize=\footnotesize]{c}
#include <stdio.h>

void main ()
{
    printf("hello world");
    printf("hello world");
    printf("hello world");
    printf("hello world");
}
\end{minted}

\begin{myminted}
#include <stdio.h>

void main ()
{
    printf("hello world");
    printf("hello world");
    printf("hello world");
    printf("hello world");
}
\end{myminted}

\end{document}

enter image description here

Update: optional language

You can add optional and mandatory parameters to a \newtcblisting command, but take care with this advise from tcolorbox documentation:enter image description here

Therefore I've preferred to declare myminted environment with two parameters, one mandatory (the language) and one optional (anything else you want to change in initial definition). An example with the new definition is:

\documentclass[11pt,letterpaper]{article}
%\usepackage{minted}
\usepackage[most, minted]{tcolorbox}

\newtcblisting{myminted}[2][]{%
    listing engine=minted,
    minted language=#2,
    listing only,
    breakable,
    enhanced,
    minted options = {
        linenos, 
        breaklines=true, 
        breakbefore=., 
        fontsize=\footnotesize, 
        numbersep=2mm
    },
    overlay={%
        \begin{tcbclipinterior}
            \fill[gray!25] (frame.south west) rectangle ([xshift=4mm]frame.north west);
        \end{tcbclipinterior}
    },
    #1   
}

\begin{document}

\begin{myminted}{c}
#include <stdio.h>

void main ()
{
    printf("hello world");
    printf("hello world");
    printf("hello world");
    printf("hello world");
}
\end{myminted}

\begin{myminted}[colframe=red,colback=blue!15,sharp corners]{java}
public class HelloWorld {
  // A 'Hello World' in Java
  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
}
\end{myminted}

\end{document}

enter image description here

Related Question