在下一页继续使用行号编写代码(minted、tcolorbox)

在下一页继续使用行号编写代码(minted、tcolorbox)

这是我的代码:

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

输出:

在此处输入图片描述

问题:

如何让代码在下一页自动继续并带有行号?

答案1

添加firstnumber=last\newtcblisting{javalst}铸造选项中。

您必须手动拆分列表才能开始新页面。

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}

更新

虽然第一个解决方案允许在两个列表之间引入文本或其他材料,但值得考虑长代码的另一种方便的可能性:使用tcolorbox breakable带有breakable选项键的库。

瓦

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

相关内容