2

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)

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}
\usepackage{longtable}
\definecolor{bg}{RGB}{240,240,240}

\begin{document}

\begin{longtable}{|p{0.8\textwidth}|p{.2\textwidth}|}\hline
\begin{tcblisting}{colback=bg,size=minimal,hbox,
          listing only,listing options={language=Mathematica}}
f[x_] := Sin[x];
Plot[f[x],{x,-Pi,Pi}]
\end{tcblisting}
&
plot command
\\\hline
\end{longtable}
\end{document}

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
(./foo.tex
LaTeX2e <2015/10/01> patch level 2
Babel <3.9m> and hyphenation patterns for 79 languages loaded.
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
.....
(/usr/local/texlive/2015/texmf-dist/tex/latex/listings/lstlang1.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/listings/lstlang1.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/listings/lstlang1.sty)
(./foo.listing)
! 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!

filelist

 *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)
 fontenc.sty
   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)
  pgfrcs.code.tex
 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)
  pgfsys.code.tex
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
 pgfcore.code.tex
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)
supp-pdf.mkii
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 '16 at 18:09
  • @ChristianHupfer but it works in tabular? – Nasser May 30 '16 at 18:10
  • Yes, indeed, I am surprised about this – user31729 May 30 '16 at 18:13
  • I think, this is a longtable speciality. David Carlisle will know some way (if there's any) – user31729 May 30 '16 at 18:50
3

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

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{many,listings}
\usepackage{longtable}
\definecolor{bg}{RGB}{240,240,240}

\begin{document}

\begin{longtable}{|p{0.8\textwidth}|p{.2\textwidth}|}\hline
\begin{tcblisting}{
  nobeforeafter, % <--- added
  colback=bg,
  size=minimal,
  hbox,
  listing only,
  listing options={language=Mathematica}
}
f[x_] := Sin[x];
Plot[f[x],{x,-Pi,Pi}]
\end{tcblisting}
&
plot command
\\\hline
\end{longtable}
\end{document}

enter image description here

|improve this answer|||||

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.