1

I am writing a document with loads of code snippets, and page breaks make it hard to read. I would like to prevent those if possible. I saw in Avoid page breaks in \lstlistings that one of the solutions is to wrap the \lstlisting with a \minipage.

Now, instead of writing this every time, I would like to create a new \lstnewenvironment to do that for me. Here is what I tried:

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

and further down:

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

However, the compilation crashes as soon as it reaches the code:

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

I checked using the tip given in Emergency stop (job aborted/no legal end found), and it indeed crashes once it reaches the codesnippet environment.

Is there a way to circumvent this? Or am I doing this wrong?

Here is a minimal example that crashes:

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

and the resulting log:

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!
3
  • do not show only snippets. It is much easier to test and debug your issue if you provide a small but complete example. Commented Jun 13, 2023 at 8:28
  • Sure can do, I'll edit the post right away Commented Jun 13, 2023 at 8:40
  • It compiles if you use \minipage{\linewidth} and \endminipage
    – daleif
    Commented Jun 13, 2023 at 9:10

1 Answer 1

1

I'm not sure why definining such an environment. Anyway, the documentation of listings is not really clear about the issue, but when verbatim material is concerned, inner environments have to be called by their internal names, here \minipage and \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}

Here I reproduce what I get with columns=flexible, because without this option the output is really awful.

enter image description here

1
  • The goal is to have a command to automatically have the \minipage applied to code snippets, as well as noindent, because for some reason the listings in the appendix get the paragraph indent applied to them. It this an anti-pattern? Commented Jun 13, 2023 at 9:41

You must log in to answer this question.

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