1

Here is my code:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{minted}
\usepackage{xcolor}
\usepackage{tcolorbox}
\tcbuselibrary{listings, minted, skins}
\tcbset{listing engine=minted}
\renewcommand{\theFancyVerbLine}{\textcolor[rgb]{1,1,1}{\scriptsize\arabic
{FancyVerbLine}}}
\newtcblisting{javalst}{listing only, minted language=java, minted style=
paraiso-dark, colback=bg, enhanced,frame hidden,
minted options={fontsize=\scriptsize, tabsize=2, breaklines, autogobble,
linenos, numbersep=5pt,fontsize=\small,},
overlay={\begin{tcbclipinterior}\fill[bg](frame.south west)rectangle([
xshift=5mm]frame.north west);\end{tcbclipinterior}}}
\definecolor{inline}{RGB}{187,57,82}
\definecolor{bg}{RGB}{22,43,58}
\setminted[java]{bgcolor=bg, fontfamily=fdm, fontsize=\scriptsize}

\begin{document}
\begin{javalst}
    VERY LONG CODE
\end{javalst}
\end{document}

The output:

enter image description here

Question:

How to continue the code automatically on the next page with line numbering?

1 Answer 1

1

Add firstnumber=last to \newtcblisting{javalst} in minted options.

You have to manually split the list to start a new page.

A

% !TeX TS-program =pdflatex 

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{minted}
\usepackage{xcolor}
\usepackage{tcolorbox}
\tcbuselibrary{listings, minted, skins}
\tcbset{listing engine=minted}

\renewcommand{\theFancyVerbLine}{\textcolor[rgb]{1,1,1}{\scriptsize\arabic
        {FancyVerbLine}}}
    
\newtcblisting{javalst}{%
    listing only, minted language=Java,
    minted style=paraiso-dark, colback=bg, enhanced,frame hidden,
    minted options={fontsize=\scriptsize, tabsize=2, breaklines, autogobble,
    linenos, numbersep=5pt,fontsize=\small,
    firstnumber=last}, % added <<<<<<<<<<<<<<<<<<<<<<
    overlay={%
        \begin{tcbclipinterior}
            \fill[bg](frame.south west)rectangle([xshift=5mm]frame.north west);
        \end{tcbclipinterior}
        }       
    }
                    
\definecolor{inline}{RGB}{187,57,82}
\definecolor{bg}{RGB}{22,43,58}
\setminted[java]{bgcolor=bg, fontfamily=fdm, fontsize=\scriptsize}

\begin{document}
    \begin{javalst}
        VERY LONG CODE
        VERY LONG CODE
        VERY LONG CODE\end{javalst} %HERE to supress the number of the extra line <<<<

\newpage

\begin{javalst}
        MORE LONG CODE
        MORE LONG CODE
        MORE LONG CODE\end{javalst} %HERE to supress the number of the extra line <<<

\end{document}

UPDATE

While the first solution allows to introduce text or other material between the two listing its worth considering another convenient possibility for a long code: to use the tcolorbox breakable library with the breakable option key.

w

% !TeX TS-program =pdflatex 

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{minted}
\usepackage{xcolor}
\usepackage{tcolorbox}
\tcbuselibrary{listings, minted, skins,breakable} %changed <<<<<<<<<<
\tcbset{listing engine=minted}

\renewcommand{\theFancyVerbLine}{\textcolor[rgb]{1,1,1}{\scriptsize\arabic
        {FancyVerbLine}}}
    
\newtcblisting{javalst}{%
    breakable, % added <<<<<<<<<<<<<<<<<<<<<<
    listing only, minted language=Java,
    minted style=paraiso-dark, colback=bg, enhanced,frame hidden,
    minted options={fontsize=\scriptsize, tabsize=2, breaklines, autogobble,
    linenos, numbersep=5pt,fontsize=\small,
 },
    overlay={%
        \begin{tcbclipinterior}
            \fill[bg](frame.south west)rectangle([xshift=5mm]frame.north west);
        \end{tcbclipinterior}
        }       
    }
                    
\definecolor{inline}{RGB}{187,57,82}
\definecolor{bg}{RGB}{22,43,58}
\setminted[java]{bgcolor=bg, fontfamily=fdm, fontsize=\scriptsize}

\begin{document}
    \begin{javalst}
    import java.util.Scanner;
    
    // Binary Search in Java
    
    class Main {
        int binarySearch(int array[], int element, int low, int high) {
            
            // Repeat until the pointers low and high meet each other
            while (low <= high) {
                
                // get index of mid element
                int mid = low + (high - low) / 2;
                
                // if element to be searched is the mid element
                if (array[mid] == element)
                return mid;
                
                // if element is less than mid element
                // search only the left side of mid
                if (array[mid] < element)
                low = mid + 1;
                
                // if element is greater than mid element
                // search only the right side of mid
                else
                high = mid - 1;
            }
            
            return -1;
        }
            import java.util.Scanner;
    
    // Binary Search in Java
    
    class Main {
        int binarySearch(int array[], int element, int low, int high) {
            
            // Repeat until the pointers low and high meet each other
            while (low <= high) {
                
                // get index of mid element
                int mid = low + (high - low) / 2;
                
                // if element to be searched is the mid element
                if (array[mid] == element)
                return mid;
                
                // if element is less than mid element
                // search only the left side of mid
                if (array[mid] < element)
                low = mid + 1;
                
                // if element is greater than mid element
                // search only the right side of mid
                else
                high = mid - 1;
            }
            
            return -1;
        }
        \end{javalst} 

\end{document}
6
  • 1
    Thank You very much! +1 May 5 at 8:08
  • Thebreaking-option should be the main solution, while the handmade breaking can be left for force majeure cases.
    – projetmbc
    May 5 at 14:47
  • 1
    @projetmbc You might want to add text or other material in between explaining the code. May 5 at 14:51
  • @SimonDispa is it possible to make it support numbering more than 99 lines? May 5 at 22:33
  • @WinnieNotThePooh Add to the minted options numbersep=-1ex like minted options={fontsize=\scriptsize, tabsize=2, breaklines, autogobble, linenos, numbersep=5pt, numbersep=-1ex }, May 6 at 15:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .