9

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:

Not so nice looking table

Whereas I'd much prefer something like this:

Much better looking table

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?

1
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Feb 3, 2014 at 17:02

3 Answers 3

6

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

\renewcommand\tabularxcolumn[1]{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[1]{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:

enter image description here


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[1]{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:

enter image description here

3
  • 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, 2014 at 19:40
  • @Korijn 2.7cm is found by hand (2.6cm gives bad boxes). The padding issue is solved by \renewcommand\tabularxcolumn[1]{m{#1}}. Feb 3, 2014 at 19:52
  • Thanks! I think I'll let "Equation" align with its contents. :)
    – Korijn
    Feb 3, 2014 at 20:42
5

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}[1]{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}

Result

Remarks:

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

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

\documentclass{article}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .