我正在为一个两列文档使用 IEEEtran 样式。我希望我的表格单元格水平和垂直居中对齐。第一行必须是多行,并且某些列的行数多于其他列,但我的代码没有将其水平居中对齐。
\begin{tabular}{|c|c|c|c|}
\hline
\multicolumn{1}{|p{0.2\columnwidth}|}{\centering {\bfseries a few rows of text}} & \multicolumn{1}{p{0.2\columnwidth}|}{\centering {\bfseries a few rows of text}} & \multicolumn{1}{p{0.2\columnwidth}|}{\centering {\bfseries a few rows of text}} & \multicolumn{1}{p{0.2\columnwidth}|}{\centering {\bfseries a few more more rows of text}}\\
\hline
Three & Four&5&6\\
\hline
\end{tabular}
由此产生了如下结果:
显然第一行没有垂直对齐在中心。
有没有什么简单的办法可以解决这个问题?
答案1
有多种方法可以实现这一点。要么使用array
的m{<len>}
列(而不是p{<len>}
),它会自动将锚点置于垂直中心。或者,使用makecell
手动堆叠内容(这也提供了默认的垂直中心对齐:
\documentclass{IEEEtran}
\usepackage{makecell,array}
\begin{document}
\begin{tabular}{|c|c|c|c|}
\hline
\multicolumn{1}{|p{0.2\columnwidth}|}{\centering\bfseries a few rows of text}
& \multicolumn{1}{p{0.2\columnwidth}|}{\centering\bfseries a few rows of text}
& \multicolumn{1}{p{0.2\columnwidth}|}{\centering\bfseries a few rows of text}
& \multicolumn{1}{p{0.2\columnwidth}|}{\centering\bfseries a few more more rows of text} \\
\hline
Three & Four& 5 & 6 \\
\hline
\end{tabular}
\bigskip
\begin{tabular}{|c|c|c|c|}
\hline
\multicolumn{1}{|m{0.2\columnwidth}|}{\centering\bfseries a few rows of text}
& \multicolumn{1}{m{0.2\columnwidth}|}{\centering\bfseries a few rows of text}
& \multicolumn{1}{m{0.2\columnwidth}|}{\centering\bfseries a few rows of text}
& \multicolumn{1}{m{0.2\columnwidth}|}{\centering\bfseries a few more more rows of text} \\
\hline
Three & Four& 5 & 6 \\
\hline
\end{tabular}
\bigskip
\begin{tabular}{|c|c|c|c|}
\hline
\multicolumn{1}{|p{0.2\columnwidth}|}{\bfseries \makecell{a few rows \\ of text}}
& \multicolumn{1}{p{0.2\columnwidth}|}{\bfseries \makecell{a few rows \\ of text}}
& \multicolumn{1}{p{0.2\columnwidth}|}{\bfseries \makecell{a few rows \\ of text}}
& \multicolumn{1}{p{0.2\columnwidth}|}{\bfseries \makecell{a few more \\ more rows \\ of text}} \\
\hline
Three & Four& 5 & 6 \\
\hline
\end{tabular}
\end{document}
答案2
我不明白为什么您要将列标题放入列类型multicolum
中p
,然后再水平居中内容。您喜欢确定表格宽度吗?如果不是,那么看看以下解决方案是否能满足您的要求:
\documentclass{IEEEtran}
\usepackage{makecell}
\renewcommand\theadfont{\normalsize\bfseries}% <-- defined fonts for column heads
\usepackage{lipsum}% for dummy text
\begin{document}
\lipsum[2]
\begin{table}[htb]
\centering
\caption{my table}
\label{tab:very-important-table}
\setlength\extrarowheight{2pt}
\begin{tabular}{|c|c|c|c|}
\hline
\thead{a few rows\\ of text}
& \thead{a few rows\\ of text}
& \thead{a few rows\\ of text}
& \thead{a few more\\ more rows\\ of text} \\
\hline
Three & Four & 5 & 6 \\
\hline
\end{tabular}
\end{table}
\lipsum
\end{document}
如果是,那么这个包tabularx
可以帮助:
\documentclass{IEEEtran}
\usepackage{makecell,tabularx} % <-- added tabularx
\renewcommand\theadfont{\normalsize\bfseries}
\newcolumntype {C}{>{\centering\arraybackslash}X}% <-- defined new column type
\usepackage{lipsum}% for dummy text
\begin{document}
\lipsum[2]
\begin{table}[htb]
\setlength\extrarowheight{2pt}
\centering
\caption{my table}
\label{tab:very-important-table}
\begin{tabularx}{\columnwidth}{|C|C|C|C|}% determined table width
\hline
\thead{a few rows\\ of text}
& \thead{a few rows\\ of text}
& \thead{a few rows\\ of text}
& \thead{a few more\\ more rows\\ of text} \\
\hline
Three & Four & 5 & 6 \\
\hline
\end{tabularx}
\end{table}
\lipsum
\end{document}