1

In this code, I would like to vertically center the text in the left cells, x = k and x > k. I tried with \parbox[c]:

\documentclass[border=2mm,tikz]{standalone}
\usepackage{tabularx}

\begin{document}

\begin{table}
\renewcommand{\arraystretch}{3}
\begin{tabularx}{0.5\textwidth}{ | X | c | }
\hline
\noindent\parbox[c]{\hsize}{$x = k$} & $a = \displaystyle \frac{1}{2}$\\
& $b = \displaystyle \frac{1}{4}$\\[0.4cm]
\hline
\noindent\parbox[c]{\hsize}{$x > k$} & $c = \displaystyle \frac{1}{8}$\\
 & $d = \displaystyle \frac{1}{16}$\\[0.4cm]
\hline
\end{tabularx}
\end{table}

\end{document}

However, this is the result:

enter image description here

What is wrong? How to vertically center the contents of those cells?

(The use of \parbox is not necessary, I just thought it was a solution).

2 Answers 2

1

If you want the left column to be vertically aligned including the added 0.4cm to the row height, then:

\documentclass[border=2mm,tikz]{standalone}
\usepackage{tabularx, multirow, adjustbox}

\begin{document}

\begin{table}
    \renewcommand{\arraystretch}{3}
    \begin{tabularx}{0.5\textwidth}{ | X | c | }
        \hline
        \multirow{2}{*}{\adjustbox{vspace=-0.2cm}{\centerline{$x = k$}}} & $a = \displaystyle \frac{1}{2}$  \\
                                                                         & $b = \displaystyle \frac{1}{4}$  \\[0.4cm]
        \hline
        \multirow{2}{*}{\adjustbox{vspace=-0.2cm}{\centerline{$x > k$}}} & $c = \displaystyle \frac{1}{8}$  \\
                                                                         & $d = \displaystyle \frac{1}{16}$ \\[0.4cm]
        \hline
    \end{tabularx}
\end{table}

\end{document}

If the 0.4cm is removed, then you can simply remove the \adjustbox{vspace-0.2cm}{} command. Also, the measurement can be adjusted if you change the value of the added row height.

4
  • Yes, I would like to keep the 0.4cm added. This is actually far better than mine. However, x = k and x > k are still slightly below the vertical center. It seems that vspace=-0.2cm is needed. Do you have any guesses for the reason?
    – BowPark
    Jan 18, 2021 at 17:00
  • This is what I get with your code. It's below the vertical center.
    – BowPark
    Jan 18, 2021 at 17:02
  • 1
    @BowPark Updated the adjustbox values. Your assertion is correct, it should be equal to half the added row height to be in the new middle. Jan 18, 2021 at 17:09
  • 1
    Ok! Thanks for your help!
    – BowPark
    Jan 18, 2021 at 17:14
1

Here is an alternative without multirow but with nested tabulars and makecell:

enter image description here

\documentclass{article}
\usepackage{tabularx}
\usepackage{makecell}
\setcellgapes{0.25cm}

\begin{document}

\begin{table}
\makegapedcells
    \begin{tabularx}{0.5\textwidth}{ | >{\centering\arraybackslash}X | c | }
        \hline
        $x = k$ & \begin{tabular}{>{\(\displaystyle}c<{\)}} a = \frac{1}{2} \\ b = \frac{1}{4} \end{tabular}\\
        \hline
        $x > k$ & \begin{tabular}{>{\(\displaystyle}c<{\)}} c = \frac{1}{8} \\ d = \frac{1}{16} \end{tabular}\\
        \hline
    \end{tabularx}
\end{table}

\end{document}

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.