# Creating additional space in tabular for \dfrac, while vertically centering multirow and other cells

\documentclass{article}
\usepackage[a4paper, portrait, margin=0.5in]{geometry}
\usepackage[table]{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{amsmath}
\usepackage{multirow}

\begin{document}
\large

\renewcommand{\arraystretch}{1.5}
\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{M}[1]{>{\raggedright\arraybackslash}m{#1}}
\begin{center}
\begin{tabular}{ | M{1.5cm} | M{9.5cm} | M{1cm} | M{4.5cm} |}
\hline
\centering \multirow{6}{*}{1} &
$f(3) = 100$
\par \medskip
$a=\dfrac{50}{21}-\dfrac{13}{42}b$ & \hfill \textbf{A} & B
\tabularnewline
\cline{2-4}
& $3x^2-x-4=(3x-4)(x+1)$ & \hfill \textbf{C} & D
\tabularnewline
\cline{2-4}
& \vspace{0.15cm} $f(-1) = 0$ or $f(\dfrac{4}{3}) = 0$
\par \medskip
$b=-\dfrac{2}{3}a$ & \hfill \textbf{E} & F
\tabularnewline
\cline{2-4}
& $a=3, \; b=-2$ & \hfill \textbf{G} & H
\tabularnewline
\hline
\end{tabular}
\end{center}

\end{document}


This code gives the following output:

The bottom \dfrac at rows AB and EF are getting cut off by the \cline. Is there a way to create additional space for the fractions while maintaining vertical centering of the other cells in the row? (The cells with the \dfrac should also be vertically centered)

The vertical centering of the multirow column (labelled "1") is passable but I am a perfectionist. Can anyone also help to ensure vertical centering of the multirow column?

• Welcome to TeX!SE- Try to use tabularray package or ellspace for additional vertical spaces. Commented Oct 30, 2022 at 17:14
• I apologise that tables in latex are not my forte (in fact my worst topic in latex), but do you mind showing me an example (possibly using my above table)? I tried to make use of the package but I have encountered too many problems with it and its documentation. (e.g. Setting fixed lengths would not work somehow , Ensuring the text would naturally wrap if it exceeds the defined cell width) Commented Oct 30, 2022 at 18:19

Like this?

%\documentclass{article}
%\usepackage[a4paper, portrait, margin=0.5in]{geometry}
\documentclass[margin=3mm]{standalone}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{amsmath}

\begin{document}
\begin{tblr}{hlines, vlines,
colspec={Q[c,m, wd=15mm] Q[l,m, wd=95mm, mode=dmath]
Q[r,m, wd=10mm, font=\bfseries] Q[l, m, wd=45mm]},
rowsep=7pt,
}
\SetCell[r=4]{c}    1
&   \begin{aligned}
f(3) & = 100  \\
a    & =\dfrac{50}{21} - \dfrac{13}{42}b
\end{aligned}   &   A   &   B       \\
&   3x^2-x-4=(3x-4)(x+1)
&   C   &   D       \\
&   \begin{aligned}
f(-1) & = 0 \text{ or } f\biggl(\dfrac{4}{3}\biggr) = 0  \\
b & = -\dfrac{2}{3}a
\end{aligned}   &   E   &   F       \\
&   a=3,  b=-2      &   G   &   H       \\
\end{tblr}
\end{document}


The simplest way to left align all equations in cells (as you request in comment below) and preserve simple table code and desired vertical spaces around cells contents, is use a array environment. For example:

\begin{array}{@{}l}
f(3)  = 100 \\
a     =\dfrac{50}{21} - \dfrac{13}{42}b
\end{array}


For shorter writing it may be handy to define new environmentČ

\newenvironment{la}%
{
\begin{array}{@{} l}
}
{
\end{array}
}


The MWE, which contain both ways of arrays writing is:

%\documentclass{article}
%\usepackage[a4paper, portrait, margin=0.5in]{geometry}
\documentclass[margin=3mm]{standalone}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{amsmath}
\newenvironment{la}%
{
\begin{array}{@{} l}
}
{
\end{array}
}

\begin{document}
\begin{tblr}{hlines, vlines,
colspec={Q[c,m, wd=15mm] Q[l,m, wd=95mm, mode=dmath]
Q[r,m, wd=10mm, font=\bfseries] Q[l, m, wd=45mm]},
row{1} = {font=\bfseries, mode=text},
row{2-Z} ={rowsep=7pt},
}
no. & equations         & right & left      \\
\SetCell[r=4]{c}    1
&   \begin{array}{@{} l}
f(3) = 100  \\
a    = \dfrac{50}{21} - \dfrac{13}{42}b
\end{array}     &   A   &   B       \\
&   3x^2-x-4=(3x-4)(x+1)
&   C   &   D       \\
&   \begin{la}
b = -\dfrac{2}{3}a\hfill
\end{la}        &   E   &   F       \\
&   a=3,  b=-2      &   G   &   H       \\
\end{tblr}
\end{document}


• What if I do not want to use \begin{aligned} as I want each math line to start at the left side of the cell? I've played around with your code by removing \begin{aligned} and the letters are no longer vertically centered. Commented Oct 31, 2022 at 7:15
• It works now, but may I confirm if there are any updates on a more elegant solution? Commented Oct 31, 2022 at 17:12
• it will also work with array. See addendum 2. Commented Oct 31, 2022 at 18:54
• I tried adding headings to the table, the headings were not vertically centered. I also tried to use tikzpicture and polylongdiv in the math column of the table - same problem too. Any suggestions on fixing this? Commented Nov 2, 2022 at 9:32

The package nicematrix helps to solve several issues, with a simpler code.

The environment NiceTabularX allows to keep the table within the text area. \begin{NiceTabularX}{<total width>}{ X ....

\Block{<nro of rows - nro of columns>}{<content>} will center the content in the allocated space (e.g a 3x1 cell in this example) and allows to use \\ inside.

hvlines will draw all lines.

\documentclass{article}
\usepackage[a4paper, portrait, margin=0.5in, showframe]{geometry}% added showframe <<<<<<<<<<<<<
\usepackage[table]{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{amsmath}
\usepackage{multirow}

\begin{document}
\large

\renewcommand{\arraystretch}{1.5}

\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}} % centered column
\newcolumntype{B}[1]{>{\bfseries\centering\arraybackslash}m{#1}} % bold column

\newcolumntype{M}[1]{>{\raggedright\arraybackslash}m{#1}}

\noindent\begin{NiceTabularX}{\linewidth}{X C{9.5cm} B{1cm} C{4.5cm} }[hvlines]
\Block{8-1}{1}  &\Block{3-1}{\begin{aligned} f(3) & = 100 \\ a &=\dfrac{50}{21}-\dfrac{13}{42}b \end{aligned} }                                                        &\Block{3-1}{A} &\Block{3-1}{B} \\
&                                                                                   &               &       \\
&                                                                                   &               &       \\
& $3x^2-x-4=(3x-4)(x+1)$                                                            &C              & D     \\
&\Block{3-1}{\begin{aligned} f(-1) & = 0 \text{ or } f\biggl(\dfrac{4}{3}\biggr) = 0\\ b & = -\dfrac{2}{3}a \end{aligned}}                                                                     &\Block{3-1}{E} & \Block{3-1}{F} \\
&                                                                                   &               &       \\
&                                                                                   &               &       \\
& $a=3, \; b=-2$                                                                    &G              & H     \\
\end{NiceTabularX}

\end{document}

• What if I do not want to use \begin{aligned} as I want each math line to start at the left side of the cell? Commented Oct 31, 2022 at 7:17

Here is another way to do this using nicematrix. You must compile twice.

• \NiceMatrixOptions{cell-space-limits = 2pt} will increase spacing to accommodate your big fractions.
• column types wc, wl and wr allow you to set the column width.
• Option [hvlines] for horizontal and vertical lines (but respect the blocks).
• To center the 1 in the first column, use \Block{4-1}{1} in cell 1-1. {4-1} indicates a block of 4 rows and 1 column.
• \Block[l]{}{line1\\line2} permits multiline cells. This is known as a "mono-cell" block. To increase vertical spacing, I used \\[1ex].

\documentclass{article}
\usepackage[a4paper, margin=0.5in]{geometry}

\usepackage{nicematrix}

\begin{document}
\large
\renewcommand{\arraystretch}{1.5}

\begin{center}
\NiceMatrixOptions{cell-space-limits = 2pt}
\begin{NiceTabular}{wc{1.5cm}wl{9.5cm}wr{1cm}wl{4.5cm}}[hvlines]
\Block{4-1}{1} & \Block[l]{}{$f(3)=100$\\[1ex]$a=\dfrac{50}{21}-\dfrac{13}{42}b$} & \textbf{A} & B \\
& $3x^2-x-4=(3x-4)(x+1)$ & \textbf{C} & D \\
& \Block[l]{}{$f(-1) = 0$ or $f(\dfrac{4}{3}) = 0$\\[1ex]$b=-\dfrac{2}{3}a$} & \textbf{E} & F \\
& $a=3, \; b=-2$ & \textbf{G} & H
\end{NiceTabular}
\end{center}

\end{document}