1

How do you vertically center the last column of a tabularx table?

I tried to create a custom column, though, TBH, I don't really know what I'm doing.

Basically, I'd like the contents of the left and right columns to aligned in the same way, both vertically and horizontally. What do I need to do to they y type column? I want to use \m{} of some type, but am not sure how.

\documentclass[12pt,letterpaper]{article}

\usepackage{tabularx}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{Y}{>{\centering\arraybackslash}X}

\usepackage{tikz}

\begin{document}

\begin{tabularx}{17cm}{|C{8cm}|Y|} \hline

\tikz{
\draw (0,0)--(4,0);
\foreach \x in {0,...,4}
 \draw (\x,-0.3)--(\x,0.3);
\foreach \x in {0,...,3} 
 \draw[-latex] (\x+0.1,-0.15)--(\x+0.9,-0.15);
\node at (2,-0.8) {4};
} &
\tikz{
\draw (0,0)--(6,0);
\foreach \x in {0,...,6}
 \draw (\x,-0.3)--(\x,0.3);
\foreach \x in {0,...,5}
 \draw[-latex] (\x+0.1,-0.15)--(\x+0.9,-0.15);
 \node at (3,-0.8) {6};}
\\ \hline
\end{tabularx}

\end{document}
  • Heiko has answered the question but really tabularx is doing nothing useful here. Why not just use \begin{tabular}{cc} and arrange the vertical allignment with tikz? Actually if it is really one row in the actual document you don't need tabularr either, just two tikz environments. – David Carlisle Sep 14 '15 at 10:13
  • @ David Carlisle, I'm still a bit of a LaTeX newb, so I'm not sure how to space the columns in \begin{tabular} properly. tabularx seemed to help a lot with that, though. Also, there will be quite a few more rows. I included only one for the MWE. – WeCanLearnAnything Sep 14 '15 at 15:55
3

Package tabularx uses \tabularxcolumn for the column type of X columns. The default is p{#1}. It can be redefined to match the other m column:

\documentclass[12pt,letterpaper]{article}

\usepackage{tabularx}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{Y}{>{\centering\arraybackslash}X}

\usepackage{tikz}

\begin{document}

\renewcommand*{\tabularxcolumn}[1]{m{#1}}

\begin{tabularx}{17cm}{|C{8cm}|Y|} \hline
\tikz{
\draw (0,0)--(4,0);
\foreach \x in {0,...,4}
 \draw (\x,-0.3)--(\x,0.3);
\foreach \x in {0,...,3}
 \draw[-latex] (\x+0.1,-0.15)--(\x+0.9,-0.15);
\node at (2,-0.8) {4};
} &
\tikz{
\draw (0,0)--(6,0);
\foreach \x in {0,...,6}
 \draw (\x,-0.3)--(\x,0.3);
\foreach \x in {0,...,5}
 \draw[-latex] (\x+0.1,-0.15)--(\x+0.9,-0.15);
 \node at (3,-0.8) {6};}
\\ \hline
\end{tabularx}

\end{document}

Result

  • Awesome! It worked perfectly. I'm not sure how it worked, though. My guess is that your \renewcommand*{\tabularxcolumn}[1]{m{#1}} renewed ONLY the vertical alignment of the X column. Is that right? My other question is what is the asterisk here for? – WeCanLearnAnything Sep 14 '15 at 22:44
  • @WeCanLearnAnything Package tabularx uses \tabularxcolumn for the internal column format of its X columns. The default is p{#1}. It can be changed by redefining to \m{#1}. #1 is a length and should not contain whole paragraphs (\par). The star form of \newcommand forbids these \par tokens. If the ending curly brace is forgotten for the argument, then TeX will already stop at the next empty line or \par token to throw the error and does not have to wait for the end of file. – Heiko Oberdiek Sep 15 '15 at 2:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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