7

I'm trying to insert a small code listing into a tabu table, however I'm unable to build a PDF.

The code I'm trying to use (well- a simplified form of it) is as follows:

% !TEX encoding = UTF-8 Unicode
\documentclass[11pt]{article} % use larger type; default would be 10pt
\usepackage[utf8]{inputenc} % set input encoding (not needed with XeLaTeX)

%%% PAGE DIMENSIONS
\usepackage{geometry} % to change the page dimensions
\geometry{a4paper} % or letterpaper (US) or a5paper or....

%%% PACKAGES
\usepackage{listings} % code listings
\usepackage{tabu} % for tables

\begin{document}

\begin{tabu}{X|X}
  \begin{lstlisting}
    myFunction() {
      do.Something();
    }
  \end{lstlisting}
  & This does something
\end{tabu}

\end{document}

And here are the errors in the build log:

D:/mwe.tex:22: Forbidden control sequence found while scanning use of \lst@next. [\end{tabu}]
D:/mwe.tex:22: Missing } inserted. [\end{tabu}]
D:/mwe.tex:22: Missing \endgroup inserted. [\end{tabu}]
D:/mwe.tex:22: Missing \endgroup inserted. [\end{tabu}]
D:/mwe.tex:22: Missing \endgroup inserted. [\end{tabu}]
D:/mwe.tex:22: Missing } inserted. [\end{tabu}]
D:/mwe.tex:22: Forbidden control sequence found while scanning use of \lst@next. [\end{tabu}]
D:/mwe.tex:22: Missing } inserted. [\end{tabu}]
D:/mwe.tex:22: Missing \endgroup inserted. [\end{tabu}]
D:/mwe.tex:22: Missing \endgroup inserted. [\end{tabu}]
D:/mwe.tex:22: Missing \endgroup inserted. [\end{tabu}]
D:/mwe.tex:22: Missing } inserted. [\end{tabu}]
D:/mwe.tex:22: Missing $ inserted. [\end{tabu}]
D:/mwe.tex:22: Missing } inserted. [\end{tabu}]
D:/mwe.tex:22: Missing } inserted. [\end{tabu}]
D:/mwe.tex:22: Forbidden control sequence found while scanning use of \lst@next. [\end{tabu}]
D:/mwe.tex:22: Missing } inserted. [\end{tabu}]
D:/mwe.tex:22: Missing \endgroup inserted. [\end{tabu}]
D:/mwe.tex:22: Missing \endgroup inserted. [\end{tabu}]
D:/mwe.tex:22: Missing \endgroup inserted. [\end{tabu}]
D:/mwe.tex:22: Missing } inserted. [\end{tabu}]

When the listing is outside of the tabu environment, everything works as expected. Using a different table environment, such as tabular, produces the listing inside of a table.

Is there an issue with my Tex code? Does tabu not support code listings? Or have I missed something glaringly obvious? I've only been using LaTeX for a handful of days, so forgive me if that's the case!

6
  • 1
    Welcome to TeX.SE. It would be helpful if you took the code in the linked question and added your own text to yield a fully compilable MWE including \documentclass{} (\begin{document} and \end{document}) and the appropriate packages that illustrates the problem . While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem, instead of spending time to first reproduce the problem. – skpblack Sep 30 '14 at 22:22
  • You need to use tabu* when you have verbatim material. Also, I'd suggest you use \lstinline (otherwise I think you would have to use a \parbox). – Peter Grill Sep 30 '14 at 22:28
  • I've updated the question with a better example of what I'm working with. – user2583109 Sep 30 '14 at 22:31
  • @PeterGrill, Changing to tabu* AND lstinline works, however limits me to single-line code listings. I'd like to have a multi-line snippet in the table - sorry if that wasn't clear. – user2583109 Sep 30 '14 at 22:32
  • If you use tabu it isn't necessary to load array package, because tabu loads array internally. – skpblack Sep 30 '14 at 22:36
3

As far as I know, tabu doesn't support listings even in the tabu* environment.

However, I found that pythontex works:

\documentclass[11pt]{article} % use larger type; default would be 10pt
\usepackage[utf8]{inputenc} % set input encoding (not needed with XeLaTeX)

%%% PAGE DIMENSIONS
\usepackage{geometry} % to change the page dimensions
\geometry{a4paper} % or letterpaper (US) or a5paper or....

%%% PACKAGES
\usepackage{pythontex} % code listings
\usepackage{tabu} % for tables

\begin{document}

\begin{tabu*}{X|X}
  \begin{pygments}{c}
    myFunction() {
      do.Something();
    }
  \end{pygments}
  & This does something
\end{tabu*}

\end{document}

One has to run

pdflatex <filename>
pythontex <filename>
pdflatex <filename>

and then the output is

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.