4

tcblisting gives error when adding numbers=left, but it does this only when inside tabular or longtable.

This compiles OK

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

Now, setting numbers=left above (un comment), it gives this error

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}

? 

Moving tcblisting outside the table, it compiles ok, with numbers=left in place. So the error only shows up when inside table environment. Same error when using lstlisting , but there is a workaround for lstlisting given in listing-with-line-numbers-inside-a-table which is to put {} around the whole environment. It works for {\begin{lstlisting}...\end{lstlisting}} and the error goes away, but this solution does not work for \begin{tcblisting}, here is my try, which is to add { } around the whole thing, same as in the above fix, but it did not work:

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

Error is

(/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}

? 

Question is: Can numbers=left be made to work with tcblisting inside table as well?

Tl 2016 on Linux

1 Answer 1

4

Since tcblisting is a verbatim environment it has to see the correct \end{tcblisting},so use \scantokens{\begin{tcblisting}...\end{tcblisting}.

The number can be shifted into the tabular with some left skip value.

enter image description here

This compiles for me both with LuaLaTeX and pdfLaTeX (on TL 2015, not updated on my old notebook yet)

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .