I was testing a nice solution provided in this answer how-to-crop-background-color-in-listings-to-the-longest-line-in-the-code

However, I happened to be using longtable and not the standard tabular. I did not think it will make a difference.

But for some reason, the code given generates an error when used inside longtable Everything is exactly the same. Only difference is that instead of using tabular, I changed it to longtable and now a syntax error shows up.

Here is MWE (same code from the above link, I just changed tabular to longtable, since in my code, I am actually using longtable for everything)



          listing only,listing options={language=Mathematica}}
f[x_] := Sin[x];
plot command

Error is

pdflatex foo.tex
This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
LaTeX2e <2015/10/01> patch level 2
Babel <3.9m> and hyphenation patterns for 79 languages loaded.
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
! Misplaced \noalign.
\pagebreak ->\noalign 
                      {\ifnum `}=0\fi \@testopt {\LT@no@pgbk -}4
l.16 \end{tcblisting}


change it back to tabular as in the original code, then there is no error!


 *File List*
 article.cls    2014/09/29 v1.4h Standard LaTeX document class
  size11.clo    2014/09/29 v1.4h Standard LaTeX file (size option)
   t1enc.def    2005/09/27 v1.99g Standard LaTeX file
   color.sty    1999/02/16
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
  pdftex.def    2011/05/27 v0.06d Graphics/color for pdfTeX
infwarerr.sty    2010/04/08 v1.3 Providing info/warning/error messages (HO)
 ltxcmds.sty    2011/11/09 v1.22 LaTeX kernel commands for general use (HO)
listings.sty    2015/06/04 1.6 (Carsten Heinz)
  keyval.sty    2014/10/28 v1.15 key=value parser (DPC)
 lstmisc.sty    2015/06/04 1.6 (Carsten Heinz)
listings.cfg    2015/06/04 1.6 listings configuration
tcolorbox.sty    2015/11/27 version 3.80 text color boxes
     pgf.sty    2015/08/07 v3.0.1a (rcs-revision 1.15)
  pgfrcs.sty    2015/08/07 v3.0.1a (rcs-revision 1.31)
everyshi.sty    2001/05/15 v3.00 EveryShipout Package (MS)
 pgfcore.sty    2010/04/11 v3.0.1a (rcs-revision 1.7)
graphicx.sty    2014/10/28 v1.0g Enhanced LaTeX Graphics (DPC,SPQR)
graphics.sty    2014/10/28 v1.0p Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    1999/03/16 v1.09 sin cos tan (DPC)
graphics.cfg    2010/04/23 v1.9 graphics configuration of TeX Live
  pgfsys.sty    2014/07/09 v3.0.1a (rcs-revision 1.48)
pgfsyssoftpath.code.tex    2013/09/09  (rcs-revision 1.9)
pgfsysprotocol.code.tex    2006/10/16  (rcs-revision 1.4)
  xcolor.sty    2007/01/21 v2.11 LaTeX color extensions (UK)
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
pgfcomp-version-0-65.sty    2007/07/03 v3.0.1a (rcs-revision 1.7)
pgfcomp-version-1-18.sty    2007/07/23 v3.0.1a (rcs-revision 1.1)
verbatim.sty    2014/10/28 v1.5q LaTeX2e package for verbatim enhancements
 environ.sty    2014/05/04 v0.3 A new way to define environments
trimspaces.sty    2009/09/17 v1.1 Trim spaces around a token list
etoolbox.sty    2015/08/02 v2.2a e-TeX tools for LaTeX (JAW)
pdftexcmds.sty    2011/11/29 v0.20 Utility functions of pdfTeX for LuaTeX (HO)
ifluatex.sty    2010/03/01 v1.3 Provides the ifluatex switch (HO)
   ifpdf.sty    2011/01/30 v2.3 Provides the ifpdf switch (HO)
longtable.sty    2014/10/28 v4.11 Multi-page Table package (DPC)
epstopdf-base.sty    2010/02/09 v2.5 Base part for package epstopdf
  grfext.sty    2010/08/19 v1.1 Manage graphics extensions (HO)
kvdefinekeys.sty    2011/04/07 v1.3 Define keys (HO)
kvoptions.sty    2011/06/30 v3.11 Key value format for package options (HO)
kvsetkeys.sty    2012/04/25 v1.16 Key value parser (HO)
etexcmds.sty    2011/02/16 v1.5 Avoid name clashes with e-TeX commands (HO)
epstopdf-sys.cfg    2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live

question is: How to make the above code work in longtable and not just in tabular?

  • I am not sure whether this can really work, using a verbatim content box inside a tabular cell
    – user31729
    May 30, 2016 at 18:09
  • @ChristianHupfer but it works in tabular?
    – Nasser
    May 30, 2016 at 18:10
  • Yes, indeed, I am surprised about this
    – user31729
    May 30, 2016 at 18:13
  • I think, this is a longtable speciality. David Carlisle will know some way (if there's any)
    – user31729
    May 30, 2016 at 18:50

1 Answer 1


tcolorbox normally issues \pagebreak (among other things) when starting; this is ignored in tabular, but not in longtable.



  nobeforeafter, % <--- added
  listing only,
  listing options={language=Mathematica}
f[x_] := Sin[x];
plot command

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.