2

I have used \arraystretch (following this answer) to reduce the height of rows in a tabular environment. This appears to have reduced the space between the top of the text and the top of the row (which is better, for my purpose), but that space is now much less than the space between the bottom of the text and the bottom of the row. I would like to reduce the latter space as well.

How do I get the cells' text vertically centred within the cells? In every cell, the text is only a single line.

I've tried m and b as column types; that had no discernable effect. This answer seemed relevant and suggested \centering\arraybackslash, but that didn't lower the text either.

Defining and using a command to apply raisebox works in horizontal mode in running text, but not in a tabular's cell. This answer recommended surrounding the raisebox and its two arguments between \smash{ and }. I couldn't guess the right syntax to do that, but that might have been the wrong approach anyway, so I return to the main question of how to get the cells' text vertically centred between the \hlines.

MWE:

\documentclass[12pt,a4paper]{article}
\usepackage{array}% for \newcolumntype

% See https://tex.stackexchange.com/a/35517
\renewcommand{\arraystretch}{0.84}

% See https://tex.stackexchange.com/a/406499
\newcolumntype{C}{>{\centering\arraybackslash}m{4.5mm}}

% A way to lower the next token without using the brace characters
\def\bury{\raisebox{-1mm}}

%\newcolumntype{C}{>{\bury}m{4.5mm}}

% See https://tex.stackexchange.com/a/63199
%\newcolumntype{C}{>{\smash\begingroup\bury}m{4.5mm}<{\endgroup}}

\newenvironment{ta}
{\Huge\bf\begin{tabular}{|C|C|}\hline}{\\ \hline\end{tabular}}

\begin{document}

\begin{ta}
6&7\\ \hline
8&9
\end{ta}

\end{document}

3 Answers 3

2

Another option might be to use the newer tabularray package, which has built in options for controlling placement of borders with the hborder option with abovesep, belowsep, abovesep+ and belowsep+ options. See its documentation.

\documentclass[12pt,a4paper]{article}

\usepackage{tabularray}
\NewColumnType{C}{Q[c,m,wd=4.5mm,font={\Huge\bfseries}]}
\newenvironment{ta}%
{\begin{tblr}{%
    colspec={CC},
    hborder{1-Z}={belowspace=4pt,abovespace=0pt},
    hlines,vlines
}}%
{\end{tblr}}

\begin{document}

\begin{ta}
6&7\\
8&9
\end{ta}

\end{document}

tblr with border settings

2

With {NiceTabular} of nicematrix, you can put \arraystretch to 0 and then, use the key cell-space-limits to put space above and below the elements. With the key hvlines, all the rules will be drawn and with the key columns-width=auto, all the columns will have the same width.

\documentclass[12pt,a4paper]{article}
\usepackage{nicematrix}

\begin{document}

\Huge\bfseries
\renewcommand{\arraystretch}{0}
\begin{NiceTabular}[hvlines,cell-space-limits=4pt,columns-width=auto]{ccc}
6&7&\\
8&9&
\end{NiceTabular}

\end{document}

Output of the above code

1

There is no real meaning of “vertically centered” in a cell, because characters have height and depth.

If your purpose is to typeset tables where the entries have no depth, such as digits, you can set \arraystretch to zero and add a suitable strut, as high as digits plus a padding, and as deep as the padding. Here I use 4pt.

\documentclass[12pt,a4paper]{article}
\usepackage{array}% for \newcolumntype

\newcommand{\mystrut}[1]{%
  \vrule
  width 0pt
  height \dimexpr\fontcharht\font`0+#1\relax
  depth #1\relax
}
\newcolumntype{C}{>{\mystrut{4pt}}c}
\newenvironment{ta}
 {\renewcommand{\arraystretch}{0}
  \Huge\bfseries
  \begin{tabular}{|C|C|}\hline}{\\ \hline\end{tabular}}

\begin{document}

\begin{ta}
6&7\\ \hline
8&9
\end{ta}

\end{document}

enter image description here

Please, be aware that \bf and the other two-letter font commands have been deprecated for more than 25 years.

If you want also characters that extend below the baseline:

\documentclass[12pt,a4paper]{article}
\usepackage{array}% for \newcolumntype

\newcommand{\mystrut}[1]{%
  \vrule
  width 0pt
  height \dimexpr\fontcharht\font`0+#1\relax
  depth \dimexpr\fontchardp\font`y+#1\relax
}
\newcolumntype{C}{>{\mystrut{4pt}}c}
\newenvironment{ta}
 {\renewcommand{\arraystretch}{0}
  \Huge\bfseries
  \begin{tabular}{|C|C|}\hline}{\\ \hline\end{tabular}}

\begin{document}

\begin{ta}
a&b\\ \hline
d&y
\end{ta}

\end{document}

enter image description here

A modification of the code at the top for ensuring equal width.

\documentclass[12pt,a4paper]{article}
\usepackage{array}% for \newcolumntype

\newcommand{\mystrut}[1]{%
  \vrule
  width 0pt
  height \dimexpr\fontcharht\font`0+\tabcolsep\relax
  depth #1\relax
}
\newcolumntype{C}{>{\mystrut{\tabcolsep}}w{c}{\fontcharwd\font`0}}
\newenvironment{ta}
 {\renewcommand{\arraystretch}{0}
  \Huge\bfseries
  \begin{tabular}{|C|C|}\hline}{\\ \hline\end{tabular}}

\begin{document}

\begin{ta}
6&7\\ \hline
8&9
\end{ta}

\bigskip

\begin{ta}
6& \\ \hline
8& 
\end{ta}

\end{document}

enter image description here

The key is the column type w{c}{<dimen>}. Adapt to your needs.

2
  • This has solved the vertical spacing problem. However, column type c, unlike m and p, doesn't take a width param, and I was rather relying on that to force columns to have equal width (including, in particular, when every cell in one column is blank).
    – Rosie F
    Sep 17, 2022 at 17:57
  • @RosieF Added using w{c}{<dimen>}
    – egreg
    Sep 17, 2022 at 19:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .