[Tex/LaTex] Vertically align text in the table cell with modified arraystretch

line-spacingtables

I have created simple table which purposefully has all cells, except first row and first column, blank.

\documentclass{book}
\usepackage{rotating}
\linespread{1.2}
\begin{document}
\thispagestyle{empty}
\begin{sideways}
\bgroup
\def\arraystretch{1.5}
\setlength\tabcolsep{10pt}
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|}
\hline
Column 1 & Column 2 & Column 3 & Column 4 & Column 5 & Column 6 & Column 7 & Column 8 & Column 9 & Column 10 & Column 11 \\
\hline
Row 2 & & & & & & & & & & \\
\hline
Row 3 & & & & & & & & & & \\
\hline
Row 4 & & & & & & & & & & \\
\hline
Row 5 & & & & & & & & & & \\
\hline
Row 6 & & & & & & & & & & \\
\hline
\end{tabular}
\egroup
\end{sideways}
\thispagestyle{empty}
\end{document}

As you can see, I have made three important changes: \linespread{1.2} for linespacing for the rest of the document (not relevant for MWE), \def\arraystretch{1.5} for vertical padding inside cells and \setlength\tabcolsep{10pt} for horizontal padding inside cells.

I want to have text in the cells to be centered both vertically and horizontally. Without those three commands above, it seems to happen. However, once I add those three commands for better presentation, they seem to move text in the cells below vertical center. Examples:

enter image description here
enter image description here

Is there any way to keep those spacing/padding adjustments, but at the same time make text in the cell centered both vertically and horizontally? Also, why code of MWE creates PDF with two pages, instead of one?

Best Answer

Two solutions for vertical centring of cell contents:

  • The makecell package has commands for vertical padding of cell: \setcellgapes to define a value for the padding (in preamble), and \makegapedcells to use is in a given environment.
  • The cellspace package works differently: it defines minimal vertical spacings between a cell and the above and below cells: \cellspacetoplimit and cellspacebottomlimit. You have to prefix the corresponding column specifier with the letter S (or C if you use the siunitx package).

Here is a code for both ways:

\documentclass{book}
\usepackage[margin=2cm]{geometry}
\usepackage{rotating}
\usepackage{array, makecell}
\setcellgapes{7.5pt}
\usepackage{cellspace}
\setlength\cellspacetoplimit{7.5pt}
\setlength\cellspacebottomlimit{7.5pt}
\linespread{1.2}

\begin{document}

\pagestyle{empty}
\begin{sideways}
  \bgroup
  \makegapedcells
  \setlength\tabcolsep{10pt}
  \begin{tabular}{|*{11}{c|}}
    \multicolumn{11}{l}{\texttt{With makecell: }}\\[2ex]
    \hline
    Column 1 & Column 2 & Column 3 & Column 4 & Column 5 & Column 6 & Column 7 & Column 8 & Column 9 & Column 10 & Column 11 \\
    \hline
    Row 2 & & & & & & & & & & \\
    \hline
    Row 3 & & & & & & & & & & \\
    \hline
    Row 4 & & & & & & & & & & \\
    \hline
    Row 5 & & & & & & & & & & \\
    \hline
    Row 6 & & & & & & & & & & \\
    \hline
  \end{tabular}
  \egroup
\end{sideways}
\hskip1cm
\begin{sideways}
  \bgroup
  \setlength\tabcolsep{10pt}
  \begin{tabular}{|*{11}{Sc|}}
    \multicolumn{11}{c}{\texttt{With cellspace: }}\\[2ex]
    \hline
    Column 1 & Column 2 & Column 3 & Column 4 & Column 5 & Column 6 & Column 7 & Column 8 & Column 9 & Column 10 & Column 11 \\
    \hline
    Row 2 & & & & & & & & & & \\
    \hline
    Row 3 & & & & & & & & & & \\
    \hline
    Row 4 & & & & & & & & & & \\
    \hline
    Row 5 & & & & & & & & & & \\
    \hline
    Row 6 & & & & & & & & & & \\
    \hline
  \end{tabular}
  \egroup
\end{sideways}

\end{document} 

enter image description here

Related Question