2

I want to remove vertical space before and after math and other environments like description, so they look tight and with even space at the top and bottom like Row 1.

enter image description here

As you can see, I already test commands like \belowdisplayskip without success.

MWE:

\documentclass{memoir}

\usepackage{amsmath}
\usepackage{enumitem}

\begin{document}

% Not works.
%\setlength{\belowdisplayskip}{0pt}
%\setlength{\abovedisplayskip}{0pt}
%\setlength{\abovedisplayshortskip}{0pt}
%\setlength{\belowdisplayshortskip}{0pt}    

\begin{tabular}{m{3cm} m{3cm}}
    \hline
    Row 1 & Text \\ \hline
    Row 2 &
        \begin{description}[topsep=0pt]
            \item[$\alpha$] Alpha
            \item[$\beta$] Beta
        \end{description} \\ \hline
    Row 3 &
        \begin{equation*}
            E = mc^2
        \end{equation*} \\ \hline
    Row 4 &
        \begin{equation*}
            \sum_i x_i
        \end{equation*}  \\ \hline
    Row 5 &
        {\begin{align*}
              y &= ax^2 + bx \\
              &= x(ax + b)
        \end{align*}} \\ \hline
\end{tabular}

\end{document}
3
  • These environments will certainly add the space like this including equation* and center. From the academic perspective, just use the inline mode like $E=mc^2$. Feb 13, 2022 at 2:29
  • Thanks but I also use environments like align, so I still need a solution. Feb 13, 2022 at 4:00
  • You can use $\begin{aligned} ... \end{aligned}$ which will not treat the math like a display. (See the amsmath user guide, texdoc amsldoc.) Mar 12, 2022 at 4:07

3 Answers 3

6
  • For math equations you can use any of amsmath math environments: aligned, gathered and split

  • With tabular the result is not very nice:

enter image description here

\documentclass{memoir}
\usepackage{amsmath}

\begin{document}
    \begin{table}[htb]
    \centering
\renewcommand\arraystretch{1.5}
\begin{tabular}{|m{3cm}| m{5cm}|}
    \hline
    Row 1 &     Text            \\ \hline
    Row 2 &     Text            \\ \hline
    Row 3 & $E = mc^2$          \\ \hline
    Row 4 & $\sum_i x_i$        \\ \hline
    Row 4 & $\begin{aligned}
             \sin(x) & = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + \cdots \\
                     & = \sum_{n=0}^{\infty} \frac{(-1)^n}{(2n+1)!} x^{2n+1}\\[1ex]
             \end{aligned}$     \\ \hline
\end{tabular}
    \end{table}
\end{document}

With use tabularray the result is much better:

enter image description here

\documentclass{memoir}
\usepackage{tabularray}
\UseTblrLibrary{amsmath}


\begin{document}
    
\begin{table}[htb]    
    \centering
\begin{tblr}{hlines, vlines,
             colspec = {Q[l,m, 3cm]  Q[l,m, mode=math]},
             row{1,2}= {mode=text},
             rowsep=3pt
             }
    Row 1 &     Text            \\  
    Row 2 &     Text            \\  
    Row 3 & E = mc^2            \\  
    Row 4 & \sum_i x_i          \\  
    Row 4 & \begin{aligned}
             \sin(x) & = x - \frac{x^3}{3!} \frac{x^5}{5!} - \frac{x^7}{7!} + \cdots \\
                     & = \sum_{n=0}^{\infty} \frac{(-1)^n}{(2n+1)!} x^{2n+1}\\[1ex]
             \end{aligned}     \\  
\end{tblr}
    \end{table}
\end{document}

In the second example is assumed that in the second column are with exception it the first two rows, equation. Therefore this column is in math mode.

If you like to have for some reasons contents in the second column centered, than Q[l,m, mode=math] replace with Q[c,m, mode=math]. This change will gives:

enter image description here

5
  • Just a minor detail. Shouldn't the first column be Q type as well? A X column makes no sense with a specified width.
    – marv
    Feb 13, 2022 at 7:14
  • @marv, you are right. Corrected now.
    – Zarko
    Feb 13, 2022 at 7:16
  • I was wondering why it still worked. The package author stated in this issue that an X column will be ignored, if a wd is specified.
    – marv
    Feb 13, 2022 at 7:20
  • @marv My bad. I should have said that co option would be ignored, but not the column itself.
    – L.J.R.
    Feb 13, 2022 at 7:43
  • @Zarko Ok, maybe center environment wasn't a good choice for what I require, so I edited my question with other more appropriate evironment. I like the tabularray solution but it seems not correct the problem at all in aligned-like envs., there's more espace below than at the top, I don't know the reason but since I use those envs. a lot I can't accept the answer yet. Feb 13, 2022 at 17:32
4

You can try another flavor using nicematrix. Does not add any space to the natural height of the cells. (first table).

Usually one wants to expand cells to avoid collision of the rules with subscripts and superscripts.

By using cell-space-limits= you can add some space above and below the content (1pt, second table).

Finally it is possible to improve only the superscripts using cell-space-top-limit=1pt (last table).

Default

d

Better

e

f

This is the complete code.

\documentclass{memoir}

\usepackage{nicematrix}

\begin{document}
    
\begin{table}[htb] 
    \centering
    \caption{Default: tight}  
        \begin{NiceTabular}{lc}[hvlines]
            Row 1   &     Text            \\  
            Row 2   &     Text            \\  
            Row 3   & $E = mc^2$          \\  
            Row 4   & $\sum_i x_i$         \\  
            Row 5   & $\begin{aligned}
                            \sin(x) & = x - \frac{x^3}{3!} \frac{x^5}{5!} - \frac{x^7}{7!} + \cdots \\
                                    & = \sum_{n=0}^{\infty} \frac{(-1)^n}{(2n+1)!} x^{2n+1}\\
                        \end{aligned} $    \\  
    \end{NiceTabular}
\end{table}
\begin{table}[htb] 
    \centering
    \caption{More relaxed (added 1pt)}      
    \begin{NiceTabular}{lc}[hvlines,  cell-space-limits=1pt] % expand upper and lower space
    Row 1   &     Text            \\  
        Row 2   &     Text            \\  
        Row 3   & $E = mc^2$          \\  
        Row 4   & $\sum_i x_i$         \\  
        Row 5   & $\begin{aligned}
                        \sin(x) & = x - \frac{x^3}{3!} \frac{x^5}{5!} - \frac{x^7}{7!} + \cdots \\
                                & = \sum_{n=0}^{\infty} \frac{(-1)^n}{(2n+1)!} x^{2n+1}\\
        \end{aligned} $    \\  
    \end{NiceTabular}
\end{table}

\begin{table}[htb] 
    \centering
    \caption{Semi relaxed (added above 1pt)}    
    \begin{NiceTabular}{lc}[hvlines,   cell-space-top-limit=1pt] % expand only lower space
        Row 1   &     Text            \\  
        Row 2   &     Text            \\  
        Row 3   & $E = mc^2$          \\  
        Row 4   & $\sum_i x_i$         \\  
        Row 5   & $\begin{aligned}
            \sin(x) & = x - \frac{x^3}{3!} \frac{x^5}{5!} - \frac{x^7}{7!} + \cdots \\
            & = \sum_{n=0}^{\infty} \frac{(-1)^n}{(2n+1)!} x^{2n+1}\\
        \end{aligned} $    \\  
    \end{NiceTabular}
\end{table}

\end{document}
1
  • Thanks a lot!, I'll try it. Feb 14, 2022 at 3:44
1
  1. With the package nicematrix. If you want a environment equation, i think it's necessary to use minipage

  2. with the code of Simon Dispa

         Row 4 & \begin{minipage}{5cm}\begin{equation*}
                 \sum_i x_i
             \end{equation*} \end{minipage}\\
    

enter image description here

You must log in to answer this question.

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