10

This question already has an answer here:

I have a table containing mathematics with sub and superscripts. These touch both the lines above and below in the table. I tried quite a few alternatives but have not been able to find anything that gives suitable spacing in the rows without also messing up the spacing of the title row (ie making the headings too tightly left aligned). Any pointers on the best approach for this would be appreciated.

Here is my current result (I am using the array package):

enter image description here

\begin{table}[htbp]
\begin{center}
    \begin{tabular}{| m{2.7cm} | m{8cm}|}
        \hline
        \textbf{Marginalised term} & \textbf{3 variable terms} \\
        \hline \hline 
        $H_\partial^{12}(\{12\})$ & 
        $\begin{aligned} 
            &H_\partial^{123}(\{12\}) + H_\partial^{123}(\{3\}\{12\}) \\
            &+ H_\partial^{123}(\{12\}\{13\}) + H_\partial^{123}(\{12\}\{23\}) \\ 
            &+ H_\partial^{123}(\{12\}\{13\}\{23\})  
            \end{aligned}$ \\
        \hline
        $H_\partial^{12}(\{1\})$ &  
        $H_\partial^{123}(\{1\})+H_\partial^{123}(\{1\}\{23\})+H_\partial^{123}(\{1\}\{3\})$ \\
        \hline
        $H_\partial^{12}(\{2\})$ &  
        $H_\partial^{123}(\{2\})+H_\partial^{123}(\{2\}\{13\})+H_\partial^{123}(\{2\}\{3\})$ \\
        \hline
        $H_\partial^{12}(\{1\}\{2\})$ &  
        $H_\partial^{123}(\{1\}\{2\})+H_\partial^{123}(\{1\}\{2\}\{3\})$ \\
        \hline
  \end{tabular}
\end{center}
\end{table}

Setting extrarowheight fixes the top spacing for the lower three rows, but they still all touch the lower hline and the first row touches both above and below.

Adding extrarowheight doesn't work:

enter image description here

Nor does arraystrectch (top row touches and vertical centering off):

enter image description here

marked as duplicate by Werner tables Feb 3 '17 at 22:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • you could remove the lines but if you really want them add renewcommand\arraystretch{2} (or whatever value you want) either in the document preamble or if just this table, at the start of the table environment – David Carlisle Feb 3 '17 at 11:57
  • Thanks. I added a comment - I have tried that but it doesn't fix the problem. It has space above the superscripts on the single line rows, but the first row still touches at the top and all rows still have subscript touching below. I added a screenshot of this. – robince Feb 3 '17 at 11:59
  • note I'd edited my comment to arraystretch rather than extrarowheight:-) – David Carlisle Feb 3 '17 at 12:00
  • Thanks! arraystretch fixes the single line rows but the the multiline first row still touches above and below. The single line rows look a bit odd too because they are not vertically centered. – robince Feb 3 '17 at 12:02
8

You can add some vertical padding to the cells, either with cellspace, which defines a minimal white space at the top and bottom of cells in columns with specifier prefixed with the letter S, or with makecell, which adds a fixed vertical spacing at the top and bottom of all cells, with the \makegapedcells command.

Note you do not have to set the columns in paragraph mode: the second code sets column widths to their natural widths.

    \documentclass{article}
    \usepackage{amsmath, array, makecell}
\usepackage{cellspace} %
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}


\begin{document}

\begin{table}[htbp]
\centering
    \begin{tabular}{| S{m{2.7cm}} | Sl|}%{m{8cm}}
        \hline
        \textbf{Marginalised term} & \textbf{3 variable terms} \\
        \hline \hline
        $H_\partial^{12}(\{12\})$ &
        $\begin{aligned}
            &H_\partial^{123}(\{12\}) + H_\partial^{123}(\{3\}\{12\}) \\
            &+ H_\partial^{123}(\{12\}\{13\}) + H_\partial^{123}(\{12\}\{23\}) \\
            &+ H_\partial^{123}(\{12\}\{13\}\{23\})
            \end{aligned}$ \\
        \hline
        $H_\partial^{12}(\{1\})$ &
        $H_\partial^{123}(\{1\})+H_\partial^{123}(\{1\}\{23\})+H_\partial^{123}(\{1\}\{3\})$ \\
        \hline
        $H_\partial^{12}(\{2\})$ &
        $H_\partial^{123}(\{2\})+H_\partial^{123}(\{2\}\{13\})+H_\partial^{123}(\{2\}\{3\})$ \\
        \hline
        $H_\partial^{12}(\{1\}\{2\})$ &
        $H_\partial^{123}(\{1\}\{2\})+H_\partial^{123}(\{1\}\{2\}\{3\})$ \\
        \hline
  \end{tabular}
\end{table}

\begin{table}[htbp]
\centering\setcellgapes{4pt}\makegapedcells \renewcommand\theadfont{\normalsize\bfseries}%
%
 \begin{tabular}{|l| l|}%{m{8cm}}
 \hline
 \thead{Marginalised\\ term} & \thead{3 variable terms} \\
 \hline \hline
 $H_\partial^{12}(\{12\})$ &
 $\begin{aligned}
 &H_\partial^{123}(\{12\}) + H_\partial^{123}(\{3\}\{12\}) \\
 &+ H_\partial^{123}(\{12\}\{13\}) + H_\partial^{123}(\{12\}\{23\}) \\
 &+ H_\partial^{123}(\{12\}\{13\}\{23\})
 \end{aligned}$ \\
 \hline
 $H_\partial^{12}(\{1\})$ &
 $H_\partial^{123}(\{1\})+H_\partial^{123}(\{1\}\{23\})+H_\partial^{123}(\{1\}\{3\})$ \\
 \hline
 $H_\partial^{12}(\{2\})$ &
 $H_\partial^{123}(\{2\})+H_\partial^{123}(\{2\}\{13\})+H_\partial^{123}(\{2\}\{3\})$ \\
 \hline
 $H_\partial^{12}(\{1\}\{2\})$ &
 $H_\partial^{123}(\{1\}\{2\})+H_\partial^{123}(\{1\}\{2\}\{3\})$ \\
 \hline
 \end{tabular}
\end{table}

    \end{document} 

enter image description here

7

There is probably a better way, but here I use a combination of struts. I added \upstrut to the top of the aligned stack, \downstrut to the bottom of the aligned stack, and \mystrut to the simple lines.

\documentclass{article}
\usepackage{tabularx,amsmath}
\newcommand\upstrut{\rule{0pt}{12pt}}
\newcommand\downstrut{\rule[-6pt]{0pt}{6pt}}
\newcommand\mystrut{\upstrut\downstrut}
\begin{document}
\begin{table}[htbp]
\begin{center}
    \begin{tabular}{| m{2.7cm} | m{8cm}|}
        \hline
        \textbf{Marginalised term} & \textbf{3 variable terms} \\
        \hline \hline 
        $H_\partial^{12}(\{12\})$ & 
        $\begin{aligned} 
            &\upstrut H_\partial^{123}(\{12\}) + H_\partial^{123}(\{3\}\{12\}) \\
            &+ H_\partial^{123}(\{12\}\{13\}) + H_\partial^{123}(\{12\}\{23\}) \\ 
            &+ H_\partial^{123}(\{12\}\{13\}\{23\}) \downstrut 
            \end{aligned}$ \\
        \hline
        $\mystrut H_\partial^{12}(\{1\})$ &  
        $H_\partial^{123}(\{1\})+H_\partial^{123}(\{1\}\{23\})+H_\partial^{123}(\{1\}\{3\})$ \\
        \hline
        $\mystrut H_\partial^{12}(\{2\})$ &  
        $H_\partial^{123}(\{2\})+H_\partial^{123}(\{2\}\{13\})+H_\partial^{123}(\{2\}\{3\})$ \\
        \hline
        $\mystrut H_\partial^{12}(\{1\}\{2\})$ &  
        $H_\partial^{123}(\{1\}\{2\})+H_\partial^{123}(\{1\}\{2\}\{3\})$ \\
        \hline
  \end{tabular}
\end{center}
\end{table}
\end{document}

enter image description here

Alternatively, one could add a \stackgap to a term in each row, which ads a (default) 3pt buffer above and below the term. An optional argument can change the stack gap value:

\documentclass{article}
\usepackage{tabularx,amsmath,stackengine}
\begin{document}
\begin{table}[htbp]
\begin{center}
    \begin{tabular}{| m{2.7cm} | m{8cm}|}
        \hline
        \textbf{Marginalised term} & \textbf{3 variable terms} \\
        \hline \hline 
        $H_\partial^{12}(\{12\})$ & 
        \addstackgap{$\begin{aligned} 
            &H_\partial^{123}(\{12\}) + H_\partial^{123}(\{3\}\{12\}) \\
            &+ H_\partial^{123}(\{12\}\{13\}) + H_\partial^{123}(\{12\}\{23\}) \\ 
            &+ H_\partial^{123}(\{12\}\{13\}\{23\}) 
            \end{aligned}$} \\
        \hline
        \addstackgap{$H_\partial^{12}(\{1\})$} &  
        $H_\partial^{123}(\{1\})+H_\partial^{123}(\{1\}\{23\})+H_\partial^{123}(\{1\}\{3\})$ \\
        \hline
        \addstackgap{$H_\partial^{12}(\{2\})$} &  
        $H_\partial^{123}(\{2\})+H_\partial^{123}(\{2\}\{13\})+H_\partial^{123}(\{2\}\{3\})$ \\
        \hline
        \addstackgap{$H_\partial^{12}(\{1\}\{2\})$} &  
        $H_\partial^{123}(\{1\}\{2\})+H_\partial^{123}(\{1\}\{2\}\{3\})$ \\
        \hline
  \end{tabular}
\end{center}
\end{table}
\end{document}

enter image description here

3

The most appealing way to typeset this table is without any vertical lines and far fewer, but well-spaced, horizontal lines. That way, the issue of subscript and superscript terms touching the horizontal lines never arises to begin with. And, no need to monkey around with \arraystretch.

Since almost all of the table's material is math-y stuff, I suggest usin an array environment instead of a tabular environment. If nothing else, you can save yourself having to type a lot of $ symbols.

Separately, since H_\partial occurs about 17 times in the table, I suggest you replace all instances with \Hp, where \Hp is defined in the preamble as, you guessed it, H_\partial.

enter image description here

\documentclass{article}
\usepackage{amsmath,booktabs,array}
\newcommand{\Hp}{H_\partial} % shortcut macro
\begin{document}
\begin{table}[htbp]
\centering

$\begin{array}{@{}ll@{}}
    \toprule
    \multicolumn{1}{@{}m{2.7cm}}{\textbf{Marginalised term}} & 
    \textbf{3 variable terms} \\
    \midrule
    \Hp^{12}(\{12\}) & 
    \!\begin{aligned}[t]
      &\Hp^{123}(\{12\}) + \Hp^{123}(\{3\}\{12\}) \\
      &\quad+ \Hp^{123}(\{12\}\{13\}) + \Hp^{123}(\{12\}\{23\}) \\ 
      &\quad+ \Hp^{123}(\{12\}\{13\}\{23\})  
    \end{aligned} \\
    \addlinespace
    \Hp^{12}(\{1\}) &  
    \Hp^{123}(\{1\})+\Hp^{123}(\{1\}\{23\})+\Hp^{123}(\{1\}\{3\}) \\
    \addlinespace
    \Hp^{12}(\{2\}) &  
    \Hp^{123}(\{2\})+\Hp^{123}(\{2\}\{13\})+\Hp^{123}(\{2\}\{3\}) \\
    \addlinespace
    \Hp^{12}(\{1\}\{2\}) &  
    \Hp^{123}(\{1\}\{2\})+\Hp^{123}(\{1\}\{2\}\{3\}) \\
    \bottomrule
\end{array}$

\end{table}
\end{document}

Not the answer you're looking for? Browse other questions tagged or ask your own question.