在 longtable 中使用 tcblisting 时出错,而在 tabular 中没有错误。

在 longtable 中使用 tcblisting 时出错,而在 tabular 中没有错误。

我正在测试这个答案中提供的一个很好的解决方案如何将列表中的背景颜色裁剪为代码中最长的一行

然而,我碰巧使用的是longtable,而不是标准tabular。我不认为会有什么不同。

但出于某种原因,给出的代码在内部使用时会产生错误longtable 。一切都完全相同。唯一的区别是tabular,我没有使用,而是将其更改为longtable,现在出现了语法错误。

这是 MWE(与上面链接的代码相同,我只是改为tabularlongtable因为在我的代码中,我实际上将其用于longtable所有内容)

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

错误是

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}

? 

将其改回tabular原始代码,则没有错误!

文件列表

 *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
 ***********

问题是:如何使上述代码在 中工作longtable而不仅仅是在 中工作tabular

答案1

tcolorbox通常\pagebreak在启动时会出现问题(除其他事项外);在 中会忽略这个问题tabular,但在 中不会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}

在此处输入图片描述

相关内容