\lstnewenvironment 无法与 \minipage 配合使用

\lstnewenvironment 无法与 \minipage 配合使用

我正在编写一个包含大量代码片段的文档,分页符使其难以阅读。如果可能的话,我想避免这种情况。我看到避免在 \lstlistings 中分页解决方案之一是用 包装\lstlisting\minipage

现在,我不想每次都写这个,而是想创建一个新的\lstnewenvironment来帮我做这件事。以下是我尝试的:

\lstnewenvironment{codesnippet}[1][]
{
\lstset{#1}
\begin{minipage}{\linewidth}
}
{
\end{minipage}
}

并进一步如下:

\begin{codesnippet}
class Wrapper[T](val x: T)
class IntWrapper(val x: Int)

val numbers = 1 to 1000000
val ws = numbers.map(Wrapper(_))
val iws = numbers.map(IntWrapper(_))

val noUnboxing =
  iws.map(w => IntWrapper(w.x*w.x + 2*w.x)).foldLeft(0)(_ + _.x)
val withUnboxing =
  ws.map(w => Wrapper(w.x*w.x + 2*w.x)).foldLeft(0)(_ + _.x)
\end{codesnippet}

然而,编译一到达代码就崩溃了:

! Emergency stop.
<*> ./main.tex
*** (job aborted, no legal \end found)

我使用给出的提示进行了检查紧急停止(作业中止/未找到合法结束点),一旦进入环境它确实就会崩溃codesnippet

有没有办法解决这个问题?还是我做错了?

这是一个崩溃的最小示例:

\documentclass[a4paper]{article}

\usepackage{listings}

\lstnewenvironment{codesnippet}[1][]
{
\lstset{#1}
\begin{minipage}{\linewidth}
}
{
\end{minipage}
}

\begin{document}

\begin{codesnippet}
class Wrapper[T](val x: T)
class IntWrapper(val x: Int)

val numbers = 1 to 1000000
val ws = numbers.map(Wrapper(_))
val iws = numbers.map(IntWrapper(_))

val noUnboxing =
  iws.map(w => IntWrapper(w.x*w.x + 2*w.x)).foldLeft(0)(_ + _.x)
val withUnboxing =
  ws.map(w => Wrapper(w.x*w.x + 2*w.x)).foldLeft(0)(_ + _.x)
\end{codesnippet}

\end{document}

以及结果日志:

This is pdfTeX, Version 3.141592653-2.6-1.40.25 (MiKTeX 23.5) (preloaded format=pdflatex 2023.5.30) 13 JUN 2023 10:43
entering extended mode
restricted \write18 enabled.
%&-line parsing enabled.
**./test.tex
(test.tex
LaTeX2e <2022-11-01> patch level 1
L3 programming layer <2023-05-15>
(C:\Users\Aisteru\AppData\Local\Programs\MiKTeX\tex/latex/base\article.cls
Document Class: article 2022/07/02 v1.4n Standard LaTeX document class
(C:\Users\Aisteru\AppData\Local\Programs\MiKTeX\tex/latex/base\size10.clo
File: size10.clo 2022/07/02 v1.4n Standard LaTeX file (size option)
)
\c@part=\count185
\c@section=\count186
\c@subsection=\count187
\c@subsubsection=\count188
\c@paragraph=\count189
\c@subparagraph=\count190
\c@figure=\count191
\c@table=\count192
\abovecaptionskip=\skip48
\belowcaptionskip=\skip49
\bibindent=\dimen140
)
(C:\Users\Aisteru\AppData\Local\Programs\MiKTeX\tex/latex/listings\listings.sty
(C:\Users\Aisteru\AppData\Local\Programs\MiKTeX\tex/latex/graphics\keyval.sty
Package: keyval 2022/05/29 v1.15 key=value parser (DPC)
\KV@toks@=\toks16
)
\lst@mode=\count193
\lst@gtempboxa=\box51
\lst@token=\toks17
\lst@length=\count194
\lst@currlwidth=\dimen141
\lst@column=\count195
\lst@pos=\count196
\lst@lostspace=\dimen142
\lst@width=\dimen143
\lst@newlines=\count197
\lst@lineno=\count198
\lst@maxwidth=\dimen144
(C:\Users\Aisteru\AppData\Local\Programs\MiKTeX\tex/latex/listings\lstmisc.sty
File: lstmisc.sty 2023/02/27 1.9 (Carsten Heinz)
\c@lstnumber=\count199
\lst@skipnumbers=\count266
\lst@framebox=\box52
)
(C:\Users\Aisteru\AppData\Local\Programs\MiKTeX\tex/latex/listings\listings.cfg
File: listings.cfg 2023/02/27 1.9 listings configuration
))
Package: listings 2023/02/27 1.9 (Carsten Heinz)
(C:\Users\Aisteru\AppData\Local\Programs\MiKTeX\tex/latex/l3backend\l3backend-pdfte
x.def
File: l3backend-pdftex.def 2023-04-19 L3 backend support: PDF output (pdfTeX)
\l__color_backend_stack_int=\count267
\l__pdf_internal_box=\box53
) (test.aux)
\openout1 = `test.aux'.
LaTeX Font Info: Checking defaults for OML/cmm/m/it on input line 14.
LaTeX Font Info: ... okay on input line 14.
LaTeX Font Info: Checking defaults for OMS/cmsy/m/n on input line 14.
LaTeX Font Info: ... okay on input line 14.
LaTeX Font Info: Checking defaults for OT1/cmr/m/n on input line 14.
LaTeX Font Info: ... okay on input line 14.
LaTeX Font Info: Checking defaults for T1/cmr/m/n on input line 14.
LaTeX Font Info: ... okay on input line 14.
LaTeX Font Info: Checking defaults for TS1/cmr/m/n on input line 14.
LaTeX Font Info: ... okay on input line 14.
LaTeX Font Info: Checking defaults for OMX/cmex/m/n on input line 14.
LaTeX Font Info: ... okay on input line 14.
LaTeX Font Info: Checking defaults for U/cmr/m/n on input line 14.
LaTeX Font Info: ... okay on input line 14.
\c@lstlisting=\count268
LaTeX Font Info: Trying to load font information for OML+cmr on input line 2
5.
(C:\Users\Aisteru\AppData\Local\Programs\MiKTeX\tex/latex/base\omlcmr.fd
File: omlcmr.fd 2022/07/10 v2.5l Standard LaTeX font definitions
)
LaTeX Font Info: Font shape `OML/cmr/m/n' in size <10> not available
(Font) Font shape `OML/cmm/m/it' tried instead on input line 25.
Overfull \hbox (39.0039pt too wide) in paragraph at lines 25--26
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
[]
Overfull \hbox (15.00366pt too wide) in paragraph at lines 27--28
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
[]
LaTeX Font Info: Trying to load font information for OMS+cmr on input line 2
8.
(C:\Users\Aisteru\AppData\Local\Programs\MiKTeX\tex/latex/base\omscmr.fd
File: omscmr.fd 2022/07/10 v2.5l Standard LaTeX font definitions
)
LaTeX Font Info: Font shape `OMS/cmr/m/n' in size <10> not available
(Font) Font shape `OMS/cmsy/m/n' tried instead on input line 28.
)
! Emergency stop.
<*> ./test.tex
*** (job aborted, no legal \end found)
Here is how much of TeX's memory you used:
2143 strings out of 476410
31864 string characters out of 5790057
1859845 words of memory out of 5000000
22565 multiletter control sequences out of 15000+600000
512622 words of font info for 33 fonts, out of 8000000 for 9000
1141 hyphenation exceptions out of 8191
41i,5n,48p,153b,1289s stack positions out of 10000i,1000n,20000p,200000b,200000s
! ==> Fatal error occurred, no output PDF file produced!

答案1

我不确定为什么要定义这样的环境。无论如何,的文档listings并没有真正清楚地说明这个问题,但是当涉及逐字材料时,内部环境必须用其内部名称来调用,这里\minipage\endminipage

\documentclass[a4paper]{article}

\usepackage{listings}

\lstnewenvironment{codesnippet}[1][]
 {\lstset{#1}\minipage{\linewidth}}
 {\endminipage}

\begin{document}

\begin{center}
\begin{codesnippet}
class Wrapper[T](val x: T)
class IntWrapper(val x: Int)

val numbers = 1 to 1000000
val ws = numbers.map(Wrapper(_))
val iws = numbers.map(IntWrapper(_))

val noUnboxing =
  iws.map(w => IntWrapper(w.x*w.x + 2*w.x)).foldLeft(0)(_ + _.x)
val withUnboxing =
  ws.map(w => Wrapper(w.x*w.x + 2*w.x)).foldLeft(0)(_ + _.x)
\end{codesnippet}
\end{center}

\end{document}

在这里我重现了我使用 得到的结果columns=flexible,因为如果没有这个选项,输出真的很糟糕。

在此处输入图片描述

相关内容