![表格中的垂直对齐不起作用](https://linux22.com/image/423346/%E8%A1%A8%E6%A0%BC%E4%B8%AD%E7%9A%84%E5%9E%82%E7%9B%B4%E5%AF%B9%E9%BD%90%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8.png)
我尝试在表格环境中对每列使用不同的垂直对齐方式,但无法使其正常工作。这是我的代码:
\renewcommand{\arraystretch}{2.5}
\begin{table}[H]
\begin{center}
\begin{tabular}{ | m{1cm} | p{1cm} | b{1cm} | }
\hline
%\multirow{-2}{*}{\textbf{Class}}
class & (mW) & (m) \\
\hline
\textbf{1} & 100 & 100\\
\textbf{2} & 2.5 & 10\\
\textbf{3} & 1 & 1\\
\textbf{4} & 0.5 & 0.5\\
\hline
\end{tabular}
\caption{test}
\end{center}
\end{table}
答案1
\documentclass{article}
\usepackage{array}
\usepackage[column=0]{cellspace}
\setlength{\cellspacetoplimit}{3pt}\setlength{\cellspacebottomlimit}{\cellspacetoplimit}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}
\centering
\begin{tabular}{ | >{\bfseries}0{wl{1cm}} | 0{wl{1cm}} | 0{wl{1cm}} | }
\hline
\textnormal{class} & (mW) & (m) \\
\hline
\rowcolor{lightgray} & 100 & \\
\rowcolor{lightgray} 1 & & \\
\rowcolor{lightgray} & & 100 \\
& 2.5 & \\
2 & & \\
& & 10 \\
\rowcolor{lightgray} & 1 & \\
\rowcolor{lightgray} 3 & & \\
\rowcolor{lightgray} & & 1 \\
& 0.5 & \\
4 & & \\
& & 0.5 \\
\hline
\end{tabular}
\caption{test}
\end{table}
\end{document}
答案2
非常简单地实现tabularray
:
\documentclass{article}
\usepackage{tabularray}
\usepackage{xcolor}
\usepackage{float}
\begin{document}
\begin{table}[H]\centering
\begin{tblr}{
colspec={Q[l,m,1cm]Q[l,h,1cm]Q[l,f,1cm]},
vlines,
row{2-Z}={ht=9ex},
row{even}={lightgray},
cell{2-Z}{1}={font=\bfseries}
}
\hline
class & (mW) & (m) \\
\hline
1 & 100 & 100\\
2 & 2.5 & 10\\
3 & 1 & 1\\
4 & 0.5 & 0.5\\
\hline
\end{tblr}
\caption{test}
\end{table}
\end{document}