我想要使用 LaTeX 布局考试问题目录,要求如下:
- 所有单元格内容应垂直居中
- 问题和答案单元格的文本应左对齐
- 其余单元格的文本应居中
这是所需的布局(X 标记正确答案):
该输出实际上是由我的 LaTeX 代码(附在下面)生成的,但当问题和/或答案比该示例中的长时,它处理得并不好。
我尝试了两种格式化表格的方法:
- 使用
\multirow{3}{=}
让问题跨越三行,并\multicolumn{3}
使用“可能的答案”标题 - 使用嵌套表格来记录“可能的答案”
请注意,我正在使用该tabu
包,因为我可能会考虑使用该longtabu
环境来处理多页表。
这是我当前的 LaTeX 输出,相应的代码附在下面。
\documentclass[draft]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{multirow}
\usepackage{tabu}
\newcommand\question[4]{
\multirow{3}{=}{#1} & #2 & A & \\
\cline{2-4}
& #3 & B & X \\
\cline{2-4}
& #4 & C & \\
\hline
}
\tabulinesep=2.2mm
\begin{document}
Attempt using \texttt{\textbackslash multirow}: \\
\begin{tabu}{|X[l]|X[l]|c|c|}
\hline
\multicolumn{1}{|c|}{Question} & \multicolumn{3}{c|}{Possible answers} \\
\hline
\question{This is a short and simple question}
{First answer}
{Second answer}
{Third answer}
\question{This is a longer question which is still properly centered vertically because the answers are single-line}
{First answer}
{Second answer}
{Third answer}
\question{However, if this question gets even longer, it crosses the cell's border. This is probably an incompatibility between multicolumn and the tabu package}
{First answer}
{Second answer}
{Third answer}
\question{Another short question but not properly centered vertically because the first answer is multi-line}
{First very very very long answer spanning multiple lines}
{Second answer}
{Third answer}
\end{tabu}
\vspace{1cm}
Trying to get rid of \texttt{\textbackslash multirow} by nesting tables produces unwanted padding: \\
\begin{tabu}{|X[l]|X[l]}
\hline
\multicolumn{1}{|c|}{Question} & \multicolumn{1}{c|}{Possible answers} \\
\hline
This is an attempt using nested tables & \begin{tabu}{X|c|c|}
First answer & A & \\
\hline
Second answer & B & X \\
\hline
Third answer & C &
\end{tabu} \\
\hline
\end{tabu}
\end{document}
您是否知道如何实现所需的布局?
提前致谢!
答案1
我一直使用 tabu 包,虽然它已被废弃,而且目前在您想要为单元格背景着色时会出现问题,但它仍然有效,因为它是表格包的扩展,仅可自动化并提供对列的样式和大小分布等控件。
以下代码更新了您使用的命令,以便不必重写太多内容(尽管它删除了每行的调整选项),添加了一个变量来控制命令vpos
的垂直位置,并根据手册第 14 页添加了幻影规则,可以使用条件值打开或关闭该代码。我还将多行文本放在允许其对齐的位置。multirow
\multirow[vpos]{nrows}[bigstruts]{width}[vmove]{text}
multirow
\parbox
结果:
梅威瑟:
\documentclass[draft]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
%Packages for beautifull tables.
\usepackage{tabu}
\usepackage{longtable}
\usepackage{tabularx}
\usepackage{array}
\usepackage[longtable]{multirow}
\usepackage[table]{xcolor}
\newcommand\question[7]{
\ifnum#6=1\rule[#7]{0pt}{20pt} \fi
\multirow{#5}{8cm}{\parbox{8cm}{#1}} & #2 & A & \\ [-\TableLineSize] \tabucline{2-4}
\ifnum#6=1\rule[#7]{0pt}{20pt} \fi
& #3 & B & X \\ [-\TableLineSize] \tabucline{2-4}
\ifnum#6=1\rule[#7]{0pt}{20pt} \fi
& #4 & C & \\ \tabucline -
}
\begin{document}
\begin{table}[!h]
\def\TableLineSize{1.5pt}
\small
\tabulinesep = 5pt
\tabulinestyle{\TableLineSize cyan!50!lime}
\begin{tabu} to \linewidth{|X[10lm]|X[5lm]|X[0.5cm]|X[0.5cm]|}
\tabucline -
\multicolumn{1}{|c|}{Question} & \multicolumn{3}{c|}{Possible answers} \\ \tabucline -
\question{This is a short and simple question}
{First answer }
{Second answer}
{Third answer}{3.2}{0}{0pt}
\question{This is a longer question which is still properly centered vertically because the answers are single-line.}
{First answer}
{Second answer}
{Third answer}{3.2}{0}{0pt}
\question{Thowever, if this question gets even longer, it crosses the cell's border. This is probably an incompatibility between multicolumn and the tabu package {\color{red}{[Not true, the problem is that the height is estimated according to the number of rows that are joined and is controlled by the veriable vpos]} }Then in case the text exceeds the space given by the rows, in the multirrow manual it indicates the trick that you can use to make these rows increase the space to fit the multi-row text.}
{First answer}
{Second answer}
{Third answer}{-1.2}{1}{10pt}
\question{Another short question but not properly centered vertically because the first answer is multi-line {\color{red}{[Not now...]} }}
{First very very very long answer spanning multiple lines}
{Second answer}
{Third answer}{2}{0}{20pt}
\end{tabu}
\caption{Beautiful table using tabu}
\label{tab:tab1}
\end{table}
\end{document}