如何定义表的某一列以包含列表?
背景是我想要为 Haskell 编写自己的备忘单,其中主要有包含关键字和简短说明的表格环境。
第一次非常简单的尝试,编译得很好,如下所示:
\documentclass{article}
\usepackage{xcolor}
\definecolor{greencyan}{rgb}{0.5, 1.0, 0.83}
\definecolor{marine}{rgb}{0.0, 0.5, 1.0}
\definecolor{firebrick}{rgb}{0.7, 0.13, 0.13}
\usepackage{listings}
\lstset{
language=haskell,
basicstyle=\ttfamily,
commentstyle=\color{gray}\textit,
keywordstyle=\color{firebrick},
numberstyle=\color{marine},
stringstyle=\color{greencyan},
breaklines=true
}
\usepackage{array}
\begin{document}
\begin{tabular}{ll}
\lstinline$[] $ & empty list initiation \\
\lstinline$++ $ & list concatenation \\
\lstinline$x:list $ & prepend x to list \\
\lstinline$head list$ & first element of a list \\
\lstinline$tail list$ & all but first element of a list \\
\lstinline$init list$ & last element of a list \\
\lstinline$last list$ & all but last element of a list \\
\end{tabular}
\end{document}
然而,\lstinline$<code>$
在整个文档的每个第二个单元格中总是创建一个是相当繁琐的,并且由于数组包允许我通过表格中使用数学环境,所以\newcolumntype{m}{>{$}l<{$}}
我认为为源代码定义一个列类型可能会很有用,如下所示:
\documentclass{article}
... % same as above, not necessary for the problem
\usepackage{array}
\newcolumntype{f}{>{\begin{lstlisting}}l<{\end{lstlisting}}}
\begin{document}
\begin{tabular}{fl}
[] & empty list initiation \\
++ & list concatenation \\
head list & first element of a list \\
tail list & all but first element of a list \\
init list & last element of a list \\
last list & all but last element of a list \\
\end{tabular}
\end{document}
这向我抛出了多达 103 个错误,首先是缺少插入 \endcsname。[ [] 想要使用环境lstlistings
而不是lstinline
,对于备忘单上即将出现的多行函数定义,我尝试将该环境带入表格单元格中,就像我对所做的那样lstinline
:
\documentclass{article}
...
\usepackage{array}
\begin{document}
\begin{tabular}{ll}
\begin{lstlisting} [] \end{lstlisting}& empty list initiation \\
\begin{lstlisting} ++ \end{lstlisting}& list concatenation \\
\begin{lstlisting} head list \end{lstlisting}& first element of a list \\
\begin{lstlisting} tail list \end{lstlisting}& all but first element of a list \\
\begin{lstlisting} init list \end{lstlisting}& last element of a list \\
\begin{lstlisting} last list \end{lstlisting}& all but last element of a list \\
\end{tabular}
\end{document}
这让我陷入一个错误:
: TeX STOPPED: fatal errors occured. Check the TeX log file for details []
检查日志文件,我猜测相关的行如下,尽管我不知道如何修复该错误。
! TeX capacity exceeded, sorry [input stack size=5000].
\end #1->\csname end#1
\endcsname \@checkend {#1}\expandafter \endgroup \if@e...
l.44 \begin{lstlisting} [] \end
{lstlisting} & empty list initiatio...
If you really absolutely need more capacity,
you can ask a wizard to enlarge me.
请注意,行号与 MWE 不匹配,因为我有一个文件,其中包含所有版本,并且可以出于测试目的对其进行注释和取消注释。
这让我想到,尽管这很可惜,但这个lstlistings
环境在表格环境中可能不起作用。所以我尝试将一列定义为一lstinline
列:
\documentclass{article}
... % same as above, not necessary for the problem
\usepackage{array}
\newcolumntype{f}{>{\lstinline$}l<{$}}
\begin{document}
\begin{tabular}{fl}
[] & empty list initiation \\
++ & list concatenation \\
head list & first element of a list \\
tail list & all but first element of a list \\
init list & last element of a list \\
last list & all but last element of a list \\
\end{tabular}
\end{document}
一方面,这对于多行函数定义来说会很麻烦,另一方面我仍然会得到两个错误:
Improper alphabetic consonant. [ []
mwe.tex: TeX STOPPED: File ended while scanning use of \lst@tem\ETC.[]
如果您读到这里,非常感谢您的关注。除了我上面提出的问题和对备忘单的修复之外,有人能解释一下导致所有这些错误的行为吗?
我猜这与环境有关lstlistings
,并且它忽略了大多数正常的 LaTeX 命令,但我仍然不明白为什么只有一个选项有效而所有其他选项都会失败。
答案1
我无法判断这个解决方案有多强大,但是使用collcell
包并定义新列
\newcolumntype{H}{>{\collectcell\lstinline}l<{\endcollectcell}}
似乎至少对你的 MWE 有效。
\documentclass{article}
\usepackage{xcolor}
\definecolor{greencyan}{rgb}{0.5, 1.0, 0.83}
\definecolor{marine}{rgb}{0.0, 0.5, 1.0}
\definecolor{firebrick}{rgb}{0.7, 0.13, 0.13}
\usepackage{listings}
\lstset{
language=haskell,
basicstyle=\ttfamily,
commentstyle=\color{gray}\textit,
keywordstyle=\color{firebrick},
numberstyle=\color{marine},
stringstyle=\color{greencyan},
breaklines=true
}
\usepackage{array}
\usepackage{collcell}
\newcolumntype{H}{>{\collectcell\lstinline}l<{\endcollectcell}}
\begin{document}
\begin{tabular}{Ll}
[] & empty list initiation \\
++ & list concatenation \\
x:list & prepend x to list \\
head list & first element of a list \\
tail list & all but first element of a list \\
init list & last element of a list \\
last list & all but last element of a list
\end{tabular}
\end{document}
答案2
\comment
如果您所追求的只是结构,那么您只需在环境中将描述设置为lstlisting
:
\documentclass{article}
\usepackage{xcolor}
\definecolor{greencyan}{rgb}{0.5, 1.0, 0.83}
\definecolor{marine}{rgb}{0.0, 0.5, 1.0}
\definecolor{firebrick}{rgb}{0.7, 0.13, 0.13}
\usepackage{listings}
\lstset{
language=haskell,
basicstyle=\ttfamily,
commentstyle=\color{gray}\textit,
keywordstyle=\color{firebrick},
numberstyle=\color{marine},
stringstyle=\color{greencyan},
breaklines=true,
mathescape
}
\newcommand{\comment}{\textrm}
\begin{document}
\begin{lstlisting}
[] $\comment{empty list initiation}$
++ $\comment{list concatenation}$
x:list $\comment{prepend x to list}$
head list $\comment{first element of a list}$
tail list $\comment{all but first element of a list}$
init list $\comment{last element of a list}$
last list $\comment{all but last element of a list}$
\end{lstlisting}
\end{document}
甚至换行也不是问题:
% ...
\usepackage{makecell}
\newcommand{\comment}[1]{\textrm{\makecell[tl]{#1}}}
\begin{document}
\begin{lstlisting}
[] $\comment{empty list initiation}$
++ $\comment{list concatenation}$
x:list $\comment{prepend x to list}$
head list $\comment{first element of a list}$
tail list $\comment{all but first element of a list}$
init list $\comment{last element of a list \\ and something below that}$
last list $\comment{all but last element of a list}$
\end{lstlisting}
\end{document}