3

How can I center a text vertically in a table? I've looked into the documentation that suggests to use tabular m{} or b{} tag, but it doesn't work for me.

MWE:

\documentclass[a4paper]{article}

\usepackage{array}
\usepackage{multirow}
\begin{document}
\begin{tabular}{| m{0.2in} | m{0.6in} | m{0.91in} |}
\hline
Lit & Page & Pages\\
\hline
1 & 2 & 5\\
\hline
\end{tabular}
\end{document}

Produced output:

Produced

Expected output:

Expected

I have read about \raisebox but I don't know how to apply it to the whole table and applying it manually to every cell is tedious.

Update 1: User @Mico suggested to use \setlength\extrarowheight{2pt}, but it changes the row height, so this solution is not precise.

Is it possible to do so without changing the row height?

4
  • 1
    To me the expected and actual outputs look the same. Could you perhaps mark the differences?
    – user121799
    Dec 16, 2018 at 19:09
  • do you need exactly that kind of tabular?
    – user2478
    Dec 16, 2018 at 19:11
  • 3
    Please -- as ususal here -- make your code snippet compilable!
    – Mensch
    Dec 16, 2018 at 19:11
  • Ok, sanitized code and made difference more distinct - the text in expected output is 2px lower than on produced, thus appearing more "centered" to the viewer.
    – Jzuken
    Dec 16, 2018 at 19:23

3 Answers 3

1

Using m or b column types instead of the p type usually only makes a difference if the cell heights are larger than the minimum required height. For your sample table, actually, p, m, and b produce the exact same output.

I think that what you're looking for is the \extrarowheight length parameter. For the tabular environment at hand, executing \setlength\extrarowheight{2pt} seems to deliver roughly the right or desirable vertical centering effect. I say "roughly" deliberately, as what's optimal depends importantly on the words in the cells: are their lots of capital letters, and how many characters have ascenders and/or descenders? Visual formatting is as much an art as it is a craft...

enter image description here

\documentclass[a4paper]{article}
\usepackage{array} % 'multirow' package is not needed
\begin{document}
\begin{tabular}{| m{0.3in} | m{0.6in} | m{0.91in} |}
\hline
Lay & Page & Pages\\
\hline
1 & 2 & 5\\
\hline
\end{tabular}

\medskip
\setlength\extrarowheight{2pt}
\begin{tabular}{| p{0.3in} | p{0.6in} | p{0.91in} |}
\hline
Lay & Page & Pages\\
\hline
1 & 2 & 5\\
\hline
\end{tabular}
\end{document}
2
  • Is it possible to do that without increasing the row height?
    – Jzuken
    Dec 16, 2018 at 19:53
  • @Jzuken - Not that I know of. Sorry.
    – Mico
    Dec 16, 2018 at 20:04
1

Do not use \multirow if the text appears exactly between empty lines:

\documentclass[12pt,a4paper]{article}
\usepackage{array,ragged2e}
\let\MC\multicolumn
\newcolumntype{M}[1]{>{\Centering}m{#1}}
\begin{document}

\def\arraystretch{1.25}
\begin{tabular}{| m{0.36in} | m{0.25in} | m{0.2in} | m{0.3in} | m{0.4in} | m{0.39in} | M{0.2in} | 
M{0.2in} | M{0.2in} | M{0.6in} | M{0.91in} |}\hline
              & & & & & \MC{6}{c|}{}\\\cline{1-5}
              & & & & & \MC{6}{c|}{\large Work name}\\\cline{1-5}
              & & & & & \MC{6}{c|}{}\\\hline
  \MC{2}{|c|}{} & & & & & & Lit & & Page & Pages\\\cline{1-5}\cline{7-11}
  \MC{2}{|c|}{} & & & & & & &  & 1 & 5  \\\cline{1-5}\cline{7-11}
  \MC{2}{|c|}{} & & & & text & \MC{5}{c|}{} \\\cline{1-5}
  \MC{2}{|c|}{} & & & & & \MC{5}{c|}{\large IN-8192} \\\cline{1-5}
  \MC{2}{|c|}{} & & & & & \MC{5}{c|}{} \\\hline
\end{tabular}   
\end{document}

enter image description here

1

The cellspace package lets you define minimal vertical spacing at the top and bottom of cells in columns with specifier prefixed with the letter S(or C if you load siunitx):

\documentclass[a4paper]{article}

\usepackage{array}
\usepackage{multirow}
\usepackage{cellspace}
\setlength{\cellspacetoplimit}{4pt}
\begin{document}

\begin{tabular}{| m{0.2in} | m{0.6in} | m{0.91in} |}
\hline
Lit & Page & Pages\\
\hline
1 & 2 & 5\\
\hline
\end{tabular}
\bigskip

\begin{tabular}{| S{m{0.2in}} | S{m{0.6in}} | S{m{0.91in}} |}
\hline
Lit & Page & Pages\\
\hline
1 & 2 & 5\\
\hline
\end{tabular}

\end{document} 

enter image description here

2

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.