listings fails in certain corner cases

"Boylan, Ross via texhax" <[email protected]> Fri, 21 Nov 2025 09:06:33 +0000
Newsgroups gmane.comp.tex.texhax
Message-ID <BY3PR05MB81168203B5D6A125E39154C387D5A@BY3PR05MB8116.namprd05.prod.outlook.com>
I was unable to latex my input file in some cases, getting the following error on a line inside of a lstlisting environment from the listings package:

ERROR: Extra }, or forgotten \endgroup.

--- TeX said ---
<recently read> \egroup

l.68   and
           I go on

I have attached a MWE, which is not so small.  There seem to be at least 2 necessary ingredients:

  1.  The \begin{lstlisting} starts at or near the bottom of one page, but the output it produces ends up on the next page.
  2.  The page header and footer (fancyhdr package) contains some text that uses \lstinline.

The first, alone, is insufficient.  This is with listings 1.11a (r76857), but I was getting the same problem with 1.10c.  After generating the attachments I upgraded listings to 1.11b; it made no difference.
Possibly the problem depends on some peculiarities of shipping the section title around for extramarks.

I think I have seen the problem even when the first line of the listing does end up on the bottom of the page before the rest continues on the next.

Papersize is default, which should be US Letter for me.

In some cases I've found putting a conditional break just before the listing (e.g., with the needspace package) is enough to clear up the problem.  Removing the \lstinline from the section name also "solves" it.

Even in  simple cases, i.e., 1 but not 2, I still get strange behavior: later sections of the listing switch to showing spaces explicitly.

It would be great if this just worked.

By the way, the listings manual doesn't say anything explicitly that I could find about how or if it handles material that crosses pages, though there is some discussion of float behavior.

Thanks.
Ross Boylan
test2.tex (application/x-tex, 980 B) - not displayed
test2.log (application/octet-stream, 4.3 KB)
This is pdfTeX, Version 3.141592653-2.6-1.40.28 (TeX Live 2025) (preloaded format=latex 2025.11.19)  21 NOV 2025 00:43
entering extended mode
 restricted \write18 enabled.
 %&-line parsing enabled.
**test2
(./test2.tex
LaTeX2e <2025-11-01>
L3 programming layer <2025-11-06>
(c:/texlive/2025/texmf-dist/tex/latex/base/article.cls
Document Class: article 2025/01/22 v1.4n Standard LaTeX document class
(c:/texlive/2025/texmf-dist/tex/latex/base/size10.clo
File: size10.clo 2025/01/22 v1.4n Standard LaTeX file (size option)
)
\c@part=\count275
\c@section=\count276
\c@subsection=\count277
\c@subsubsection=\count278
\c@paragraph=\count279
\c@subparagraph=\count280
\c@figure=\count281
\c@table=\count282
\abovecaptionskip=\skip49
\belowcaptionskip=\skip50
\bibindent=\dimen148
)
(c:/texlive/2025/texmf-dist/tex/latex/listings/listings.sty
(c:/texlive/2025/texmf-dist/tex/latex/graphics/keyval.sty
Package: keyval 2022/05/29 v1.15 key=value parser (DPC)
\KV@toks@=\toks17
)
\lst@mode=\count283
\lst@gtempboxa=\box53
\lst@token=\toks18
\lst@length=\count284
\lst@currlwidth=\dimen149
\lst@column=\count285
\lst@pos=\count286
\lst@lostspace=\dimen150
\lst@width=\dimen151
\lst@newlines=\count287
\lst@lineno=\count288
\lst@maxwidth=\dimen152

(c:/texlive/2025/texmf-dist/tex/latex/listings/lstpatch.sty
File: lstpatch.sty 2025/11/14 1.11a (Carsten Heinz)
)
(c:/texlive/2025/texmf-dist/tex/latex/listings/lstmisc.sty
File: lstmisc.sty 2025/11/14 1.11a (Carsten Heinz)
\c@lstnumber=\count289
\lst@skipnumbers=\count290
\lst@framebox=\box54
)
(c:/texlive/2025/texmf-dist/tex/latex/listings/listings.cfg
File: listings.cfg 2025/11/14 1.11a listings configuration
))
Package: listings 2025/11/14 1.11a (Carsten Heinz)

==> First Aid for listings.sty no longer applied!
    Expected:
        2024/09/23 1.10c (Carsten Heinz)
    but found:
        2025/11/14 1.11a (Carsten Heinz)
    so I'm assuming it got fixed.
(c:/texlive/2025/texmf-dist/tex/latex/fancyhdr/fancyhdr.sty
Package: fancyhdr 2025/02/07 v5.2 Extensive control of page headers and footers

\f@nch@headwidth=\skip51
\f@nch@offset@elh=\skip52
\f@nch@offset@erh=\skip53
\f@nch@offset@olh=\skip54
\f@nch@offset@orh=\skip55
\f@nch@offset@elf=\skip56
\f@nch@offset@erf=\skip57
\f@nch@offset@olf=\skip58
\f@nch@offset@orf=\skip59
\f@nch@height=\skip60
\f@nch@footalignment=\skip61
\f@nch@widthL=\skip62
\f@nch@widthC=\skip63
\f@nch@widthR=\skip64
\@temptokenb=\toks19
)
(c:/texlive/2025/texmf-dist/tex/latex/fancyhdr/extramarks.sty
Package: extramarks 2025/02/07 v5.2 Extra marks for LaTeX
\c__mark_class_extramarks-left_mark=\marks4
\c__mark_class_extramarks-right_mark=\marks5
)
(c:/texlive/2025/texmf-dist/tex/latex/l3backend/l3backend-dvips.def
File: l3backend-dvips.def 2025-10-09 L3 backend support: dvips
\l__pdfannot_backend_content_box=\box55
\l__pdfannot_backend_model_box=\box56
\g__pdfannot_backend_int=\count291
\g__pdfannot_backend_link_int=\count292
\g__pdfannot_backend_link_sf_int=\count293
)
(./test2.aux)
\openout1 = `test2.aux'.

LaTeX Font Info:    Checking defaults for OML/cmm/m/it on input line 11.
LaTeX Font Info:    ... okay on input line 11.
LaTeX Font Info:    Checking defaults for OMS/cmsy/m/n on input line 11.
LaTeX Font Info:    ... okay on input line 11.
LaTeX Font Info:    Checking defaults for OT1/cmr/m/n on input line 11.
LaTeX Font Info:    ... okay on input line 11.
LaTeX Font Info:    Checking defaults for T1/cmr/m/n on input line 11.
LaTeX Font Info:    ... okay on input line 11.
LaTeX Font Info:    Checking defaults for TS1/cmr/m/n on input line 11.
LaTeX Font Info:    ... okay on input line 11.
LaTeX Font Info:    Checking defaults for OMX/cmex/m/n on input line 11.
LaTeX Font Info:    ... okay on input line 11.
LaTeX Font Info:    Checking defaults for U/cmr/m/n on input line 11.
LaTeX Font Info:    ... okay on input line 11.
\c@lstlisting=\count294

! Extra }, or forgotten \endgroup.
<recently read> \egroup 
                        
l.68   and 
           I go on
? x
 
Here is how much of TeX's memory you used:
 2643 strings out of 468662
 44320 string characters out of 5444562
 458903 words of memory out of 5000000
 31430 multiletter control sequences out of 15000+600000
 629355 words of font info for 45 fonts, out of 8000000 for 9000
 430 hyphenation exceptions out of 8191
 48i,15n,54p,169b,2820s stack positions out of 10000i,1000n,20000p,200000b,200000s
No pages of output.