0

I try to use different vertical alignments for each column with tabular environment, but I cannot get it to work. Here is my code:

\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}

Here is the output: Canenter image description here

NickG
  • 193
  • 2
    Welcome to TeX.SX! What output do you expect? If I remember correctly, you only see a difference if a cell contains more than one row. – leandriis Nov 01 '20 at 16:18
  • Is this the output you want to achieve? https://i.stack.imgur.com/xEiPp.png – leandriis Nov 01 '20 at 16:40
  • 1
    b means align on bottom row, t means align on top row so they are the same in a one row parbox, m means align on the vertical centre but as there is a strut this is almost the same as b and t for a one row entry. – David Carlisle Nov 01 '20 at 19:48
  • @leandriis yes, that is the desired output – NickG Nov 02 '20 at 21:15

2 Answers2

3

enter image description here

\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}

leandriis
  • 62,593
2

Very simple to achieve with 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}

enter image description here

Off-topic: see also When should we use \begin{center} instead of \centering?.

CarLaTeX
  • 62,716