如何使用 Minted 包在多个页面中呈现长代码列表?

如何使用 Minted 包在多个页面中呈现长代码列表?

我使用 minted 软件包来格式化论文中的代码。minted 软件包提供了一个“列表”环境,它将其中的代码视为浮点数。在大多数情况下,它都能很好地工作。但是,如何使用 minted 嵌入可能跨越两页或更多页的长代码列表?它的列表环境在这种情况下效果不佳。

我还想使用\caption\label位于铸造代码清单下方。我还想在我的目录中索引清单。

任何帮助都将不胜感激!

-妮可

答案1

作为第一部分的示例,使用 minted 的可破坏 tcb 列表框(tcb 代码在某种程度上改编自本网站上关于引用的另一个答案;javascript 代码来自此页面):

示例 tcb

有三到四种方法可以解决长行换行这一长期问题:这里,对第一个代码块进行了一些(手动)换行和缩进删除,如图所示。

平均能量损失

\documentclass[
    12pt,     
    openright,
    twoside,  
    a4paper,  
    english,  
    draft
]{memoir}
\usepackage{microtype}
\usepackage{minted}
%\usepackage{geometry}
\usepackage{tcolorbox}

\usepackage{tikz}
\usepackage{tkz-euclide}
\tcbuselibrary{minted,skins,breakable,xparse}
\usetikzlibrary{positioning}

\definecolor{corAzulTema}{RGB}{0,66,137}
\definecolor{corFundoCaixas}{RGB}{245,245,245}

\tcbset{
    listing engine=minted,
    minted options={fontsize=\small,linenos,numbersep=3mm,breaklines,escapeinside=@@},
    colback=corFundoCaixas,
    colframe=corAzulTema!40,
    fonttitle=\bfseries,
    listing only,
    left=7mm,
    enhanced,
    breakable,
    drop fuzzy shadow,
    before skip=\baselineskip,
    grow to left by=13mm,grow to right by=15mm,
    overlay={\begin{tcbclipinterior}\fill[corAzulTema!40](frame.south west)rectangle([xshift=7mm]frame.north west);\end{tcbclipinterior}}
}

\newtcbinputlisting{\javaScriptCode}[3][]{%
    listing file={#3},
    title=\texttt{#2},
    colframe=corAzulTema,
    minted language=javascript
}

\DeclareTotalTCBox{\inlineJavaScriptCode}{ s v }{
    reset,
    listing only,
    on line,
    enhanced,
    nobeforeafter,
    tcbox raise base,
    boxrule=0.7pt,
    top=1mm,
    bottom=0mm,
    right=1mm,
    left=1mm,
    boxsep=0.5pt,
    before upper={\vphantom{dlg}},
    colback=corFundoCaixas,
    colframe=corAzulTema,
    drop fuzzy shadow,
        minted options={escapeinside=@@},
%       text=\ttfamily,
    arc=3pt}
{\mintinline{javascript}{#2}}%



\begin{document}

\begin{enumerate}
    \item apostrophe/single quotes: \inlineJavaScriptCode{'string'}
    \item double quotes:  \inlineJavaScriptCode{"string"}
    \item grave: \inlineJavaScriptCode{`string`}
    \item line \inlineJavaScriptCode{let s1 = 'string';    // apostrophe/single quotes}
\end{enumerate}

\javaScriptCode{title}{strings2.js}

\end{document}

js文件:

 StackExchange.ready(function () {

      StackExchange.using("postValidation", function () {
        StackExchange.postValidation.initOnBlurAndSubmit( $('#post-form'), 2, 'answer');
                });


      StackExchange.question.init({canOpenBounty:true,votesCast:[], canViewVoteCounts:true, totalCommentCount:2, shownCommentCount:2, enableTables:true, questionId:642048});

       styleCode();

          StackExchange.realtime.subscribeToQuestion('85', '642048');
          StackExchange.using("gps", function () { StackExchange.gps.trackOutboundClicks('#content', '.js-post-body'); });


            });

 StackExchange.ready(function () {

                StackExchange.using("postValidation", function () {
                    StackExchange.postValidation.initOnBlurAndSubmit($('#post-form'), 2, 'answer');
                });


                StackExchange.question.init({canOpenBounty:true,votesCast:[],canViewVoteCounts:true,totalCommentCount:2,shownCommentCount:2,enableTables:true,questionId:642048});

                styleCode();

                    StackExchange.realtime.subscribeToQuestion('85', '642048');
                    StackExchange.using("gps", function () { StackExchange.gps.trackOutboundClicks('#content', '.js-post-body'); });


            });

 StackExchange.ready(function () {

                StackExchange.using("postValidation", function () {
                    StackExchange.postValidation.initOnBlurAndSubmit($('#post-form'), 2, 'answer');
                });


                StackExchange.question.init({canOpenBounty:true,votesCast:[],canViewVoteCounts:true,totalCommentCount:2,shownCommentCount:2,enableTables:true,questionId:642048});

                styleCode();

                    StackExchange.realtime.subscribeToQuestion('85', '642048');
                    StackExchange.using("gps", function () { StackExchange.gps.trackOutboundClicks('#content', '.js-post-body'); });


            });

 StackExchange.ready(function () {

                StackExchange.using("postValidation", function () {
                    StackExchange.postValidation.initOnBlurAndSubmit($('#post-form'), 2, 'answer');
                });


                StackExchange.question.init({canOpenBounty:true,votesCast:[],canViewVoteCounts:true,totalCommentCount:2,shownCommentCount:2,enableTables:true,questionId:642048});

                styleCode();

                    StackExchange.realtime.subscribeToQuestion('85', '642048');
                    StackExchange.using("gps", function () { StackExchange.gps.trackOutboundClicks('#content', '.js-post-body'); });


            });

 StackExchange.ready(function () {

                StackExchange.using("postValidation", function () {
                    StackExchange.postValidation.initOnBlurAndSubmit($('#post-form'), 2, 'answer');
                });


                StackExchange.question.init({canOpenBounty:true,votesCast:[],canViewVoteCounts:true,totalCommentCount:2,shownCommentCount:2,enableTables:true,questionId:642048});

                styleCode();

                    StackExchange.realtime.subscribeToQuestion('85', '642048');
                    StackExchange.using("gps", function () { StackExchange.gps.trackOutboundClicks('#content', '.js-post-body'); });


            });

答案2

感谢@TeXnician 的评论,我已经使用该包解决了这个问题caption

在序言中:

\usepackage[chapter, newfloat]{minted} % Code listings, with syntax highlighting
\usepackage[font=small]{caption}

\newenvironment{code}{\captionsetup{type=listing}}{}
\SetupFloatingEnvironment{listing}{name=Code}

%   Code list in content index
\renewcommand{\listoflistings}{
  \cleardoublepage
  \addcontentsline{toc}{chapter}{List of Code}
  \listof{listing}{List of Code}
}

\setminted{
    frame=lines,
    framesep=2mm,
    bgcolor=Snow1,
    fontsize=\small,
    linenos,
    breaklines
}

在章节或节中:

\begin{code}
    \begin{minted}{yaml}
# long long YAML code
# long long YAML code
...

    \end{minted}
    \captionof{listing}{A very long YAML snippet}
    \label{lst:longYamlSnippet}
\end{code}

相关内容