禁忌环境中的代码列表

禁忌环境中的代码列表

我正在尝试将一个小的代码列表插入到tabu表中,但是我无法创建 PDF。

我尝试使用的代码(嗯 - 它的简化形式)如下:

% !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}

以下是构建日志中的错误:

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

当列表在tabu环境之外时,一切都按预期进行。使用不同的表环境(例如tabular)会在表内生成列表。

我的 Tex 代码有问题吗?不tabu支持代码列表?还是我错过了一些显而易见的东西?我只使用 LaTeX 几天,所以如果是这样的话,请原谅我!

答案1

据我所知,即使在环境中也不tabu支持。listingstabu*

然而,我发现这样pythontex做是有效的:

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

必须有人跑

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

然后输出是

在此处输入图片描述

相关内容