1

I am trying to center the text in a table both vertically and horizontally. I thought I had managed to do so, but the last column is only centered horizontally. The code I am entering is

\documentclass[11 pt, a4paper, twoside]{report}
\usepackage{array}
\begin{document}

\begin{table} [h]
\centering
\caption{}

\begin{tabular}{ | >{\centering\arraybackslash} m{3cm} | >{\centering\arraybackslash} m{3cm} | >{\centering\arraybackslash} m{4cm} | }
    \hline
    \rowcolor{lightgray} \textbf{Variable} & \textbf{Description} & \textbf{Value} \\ [0.5cm]
\end{tabular}
\end{table}

\end{document}

and it appears

enter image description here

  • (i) please always provide complete but small document beginning with \documntclass{...} and ending with \end{document} (ii) i cant reproduce your problem. welcome to tex.se! – Zarko Mar 20 '18 at 21:01
  • @Zarko using \documentclass{article}\usepackage[table]{xcolor}\usepackage{array} as a preamble in TL2017 does reproduce the problem. – Skillmon Mar 20 '18 at 21:04
  • 1
    The issue is caused by the additional vertical skip in \\[0.5cm]. If you leave that [0.5cm] out, everything works fine. – Skillmon Mar 20 '18 at 21:04
  • @Skillmon but then I can't separate my rows? I only copied the first part of the table, so if I remove the "\\" I am not able to separate my rows. – Rebecca Mar 20 '18 at 21:08
  • @Rebecca you still use \\ but without the optional argument [0.5cm]. – Skillmon Mar 20 '18 at 21:09
2

For a solution which still uses \rowcolor and stuff see @AboAmmar's answer. But if you have the freedom of formatting your tables yourself, consider ditching the colours and vertical rules and use booktabs. Also text like in the "Description" column looks better, if it's left aligned. And consider using p columns instead of m.

\documentclass[]{article}

\usepackage{array}
\usepackage{booktabs}

\begin{document}

\begin{table} [h]
\centering
\caption{}
\begin{tabular}{ >{\centering\arraybackslash} p{3cm} p{3cm} >{\centering\arraybackslash} p{4cm} }
  \toprule
    Variable & Description & Value \\
  \midrule
    Variable & Description which really needs the full width& Value \\
    Variable & Description & Value \\
    Variable & Description & Value \\
  \bottomrule
\end{tabular}
\end{table}
\end{document}

enter image description here

And generally values shouldn't be that wide that you need multiple lines to format them. Instead consider using a S-type column provided by siunitx which horizontally aligns the numbers to have the same digits beneath each other (You need to hide the table header from that by enclosing it with {Value}). And variable names shouldn't be that wide, too, so perhaps use the c or the l specifier there. Additionally if each variable name is to be typeset in math-mode, you can use >{\(}c<{\)} to change that column to use math mode. A \hbox can be used to hide something from that effect (or \text provided by amsmath).

\documentclass[preview,border=2mm]{standalone}

\usepackage{array}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}

\begin{table} [h]
\centering
\caption{}
\begin{tabular}{ >{\(}c<{\)} p{3cm} S }
  \toprule
  \hbox{Variable} & Description & {Value} \\
  \midrule
  x & Description which really needs the full width& 0.12 \\
  \sigma & Description & 10.51 \\
  \delta & Description & 15.43 \\
  \bottomrule
\end{tabular}
\end{table}
\end{document}

enter image description here

1

Don't use \\[0.5cm] to increase the first row height. This is the cause of misalignment in the last cell. You can use your own \strut to do this via a $\vcenter{\hbox{\rule{0cm}{.5cm}}}$, for example, or more easily by adding the bigstrut package and using its bigstrut macro.

Also, you can define \newcolumntype{C}[1]{>{\centering\arraybackslash} m{#1}} for brevity and re-usability of your code.

\documentclass{article}
\usepackage{xcolor}
\usepackage{colortbl}
\usepackage{bigstrut}
\begin{document}

\newcolumntype{C}[1]{>{\centering\arraybackslash} m{#1}}
\begin{table}[h]
\centering
\caption{}
\begin{tabular}{ |C{3cm} |C{3cm} | C{4cm} | }
  \hline
  \rowcolor{lightgray} \textbf{Variable} & \textbf{Description} & \textbf{Value}\bigstrut \\ \hline 
  Variable & Description & Value \\ \hline 
  Variable & Description & Value \\ \hline 
  Variable & Description & Value \\ \hline 
\end{tabular}
\end{table}

\end{document}

enter image description here

  • How would I do it if I want to set the size of the first row? I like the vertical spacing with \bigstrut so I want to use this on the rest of the rows, and then adjust the height of the first grey row. – Rebecca Mar 20 '18 at 21:33
  • @Rebecca -- Simply add this line before the table \renewcommand{\arraystretch}{1.2}, this is equivalent to 20% more row height, you can change this to your need. – AboAmmar Mar 20 '18 at 22:12

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.