Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How to vertically center \begin{equation}...\end{equation} in a table cell?

alt text

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
\begin{equation}
  \sin x \cos x = y
\end{equation}
& \hrulefill \tabularnewline\hline
\end{longtable}
\end{document}
share|improve this question
    
I'm repeating myself, please don't make your code so compact that it becomes obscure. –  Hendrik Vogt Jan 7 '11 at 14:37

3 Answers 3

up vote 2 down vote accepted

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
\begin{equation}
  \sin x \cos x = y
\end{equation}
\strut
& \hrulefill \tabularnewline\hline
\end{longtable}
\end{document}

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

share|improve this answer
    
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
\begin{equation}
  \sin x \cos x = y
\end{equation}
& foo \hrulefill \tabularnewline\hline
\end{longtable}
\end{document}
share|improve this answer
    
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.

share|improve this answer
    
@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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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