我有一张这样的表格,改编自模板:
\begin{table}[!htbp]
\centering
\caption{List of Open and Closed Evaluation Properties}
\label{table:openclosed}
\begin{tabular}{|@{}c@{}|@{}c@{}|}
\hline
\multicolumn{1}{|c|}
{\textbf{Open Properties}} &
\textbf{Closed Properties} \\
\hline
\begin{tabular}[|c]{@{}l@{}}
What is the biggest city in USA\\
New York is the biggest city in USA \\
\hline
\end{tabular}
&
\begin{tabular}[|c]{@{}l@{}}
What is the biggest city in USA\\
New York is the biggest city in USA \\
\hline
\end{tabular}
\hline
\end{tabular}
\end{table}
看起来很完美:
但是,对于这里的每个表格,我都必须\hline
为其自己的环境创建一个。\hline
为嵌套表格所在的更广泛的总体表格放置一个(这样我就不必定义\hline
两次,只需定义一次)会把事情搞乱。注意 - 类似于此 -嵌套表格环境中的水平线但略有不同。
我如何才能在不需要多个 hline 的情况下实现我想要的效果?
答案1
我不明白你想实现什么。正如 Runar Trollet 指出的那样,你可以在不使用嵌套表的情况下实现这一点,例如将列类型更改c
为>\centering\arraybackslash}p{<width>}
(为此,你需要array
在序言中添加包)或使用tabularx
其列类型X
如下>\centering\arraybackslash}X
:
\documentclass{article}
\usepackage{tabularx}
\usepackage{caption}
\begin{document}
\begin{table}[htb]
\centering
\renewcommand\arraystretch{1.5}
\caption{List of Open and Closed Evaluation Properties}
\label{table:openclosed}
\begin{tabularx}{\textwidth}{|*{2}{>{\centering\arraybackslash}X | } }
\hline
\textbf{Open Properties} & \textbf{Closed Properties} \\
\hline
What is the biggest city in USA \newline
New York is the biggest city in USA \
&
What is the biggest city in USA \newline
New York is the biggest city in USA \\
\hline
\end{tabularx}
\end{table}
\end{document}
因为您有表格,并且假设这是其他更复杂的表格,您可以按如下方式简化代码:
\documentclass{article}
\usepackage{caption}
\begin{document}
\begin{table}[!htbp]
\centering
\renewcommand\arraystretch{1.5}
\caption{List of Open and Closed Evaluation Properties}
\label{table:openclosed}
\begin{tabular}{| c | c |}
\hline
\textbf{Open Properties} & \textbf{Closed Properties} \\
\hline
\begin{tabular}{@{}l@{}}
What is the biggest city in USA \\
New York is the biggest city in USA \\
\end{tabular}
& \begin{tabular}{@{}l@{}}
What is the biggest city in USA \\
New York is the biggest city in USA \\
\end{tabular} \\
\hline
\end{tabular}
\end{table}
\end{document}
在两种情况下,您都会获得相同的表格外观:
答案2
您的输出真的看起来完美吗?当然,这取决于个人观点,但我建议不要删除单元格内的所有空间,因为您的文本和规则非常接近,甚至接触,难以阅读。
至于您的表格,我建议只使用常规语法,实际上没有什么特别的。我制作了一个新的宏,使标题的格式更加一致,并且如果您改变主意,更容易更改标题的外观。
我还添加了另一种方法,让您的表格看起来更专业。垂直线被删除,因为这里的阅读方向是水平的,而不是垂直的。水平线被赋予了一些不同且一致的权重,使用和-package中的。它们用于标题的上方和下方以及表格的末尾,而不是常规行之间\toprule
。\midrule
\bottomrule
booktabs
我还添加了caption
-package,它具有一些非常好的功能,可用于自定义标题,并且默认情况下为表格提供了比常规方法更好的间距。
输出
代码
\documentclass{article}
\usepackage{caption}
\usepackage{booktabs}
\begin{document}
\newcommand{\headerFormatA}[1]{%
\multicolumn{1}{|c|}{\textbf{#1}}
}
\newcommand{\headerFormatB}[1]{%
\multicolumn{1}{c}{#1}
}
\begin{table}[hbt]
\centering
\caption{Alternative using a single tabular}
\begin{tabular}{|l|l|}
\hline
\headerFormatA{Open properties} & \headerFormatA{Closed properties} \\
\hline
What is the biggest city in \textsc{usa}? & What is the biggest city in \textsc{usa}? \\
New York is the biggest city in the \textsc{usa} & New York is the biggest city in the \textsc{usa}\\
\hline
\end{tabular}
\end{table}
\begin{table}[hbt]
\centering
\caption{Improved by using \texttt{booktabs}-package}
\begin{tabular}{ll}
\toprule
\headerFormatB{Open properties} & \headerFormatB{Closed properties} \\
\midrule
What is the biggest city in \textsc{usa}? & What is the biggest city in \textsc{usa}? \\
\addlinespace
New York is the biggest city in the \textsc{usa} & New York is the biggest city in the \textsc{usa}\\
\bottomrule
\end{tabular}
\end{table}
\end{document}
答案3
\begin{tabular}{|p{6cm}|p{6cm}|}
\hline
{\large\textbf{Open Properties}} &
{\large\textbf{Closed Properties} }\\
\hline
What is the biggest city in USA &
New York is the biggest city in USA \\
\hline
What is the biggest city in USA &
New York is the biggest city in USA \\
\hline
\end{tabular}
% this also produces the same result.