Graphics, Figures & TablesVertical Alignment of Equations within a Table

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Phredd
Posts: 1
Joined: Mon Feb 15, 2016 2:43 pm

Vertical Alignment of Equations within a Table

Post by Phredd »

I am writing up a university project, and I want to create a table with the left column containing Feynman diagrams (which I have drawn using the feynmp package) and the right column containing equations. When I do this, however, the equations are vertically aligned in the bottom of the cell, and I can’t figure out how to change this, so that they align with the centre of the diagram in the same row.

Here is some sample code which I hope demonstrates the problem without writing out my whole report.

Code: Select all

\begin{table*}[t]
\centering
\begin{tabular}{|c|c|}
\hline
\textbf{feynmp diagram goes here}
&
$\begin{aligned} I &= \\ &\frac{D}{4} \end{aligned}$
\\
\hline
\end{tabular}
\end{table*}
Last edited by Stefan Kottwitz on Mon Feb 15, 2016 5:33 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Vertical Alignment of Equations within a Table

Post by Stefan Kottwitz »

Hi Phredd,

welcome to the forum!

Sure, there's no need to post your whole report. But there's something between a small code snippet and the whole report, right? ;) If you would post a compilable example, a reader could test and improve it in the desired way. Without compiling, it stays a bit theoretical.

So, without the Feynman diagram (it would have been nice to see it), I guess it just has the base line for vertical alignment at the bottom. The base line for the aligned math environment is at it's center (but can be changed to top or bottom line). In the table, the base lines are aligned next to each other. So you see the diagram going upwards above the base line, the math text at the base line stays low. It's base line alignment, not top alignment.

You can change it in different ways, such as using \raisebox, the adjustbox package, or the stackengine package.

With stackengine loaded, you can use \belowbaseline[0pt]{your diagram} and \belowbaseline[0pt]{$math formula$} for top alignment. Or \Centerstack for middle alignment.

In contrast to m columns, you don't need a fixed width.
centered.png
centered.png (905 Bytes) Viewed 7228 times
Done by:

Code: Select all

\documentclass{article} 
\usepackage[demo]{graphicx}
\usepackage{amsmath}
\usepackage{stackengine}
\begin{document}
\begin{table*}[t]
\centering
\begin{tabular}{|c|c|}
  \hline
  \Centerstack{\includegraphics{diagram}}
    &
  $\begin{aligned} I &= \\
  &\frac{D}{4} \end{aligned}$\\
\hline
\end{tabular}
\end{table*}
\end{document}
Here with top alignment:
mwe.png
mwe.png (948 Bytes) Viewed 7228 times

Code: Select all

\documentclass{article} 
\usepackage[demo]{graphicx}
\usepackage{amsmath}
\usepackage{stackengine}
\begin{document}
\begin{table*}[t]
\centering
\begin{tabular}{|c|c|}
  \hline
  \belowbaseline[0pt]{\includegraphics{diagram}}
    &
  \belowbaseline[0pt]{$\begin{aligned} I &= \\
    &\frac{D}{4} \end{aligned}$}\\
\hline
\end{tabular}
\end{table*}
\end{document}
Stefan
LaTeX.org admin
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Vertical Alignment of Equations within a Table

Post by Johannes_B »

Use m-columns with a fixed width.

Code: Select all

\documentclass{article}
\usepackage{array}
\usepackage{mathtools}
\begin{document}
\begin{table*}[t]
	\centering
	\begin{tabular}{|m{.5\linewidth}|m{.4\linewidth}|}
		\hline
		\textbf{feynmp diagram goes here}
		\rule{1cm}{8cm}
		&
		$\begin{aligned} I &= \\ &\frac{D}{4} \end{aligned}$
		\\
		\hline
	\end{tabular}
\end{table*}
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply