# How to vertically center $$…$$ in a table cell?

How to vertically center $$...$$ in a table cell?

## Minimal Code (I cannot minimize anymore)

\documentclass{article}
\usepackage{array,longtable,calc,amsmath}

\newcolumntype{A}[1]{%
>{\begin{minipage}{#1\linewidth-2\tabcolsep-1.5\arrayrulewidth}\vspace{\tabcolsep}}%
c%
<{\vspace{\tabcolsep}\end{minipage}}%
}

\begin{document}
\begin{longtable}{|*2{A{0.5}|}}
\hline
$$\sin x \cos x = y$$
& \hrulefill \tabularnewline\hline
\end{longtable}
\end{document}

-
I'm repeating myself, please don't make your code so compact that it becomes obscure. – Hendrik Vogt Jan 7 '11 at 14:37

To get correct spacing, put a \strut before and after the display, and give the two shortdisplayskips the same value.

\documentclass{article}
\usepackage{array,longtable,calc,amsmath}

\tabcolsep=1pt
\arrayrulewidth=1pt

\newcolumntype{A}[1]{%
>{\begin{minipage}{#1\linewidth-2\tabcolsep-1.5\arrayrulewidth}\vspace{\tabcolsep}}%
c%
<{\vspace{\tabcolsep}\end{minipage}}%
}

\begin{document}
\begin{longtable}{|*2{A{0.5}|}}
\hline
\setlength\abovedisplayshortskip{0pt}
\setlength\belowdisplayshortskip{0pt}
\strut
$$\sin x \cos x = y$$
\strut
& \hrulefill \tabularnewline\hline
\end{longtable}
\end{document}


You can change the shortdisplayskips according to your needs; negative values are possible, too.

-
Your solution looks like a magic. – xport Jan 7 '11 at 14:57
@xport: See my updated answer; this is a lot clearer, I think. – Hendrik Vogt Jan 8 '11 at 9:57
I just checked it and it really makes the displayed equation get vertically centered. Thanks. – xport Aug 10 '11 at 12:24
\documentclass{article}
\usepackage{array,calc,longtable,ragged2e}

\tabcolsep=1pt
\arrayrulewidth=1pt

\newcolumntype{A}[1]{>{%
\abovedisplayskip=0pt\belowdisplayskip=0pt
\Centering}m{#1\linewidth-2\tabcolsep-1.5\arrayrulewidth}}

\begin{document}
\begin{longtable}{|*2{A{0.5}|}}\hline
$$\sin x \cos x = y$$
& foo \hrulefill \tabularnewline\hline
\end{longtable}
\end{document}

-
thank you very much for this. I am attempting to make use of your setting on my original setting (using minipage). – xport Jan 7 '11 at 14:53
I just realized that your answer does not exactly center the displayed equation vertically. But Hendrik's answer does. Sorry. :-) – xport Aug 10 '11 at 12:23

Why don't you use \begin{longtable}{|*2{m{0.5\linewidth}|}}? I cannot see the advantage of column type A here.

-
@Leo, You should not make a question in answer form. :-) – xport Jan 7 '11 at 15:52
@xsport: That was not a real question, it was more an answer and it is correct. – Herbert Jan 7 '11 at 16:13
@xport: this is a valid answer to the problem (although I can’t judge whether it’s correct …). – Konrad Rudolph Jan 7 '11 at 17:09
@xport: If you look into the answer, you'll find that @Herbert's answer is nearly the same as this one. The key to this question is to use m column type. And the calculation you used seems too complex, so I just skipped them. – Leo Liu Jan 8 '11 at 2:15
@Leo: Do you notice that the displayed equations in m{} will not be vertically centered? – xport Jan 12 '11 at 5:37