![如何垂直对齐表格单元格中的文本?](https://linux22.com/image/287447/%E5%A6%82%E4%BD%95%E5%9E%82%E7%9B%B4%E5%AF%B9%E9%BD%90%E8%A1%A8%E6%A0%BC%E5%8D%95%E5%85%83%E6%A0%BC%E4%B8%AD%E7%9A%84%E6%96%87%E6%9C%AC%EF%BC%9F.png)
以下是在 BibTxtMng 中创建表格的乳胶代码
\begin{table}[!htbp]
\centering
\caption{Spanish Traffic Sign according to the color and shape \cite{Paper12}}
\begin{tabular}{|>{\raggedright}p{3.1cm}|>{\raggedright}p{3.1cm}|>
{\raggedright}p{3.1cm}|}
\hline
Color & Shape & Meaning \tabularnewline
\hline
Red Rim & Circle & Prohibition\tabularnewline
\hline
Red Rim (Up) & Triangle & Danger \tabularnewline
\hline
Red Rim (Down) & Triangle & Yield \tabularnewline
\hline
Red & Octagonal & Stop\tabularnewline
\hline
Blue & Square & Recommendation \tabularnewline
\hline
Blue & Circle & Obligation \tabularnewline
\hline
White & Circle & End of Prohibition \tabularnewline
\hline
Yellow & Circle & End of Prohibition (construction)\tabularnewline
\hline
\end{tabular}
\label{Spanish_TS}
\end{table}
这是我得到的输出表
我希望黄色和圆形的文字(最后一行)位于垂直方向的中心。不要像那样放在顶部。
怎么做??
答案1
您可以将m
列说明符用于最后一行的最后一个单元格。
\documentclass{article}
\usepackage{array} %% you need this
\begin{document}
\begin{table}[!htbp]
\centering
\caption{Spanish Traffic Sign according to the color and shape \cite{Paper12}}
\begin{tabular}{|>{\raggedright}p{3.1cm}|>{\raggedright}p{3.1cm}|>
{\raggedright}p{3.1cm}|}
\hline
Color & Shape & Meaning \tabularnewline
\hline
Red Rim & Circle & Prohibition\tabularnewline
\hline
Red Rim (Up) & Triangle & Danger \tabularnewline
\hline
Red Rim (Down) & Triangle & Yield \tabularnewline
\hline
Red & Octagonal & Stop\tabularnewline
\hline
Blue & Square & Recommendation \tabularnewline
\hline
Blue & Circle & Obligation \tabularnewline
\hline
White & Circle & End of Prohibition \tabularnewline
\hline
Yellow & Circle & \multicolumn{1}{|m{3.1cm}|}{End of Prohibition (construction)}\tabularnewline
\hline
\end{tabular}
\label{Spanish_TS}
\end{table}
\end{document}
m
如果列中有可变宽度的文本,则可以对所有列使用列说明符以实现垂直对齐。
答案2
如果您愿意自己手动在行内进行换行,则可以使用\Centerstack
,如下所示。但是在这种情况下,p
除非您想要它们的宽度,否则实际上不需要列。您可以通过删除该声明并取消注释我的代码中的其他 3 行来省去它们。
\documentclass{article}
\usepackage{array}
\usepackage[usestackEOL]{stackengine}
%\edef\tmp{\the\baselineskip}
%\setstackgap{L}{\tmp}
\begin{document}
\begin{table}[!htbp]
\centering
\caption{Spanish Traffic Sign according to the color and shape \cite{Paper12}}
%\begin{tabular}{|l|l|l|}
\begin{tabular}{|>{\raggedright}p{3.1cm}|>{\raggedright}p{3.1cm}|>{\raggedright}p{3.1cm}|}
\hline
Color & Shape & Meaning \tabularnewline
\hline
Red Rim & Circle & Prohibition\tabularnewline
\hline
Red Rim (Up) & Triangle & Danger \tabularnewline
\hline
Red Rim (Down) & Triangle & Yield \tabularnewline
\hline
Red & Octagonal & Stop\tabularnewline
\hline
Blue & Square & Recommendation \tabularnewline
\hline
Blue & Circle & Obligation \tabularnewline
\hline
White & Circle & End of Prohibition \tabularnewline
\hline
Yellow & Circle & \Centerstack[l]{End of\\ Prohibition\\ (construction)}\tabularnewline
\hline
\end{tabular}
\label{Spanish_TS}
\end{table}
\end{document}