# How to correctly pad and align a displaystyle inline equation in a table

I've got the following code in a document and am at a loss trying to figure out how I can correctly pad the cells and/or align the equations correctly.

\begin{table}[h]
\begin{tabularx}{\columnwidth}{ |X|l| }
\hline
\textbf{Measurement} & \textbf{Equation} \\
\hline \hline
Euclidean distance between the shape model parameters vector for all landmarks $b$ and without the current landmark $b'$ & $\|b-b'\|$ \\
\hline
Average point-wise distance between the matched shape for all landmarks $\bar{x}$ and without the current landmark $\bar{x'}$ & $\displaystyle \frac{\sum_{i=1}^{3(m-1)}\bar{x}_i-\bar{x'}_i}{3(m-1)}$\\
\hline
\end{tabularx}
\caption{Landmark influence measurements.}
\label{tab:1}
\end{table}


This gives me the following output: Whereas I'd much prefer something like this: As you can see, the differences are the vertical padding of the left column and the alignment of the equations in the right column. Can someone point me in the proper direction on this one?

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Feb 3 '14 at 17:02

## 3 Answers

We redefine the X column type to be of m type instead of p:

\renewcommand\tabularxcolumn{m{#1}}


and create a new column type M with horizontal and vertical centering:

\newcolumntype{M}{>{\centering\arraybackslash}m{2.7cm}}


MWE

\documentclass[twocolumn]{article}
\usepackage{tabularx}

\renewcommand\tabularxcolumn{m{#1}}
\newcolumntype{M}{>{\centering\arraybackslash}m{2.7cm}}

\begin{document}

\begin{table}[h]
\begin{tabularx}{\columnwidth}{ |X|M| }
\hline
\textbf{Measurement} & \textbf{Equation} \\
\hline \hline
Euclidean distance between the shape model parameters vector for all landmarks $b$ and without the current landmark $b'$ & $\|b-b'\|$ \\
\hline
Average point-wise distance between the matched shape for all landmarks $\bar{x}$ and without the current landmark $\bar{x'}$ & $\displaystyle \frac{\sum_{i=1}^{3(m-1)}\bar{x}_i-\bar{x'}_i}{3(m-1)}$\\
\hline
\end{tabularx}
\caption{Landmark influence measurements.}
\label{tab:1}
\end{table}

\end{document}


Output: ## EDIT

If you still want "Equation" to be left aligned use \multicolumn:

\multicolumn{1}{l|}{\textbf{Equation}}


MWE:

\documentclass[twocolumn]{article}
\usepackage{tabularx}

\renewcommand\tabularxcolumn{m{#1}}
\newcolumntype{M}{>{\centering\arraybackslash}m{2.7cm}}

\begin{document}

\begin{table}[h]
\begin{tabularx}{\columnwidth}{ |X|M| }
\hline
\textbf{Measurement} & \multicolumn{1}{l|}{\textbf{Equation}} \\
\hline \hline
Euclidean distance between the shape model parameters vector for all landmarks $b$ and without the current landmark $b'$ & $\|b-b'\|$ \\
\hline
Average point-wise distance between the matched shape for all landmarks $\bar{x}$ and without the current landmark $\bar{x'}$ & $\displaystyle \frac{\sum_{i=1}^{3(m-1)}\bar{x}_i-\bar{x'}_i}{3(m-1)}$\\
\hline
\end{tabularx}
\caption{Landmark influence measurements.}
\label{tab:1}
\end{table}

\end{document}


Output: • You've certainly helped me along my way and answered my question, thanks a lot. Could you clarify why this works? How did you determine the value 2.7cm? And why did alignment of the equations solve the padding issue on the left as well? – Korijn Feb 3 '14 at 19:40
• @Korijn 2.7cm is found by hand (2.6cm gives bad boxes). The padding issue is solved by \renewcommand\tabularxcolumn{m{#1}}. – karlkoeller Feb 3 '14 at 19:52
• Thanks! I think I'll let "Equation" align with its contents. :) – Korijn Feb 3 '14 at 20:42

Package tabularx maps its column type X internally to p columns. This can be changed to vertically centered m columns of package array by redefining \tabularxcolumn:

\documentclass[a5paper]{article}
\usepackage{tabularx}
\usepackage{array}

\begin{document}
\begin{table}[h]
\renewcommand*{\tabularxcolumn}{m{#1}}
\setlength{\extrarowheight}{1.5pt}
\begin{tabularx}{\columnwidth}{ |X|>{$\displaystyle}c<{$}| }
\hline
\textbf{Measurement} & \textbf{Equation} \\
\hline \hline
Euclidean distance between the shape model parameters vector for all
landmarks $b$ and without the current landmark $b'$
& \|b-b'\| \\
\hline
Average point-wise distance between the matched shape for all landmarks
$\bar{x}$ and without the current landmark $\bar{x'}$
& \frac{\sum_{i=1}^{3(m-1)}\bar{x}_i-\bar{x'}_i}{3(m-1)} \\
\hline
\end{tabularx}
\caption{Landmark influence measurements.}
\label{tab:1}
\end{table}
\end{document} Remarks:

• The equation column automatically switches to math mode and sets \displaystyle.
• array is not needed. – karlkoeller Feb 3 '14 at 17:00
• Thanks for the clarification on some of the latex behaviour we're seeing here. – Korijn Feb 3 '14 at 19:43
• @karlkoeller: \extrarowheight requires package array. Currently tabularx loads package array, but this is an implementation detail and could change in future. – Heiko Oberdiek Feb 3 '14 at 20:02
• @HeikoOberdiek Sorry for bothering... – karlkoeller Feb 3 '14 at 20:05

Redefine the tabularx column and sue a fixed width for the second column:

\documentclass{article}
\usepackage{tabularx}
\renewcommand\tabularxcolumn{m{#1}}

\begin{document}

\begin{table}[h]
\setlength\abovedisplayskip{-5pt}
\setlength\belowdisplayskip{-5pt}
\begin{tabularx}{\columnwidth}{ |X|m{8em}| }
\hline
\textbf{Measurement} & \textbf{Equation} \\
\hline \hline
Euclidean distance between the shape model parameters vector for all landmarks $b$ and without the current landmark $b'$ & $\|b-b'\|$ \\
\hline
Average point-wise distance between the matched shape for all landmarks $\bar{x}$ and without the current landmark $\bar{x'}$ & $\frac{\sum\limits_{i=1}^{3(m-1)}\bar{x}_i-\bar{x'}_i}{3(m-1)}$\\
\hline
\end{tabularx}
\caption{Landmark influence measurements.}\label{tab:1}
\end{table}

\end{document} 