如何使用 tabularx 垂直对齐文本

如何使用 tabularx 垂直对齐文本

我正在尝试使用 tabularx 环境垂直对齐下表的文本。这是我的代码:

\newcolumntype{Y}{>{\centering\arraybackslash}X}

\begin {table}[ht]
\caption {General measurements} \label{tab:gen_mes} 
\begin{center}

\begin{tabularx}{\linewidth}{| Y | Y | Y | Y | Y |}
\hline
 Properties & Design A1 & Design B1 & Design C1 & Design D1 \\
\hline
Mechanical movement & yes & yes & yes & yes \\
\hline
Electrostatic movement & yes & yes & yes & yes \\
\hline
First movement $[V]$ & 6 & 24 & 6 & 24 \\
\hline
Hold voltage $[V]$ & 43 & too big & 42 & too big \\
\hline
Rotational stability & quite stable & bad & quite stable & bad \\
\hline
Lateral stability & very good & quite bad & very good & quite good \\
\hline
Maximum frequency [Hz] & 25 & no oscillation (get stuck) & 25 & no oscillation (get stuck) \\
\hline

\end{tabularx}     
\end{center}
\end{table}

感谢您的帮助 !

答案1

X包提供的列类型基本上tabularx是一种美化的p列类型。 -type 列中的单元格内容p是顶部对齐的。要获得垂直居中而不是顶部对齐,您需要运行

\renewcommand\tabularxcolumn[1]{m{#1}}

该包的用户指南第 3 页对此进行了更详细的解释tabularx

为了以印刷上可接受的方式排版科学单位(例如伏特和赫兹),我建议您加载siunitx包并写入\unit{\volt}而不是$V$

单独的评论:您可能需要考虑让表格对读者更具“视觉吸引力”,以便他们真正选择停留一段时间并积极浏览表格的内容。这可以通过摆脱所有垂直规则并使用更少但间隔良好的水平规则来实现。下面的第二个表格显示了如何在实践中借助软件包的规则绘制宏来实现这一点booktabs。第二个表格还努力使标题材料的结构更加系统化;这种努力的结果再次提高了整体可读性。

在此处输入图片描述

\documentclass{article}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\newcolumntype{Z}{>{\raggedright\arraybackslash}X}
\newcolumntype{M}[1]{>{\raggedright\arraybackslash}m{#1}} % for 2nd table
\usepackage{siunitx}
\usepackage{booktabs}
\addtolength\textheight{6cm} % just for this example
\begin{document}


\begin{table}[ht]
\setlength\extrarowheight{2pt} % optional, for a less-cramped look

\caption{General measurements\strut} \label{tab:gen_mes} 

\begin{tabularx}{\linewidth}{| Z | Y | Y | Y | Y |}
\hline
 Properties & Design A1 & Design B1 & Design C1 & Design D1 \\
\hline
Mechanical movement & yes & yes & yes & yes \\
\hline
Electrostatic movement & yes & yes & yes & yes \\
\hline
First movement [\unit{\volt}] & 6 & 24 & 6 & 24 \\
\hline
Hold voltage [\unit{\volt}] & 43 & too big & 42 & too big \\
\hline
Rotational stability & quite stable & bad & quite stable & bad \\
\hline
Lateral stability & very good & quite bad & very good & quite good \\
\hline
Maximum frequency [\unit{\hertz}] & 25 & no oscillation (get stuck) & 25 & no oscillation (get stuck) \\
\hline
\end{tabularx}     
\end{table}

\begin{table}[ht]
\newlength\mylen
\settowidth{\mylen}{First movement} % desired width of first column
\setlength{\tabcolsep}{4.5pt} % default: 6pt

\caption{Same table, but with a more open ``look''\strut} 
\label{tab:gen_mesyyy} 

\begin{tabularx}{\linewidth}{@{} M{\mylen} YYYY @{}}
\toprule
Properties & \multicolumn{4}{c@{}}{Design} \\
\cmidrule(l){2-5}
& A1 & B1 & C1 & D1 \\
\midrule
Mechanical movement & yes & yes & yes & yes \\
\addlinespace
Electrostatic movement & yes & yes & yes & yes \\
\addlinespace
First movement [\unit{\volt}] & 6 & 24 & 6 & 24 \\
\addlinespace
Hold voltage [\unit{\volt}] & 43 & too big & 42 & too big \\
\addlinespace
Rotational stability & quite stable & bad & quite stable & bad \\
\addlinespace
Lateral stability & very good & quite bad & very good & quite good \\
\addlinespace
Maximum frequency [\unit{\hertz}] & 25 & no oscillation (get stuck) & 25 & no oscillation (get stuck) \\
\bottomrule
\end{tabularx}     
\end{table}
\end{document}

相关内容