# Sub and super scripts touching lines in table [duplicate]

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):

\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:

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

## marked as duplicate by Werner tables StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Feb 3 '17 at 22:22

• 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

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]
%
\begin{tabular}{|l| l|}%{m{8cm}}
\hline
\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}


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}


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}


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. \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\}) \\
\end{aligned} \\
\Hp^{12}(\{1\}) &
\Hp^{123}(\{1\})+\Hp^{123}(\{1\}\{23\})+\Hp^{123}(\{1\}\{3\}) \\