1

I'm trying to vertically center every row of the table, but I can't. How can I do it?

%\documentclass{article}
\documentclass[margin=10pt]{standalone}
\usepackage{array,booktabs,siunitx}
\newcolumntype{Q}[1]{>{\hfil$}p{#1}<{$\hfill}} % centered, automatic math mode
\usepackage[table]{xcolor} % for "\rowcolor" macro
\usepackage{amsmath}

\renewcommand{\rmdefault}{phv}
%\usepackage{mathpple}
%\usepackage{palatino}

\usepackage{lxfonts}
%
%   \usepackage{multicol}

\begin{document}
\centering
\setlength{\extrarowheight}{20pt} % for a more "open" look
\begin{tabular}{|*{5}{Q{2cm}|}} % or: {|*{b}{c|}}
\hline
\rowcolor{yellow} \textbf{$a$} & \textbf{$b$}& \textbf{$a+b$} & \textbf{$a-b$} \\
\hline
\dfrac{157}{3}   & \dfrac{14}{9}       &   \dfrac{485}{9} & \dfrac{457{9}  \\
\hline
\dfrac{704}{25}      & 3      &   \dfrac{779}{25} & \dfrac{625}{25}     \\
\hline
\end{tabular}
\end{document}
ryuk
  • 2,257
  • may be you are looking for th m comumn type instead of the p used in your \newcolumntype command – koleygr Dec 09 '17 at 19:46

2 Answers2

4

Here is a solution with the cellspace package, which allows to define a minimal vertical spacing at the top and bottom of cells in columns prefixed with the letter S, or C if you load siunitx.

\documentclass[margin=10pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage{array,booktabs,siunitx}%
\usepackage{cellspace}
\setlength\cellspacetoplimit{11pt}
\setlength\cellspacebottomlimit{10pt}
\newcolumntype{Q}[1]{>{\centering\arraybackslash$}C{p{#1}}<{$}}
\usepackage[table]{xcolor} % for "\rowcolor" macro
\usepackage{amsmath}

\renewcommand{\rmdefault}{phv}

\usepackage{lxfonts}

\begin{document}

\centering
%\setlength{\extrarowheight}{20pt} % for a more "open" look
\begin{tabular}{|*{5}{Q{2cm}|}|}% or: {|*{b}{c|}}
\hline
\rowcolor{yellow} \textbf{$a$} & \textbf{$b$}& \textbf{$a+b$} & \textbf{$a-b$} \\
\hline
\dfrac{157}{3} & \dfrac{14}{9} & 5 & 2 \\
\hline
\dfrac{704}{25} & 3 & \dfrac{17}{5} & \dfrac{1}{2} \\
\hline
\end{tabular}

\end{document} 

enter image description here

Bernard
  • 271,350
3

Here is a solution:

\documentclass[margin=10pt]{standalone}
\usepackage{array}
\newcolumntype{Q}[1]{>{\vfil\hfil$}m{#1}<{$\hfill\vfill}} % centered, automatic math mode
\usepackage[table]{xcolor} % for "\rowcolor" macro
\usepackage{amsmath}

%\renewcommand{\rmdefault}{phv}
%\usepackage{mathpple}
%\usepackage{palatino}

%\usepackage{lxfonts}
%   
%   \usepackage{multicol}

\begin{document}
\centering
\begingroup
%\setlength{\extrarowheight}{15pt} % for a more "open" look
\def\arraystretch{3}
\begin{tabular}{|*{4}{Q{2cm}|}} % or: {|*{b}{c|}}
\hline\rowcolor{yellow} \textbf{$a$} & \textbf{$b$}& \textbf{$a+b$} & \textbf{$a-b$} \\
\hline
\dfrac{157}{3}       & \dfrac{14}{9}       &   5      &     2      \\
\hline
\dfrac{704}{25}      & 3      &   \dfrac{17}{5}      &    \dfrac{1}{2}      \\
\hline
\end{tabular}
\endgroup
\end{document}

You can replace arraystretch with the above commented line.

Output:

enter image description here

koleygr
  • 20,105
  • 2
    It looks like the entries in cells are not vertically centered actually. They look a little bit flushed to the top. – Cragfelt Dec 09 '17 at 20:54
  • 1
    @Cragfelt I just gave the idea... the numbers have to be adjusted and sure there are better solutions... Just leaving the answer as an options and of course not the best one – koleygr Dec 09 '17 at 21:00