4

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.

5

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

  • if I want to change the language, what should I do? I mean, if I want to use the same environment for c, java, etc... – edkeveked Oct 18 '17 at 9:22
  • @edkeveked I've updated the answer to include parameters. – Ignasi Oct 18 '17 at 10:59
2

With boxsep you could set the width of the space between the border and your numbers.

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

\BeforeBeginEnvironment{minted}
{\begin{tcolorbox}[breakable, enhanced, boxsep=10pt]}

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

enter image description here

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.