6

I have a table with a bigger table head. How can I vertically center the table head?

\documentclass[11pt,a4paper]{article}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage[table,xcdraw]{xcolor}
\usepackage[margin=2.5cm]{geometry}

\newcolumntype{C}{>{\centering\arraybackslash}X} 

\begin{document}  

\begin{table}[h]
\rowcolors  {2}{lightgray}{white}
\resizebox{\textwidth}{!}{%
\begin{tabularx}{1\textwidth}{C|C|C}
\rowcolor{white}
U / V & I / A & $P_G$ / W     \\[5pt] \cline{1-3}
39,00    & 1,35  & 52,65  \\
39,50  & 1,30   & 51,35  \\
39,00    & 1,20   & 46,80   \\
38,80  & 1,10   & 42,68  \\
38,60  & 1,08  & 41,69 \\
38,50  & 1,02  & 39,27  \\
38,50  & 0,92  & 35,42  \\
38,20  & 0,87  & 33,23 \\
\end{tabularx}%
}
\end{table}

\end{document}
3

two solution (among possibles):

  • remove [5pt] after \\ (as I show you in my answer on your previous question)
  • in case that you like to have more vertical spaces in column headers, you can use S vertical space modifier from the callspace package as shown in MWE below:
\documentclass[11pt,a4paper]{article}
\usepackage[margin=2.5cm]{geometry}
%\usepackage{graphicx}
\usepackage[table,xcdraw]{xcolor}

\usepackage{cellspace, tabularx}
\setlength\cellspacetoplimit{2ex}   % select amount according to your wish
\setlength\cellspacebottomlimit{2ex}% select amount according to your wish
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand\mc[2]{\multicolumn{1}{#1}{#2}} % <---

\begin{document}
    \begin{table}[h]
\rowcolors  {2}{lightgray}{white}
\begin{tabularx}{1\textwidth}{C|C|C}
    \hline
\rowcolor{white}
\mc{Sc|}{U / V} & \mc{Sc|}{I / A}   & \mc{Sc}{$P_G$ / W}   \\ % <---
    \hline
39,00       & 1,35          & 52,65             \\
39,50       & 1,30          & 51,35             \\
39,00       & 1,20          & 46,80             \\
38,80       & 1,10          & 42,68             \\
38,60       & 1,08          & 41,69             \\
38,50       & 1,02          & 39,27             \\
38,50       & 0,92          & 35,42             \\
38,20       & 0,87          & 33,23             \\
\end{tabularx}%
    \end{table}
\end{document}

which gives:

enter image description here

(first \hline` I add that you can see, that cells contents are vertical centered. if you not liked, remove it from code)

You may liked solution with adding \setlength\extrarowheight{1pt} after `\begin{table}:

    \begin{table}[h]
    \setlength\extrarowheight{1pt}
\rowcolors  {2}{lightgray}{white}
\begin{tabularx}{1\textwidth}{C|C|C}
    \hline
\rowcolor{white}
\mc{Sc|}{U / V} & \mc{Sc|}{I / A}   & \mc{Sc}{$P_G$ / W}   \\
    \hline
...

With it you will obtain:

enter image description here

With this the content is not better vertically centered (distance from top of text and bottom of text (considering comas) are not equal anymore, but visually may looks better.

off topic: in your case of three column table \cline{1-3} gives the same result as \hline.

  • Thank you, this works! And yes, I'm aware of \cline and \hline. I use \cline because \hline changes the order of the background color and I want the first row (39,00;1,35 etc) to be colored. \hline starts with the second row and I can't change it to first row. – latexcanbedifficult May 28 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.