# 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