我对 LaTex 还比较陌生,我想在文档中添加一个横向表格。我查看了这个问题并且给定的\afterpage
示例在我的环境中运行良好。但是,当我尝试将列表插入到我的表中时,我不再能够编译该文件。以下是相关代码:
\documentclass[a4paper]{report}
\usepackage{pdflscape}
\usepackage{afterpage}
\usepackage{caption}
\usepackage{listings}
\usepackage{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{frame=none,
language=Java,
aboveskip=3mm,
belowskip=0mm,
showstringspaces=false,
columns=flexible,
basicstyle={\small\ttfamily},
numbers=none,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true,
tabsize=3
}
\begin{document}
\afterpage{
\clearpage
\thispagestyle{empty}
\begin{landscape}
\centering
\begin{tabular}{llll}
\hline
A & B & C &
\begin{lstlisting}
public int add(int a, int b){
int sum = a + b;
return sum;
}
\end{lstlisting}
\\
\hline
\end{tabular}
\captionof{table}{Table caption}
\end{landscape}
\clearpage
}
\end{document}
我运行时得到的错误pdflatex
是:
Runaway argument?
! Paragraph ended before \lst@next was complete.
<to be read again>
\par
l.103 \newpage
{\pagestyle{empty} \cleardoublepage}
?
非常感谢您的帮助!
答案1
listings
不能在另一个命令的参数中使用(逐字命令的一般限制),但\afterpage
这里的命令不执行任何操作,因此您可以简单地将其删除:
\documentclass[a4paper]{report}
\usepackage{pdflscape}
\usepackage{afterpage}
\usepackage{caption}
\usepackage{listings}
\lstset{frame=none,
language=Java,
aboveskip=3mm,
belowskip=0mm,
showstringspaces=false,
columns=flexible,
basicstyle={\small\ttfamily},
numbers=none,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true,
tabsize=3
}
\usepackage{xcolor}% missing
\begin{document}
\thispagestyle{empty}
\begin{landscape}
\centering
\begin{tabular}{llll}
\hline
A & B & C &
\begin{lstlisting}
public int add(int a, int b){
int sum = a + b;
return sum;
}
\end{lstlisting}
\\
\hline
\end{tabular}
\captionof{table}{Table caption}
\end{landscape}
\end{document}