我尝试在表格环境中对每列使用不同的垂直对齐方式,但无法使其正常工作。这是我的代码:
\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}