tabu 環境中的程式碼清單

tabu 環境中的程式碼清單

我正在嘗試將一個小的程式碼清單插入到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>

然後輸出是

在此輸入影像描述

相關內容