如何创建如图所示的具有多种对齐方式的表格?第一行(即标题)居中对齐,后续行左对齐?
\documentclass[a4paper]{book}
\usepackage{multicol}
\usepackage{float}
\begin{document}
\begin{table}[H]
\centering
\begin{tabular}{|l|l|}
\hline
\multicolumn{1}{|c}{Compressible} & \multicolumn{1}{|c|}{Incompressible} \\ \hline
aaaaaaaaaaaaaaaaaaaaaaaa & bbbbbbb bbbbbbbbbbbbb bbbbbbbbbbb bbbbbbbbbbbbbbb bbbbbbbbbb bbbbbbbbbb bbbbbb \\
cccccccccccccccccccccccccccc & ddddddddddddddddddddddddddddd \\ \hline
\end{tabular}
\end{table}
\end{document}
答案1
我认为最简单的方法是使用tabularx
。
请注意,该multicol
包用于将文档包含在两列(或更多列)中,而不适用\multicolumn
于表格。
还请注意,因为我已将环境的宽度设置tabularx
为相等\linewidth
,\centering
所以不再需要该指令。
正如 egreg 在他的评论中指出的那样,如果表格没有标题,则center
环境就足够了;如果它有一个标题,那么table
就不应该有这个[H]
选项。
对于更专业的表格,请使用booktabs
。
\documentclass[a4paper]{book}
\usepackage{tabularx}
\usepackage{ragged2e}
\usepackage{float}
\usepackage{caption}
\usepackage{booktabs}
\begin{document}
If your table is at a fixed position, you can use
\texttt{center} environment.
%\begin{table}[H]
\begin{center}
%%\centering %% not needed since width of tabularx env. = \linewidth
\begin{tabularx}{\linewidth}{|l|>{\RaggedRight}X|}
\hline
\multicolumn{1}{|c}{Compressible} & \multicolumn{1}{|c|}{Incompressible} \\ \hline
aaaaaaaaaaaaaaaaaaaaaaaa & bbbbbbb bbbbbbbbbbbbb bbbbbbbbbbb bbbbbbbbbbbbbbb bbbbbbbbbb bbbbbbbbbb bbbbbb \\
cccccccccccccccccccccccccccc & ddddddddddddddddddddddddddddd \\ \hline
\end{tabularx}
%\end{table}
\end{center}
If your table has a caption, the \texttt{[H]} option should not be used, because it can float.
Table \ref{tab:mytab} is an example
with \texttt{booktabs}.
\begin{table}[htb]
\caption{\label{tab:mytab}A professional table}
\begin{tabularx}{\linewidth}{l>{\RaggedRight}X}
\toprule
\multicolumn{1}{c}{Compressible} & \multicolumn{1}{c}{Incompressible} \\
\midrule
aaaaaaaaaaaaaaaaaaaaaaaa & bbbbbbb bbbbbbbbbbbbb bbbbbbbbbbb bbbbbbbbbbbbbbb bbbbbbbbbb bbbbbbbbbb bbbbbb \\
cccccccccccccccccccccccccccc & ddddddddddddddddddddddddddddd \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}