设置数字时在表格中使用 tcblisting 时出错

设置数字时在表格中使用 tcblisting 时出错

tcblisting添加时出现错误numbers=left,但仅在表格或长表内部时才会出现这种情况。

编译成功

\documentclass{article}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{color}
\definecolor{bg}{RGB}{240,240,240}    
\usepackage{upquote} %to fix string quotes
\usepackage[most]{tcolorbox}
\tcbuselibrary{many,listings}

\begin{document}    
\begin{tabular}{|p{0.5\textwidth} |}
\begin{tcblisting}{
     nobeforeafter,
     listing only,
     size=minimal,
     listing options={language=Mathematica,
                    framerule=0.05pt,
                    %numbers=left,
                    numberstyle=\scriptsize,
                    basicstyle=\ttfamily\normalsize,
                    breaklines         = true,
                    showspaces         = false}
    }
x=10;
\end{tcblisting}
\end{tabular}    
\end{document}

现在,设置numbers=left上面(取消注释),它会出现这个错误

lualatex foo3.tex
This is LuaTeX, Version 0.95.0 (TeX Live 2016) 
 restricted system commands enabled.
(./foo3.tex
LaTeX2e <2016/03/31> patch level 1
...
(/usr/local/texlive/2016/texmf-dist/tex/latex/listings/lstlang1.sty)
Runaway definition?
#1\\left\@endpbox \hskip \tabcolsep \hskip -.5\arrayrulewidth \vrule \ETC.
! Forbidden control sequence found while scanning definition of \lst@temp.
<inserted text> 
}
l.26 \end{tcblisting}

? 

移出tcblisting表格后,使用 编译成功numbers=left。因此,只有在表格环境中才会出现错误。使用 时会出现同样的错误,但中给出了 lstlisting解决方法lstlisting表格内带行号的列表 就是把{}整个环境都放进去。它适用于{\begin{lstlisting}...\end{lstlisting}}并且错误消失,但是这个解决方案不适用于\begin{tcblisting},这是我的尝试,就是把{ }整个东西都放进去,与上面的修复相同,但它不起作用:

\documentclass{article}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{color}
\definecolor{bg}{RGB}{240,240,240}    
\usepackage{upquote} %to fix string quotes
\usepackage[most]{tcolorbox}
\tcbuselibrary{many,listings}

\begin{document}    
\begin{tabular}{|p{0.5\textwidth} |}
{\begin{tcblisting}{
     nobeforeafter,
     listing only,
     size=minimal,
     listing options={language=Mathematica,
                    framerule=0.05pt,
                    numbers=left,
                    numberstyle=\scriptsize,
                    basicstyle=\ttfamily\normalsize,
                    breaklines         = true,
                    showspaces         = false}
    }
x=10;
\end{tcblisting}}
\end{tabular}    
\end{document}

错误是

(/usr/local/texlive/2016/texmf-dist/tex/latex/listings/lstlang1.sty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/listings/lstlang1.sty)
(./foo3.listing)

LaTeX Warning: Characters dropped after `\end{tcblisting}' on input line 25.

! Missing } inserted.
<inserted text> 
                }
l.26     \end{tabular}

? 

问题是:也可以与内部表格numbers=left一起使用吗?tcblisting

Linux 上的 Tl 2016

答案1

因为tcblisting是逐字环境,它必须看到正确的\end{tcblisting},所以使用\scantokens{\begin{tcblisting}...\end{tcblisting}

该数字可以以一定的值移入表格中left skip

在此处输入图片描述

这可以用 LuaLaTeX 和 pdfLaTeX 进行编译(在 TL 2015 上,尚未在我的旧笔记本上更新)

\documentclass{article}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{color}
\definecolor{bg}{RGB}{240,240,240}    
\usepackage{upquote} %to fix string quotes
\usepackage[most]{tcolorbox}
\tcbuselibrary{many,listings}

\begin{document}    
\begin{tabular}{|p{0.5\textwidth}|}
{\scantokens{\begin{tcblisting}{%
        nobeforeafter,
        left skip=20pt,
        listing only,
        size=minimal,
        listing options={language=Mathematica,
          framerule=0.05pt,
          numbers=left,
          numberstyle=\scriptsize,
          basicstyle=\ttfamily\normalsize,
          breaklines         = true,
          showspaces         = false}
      }
      x=10;
\end{tcblisting}}}
\end{tabular}    
\end{document}

相关内容