3

I am trying to vertical align a matrix to the content of the other cells in a table, and im pretty much stuck. Here is a quick mockup of whats my problem.

In fig2 at least the content is aligned correctly.

It seams to me that the math mode environment itself is the one to be aligned, since a nested table thats not wrapped in math mode is aligned correctly.

    \documentclass{article}
    \usepackage[margin=1in]{geometry}
    \usepackage{tabularx}
    \usepackage{amsmath,mleftright}
    \usepackage[nopar]{lipsum}

    \begin{document}
    \begin{tabularx}{\textwidth}[t]{|X|c|l|}        % fig1
        \hline
        \lipsum[5] &
        $\begin{aligned}[t]\begin{bmatrix} a\\ b\\ c\\ d\\ e\end{bmatrix}\end{aligned}$ &
        $123$ \\
        \hline
    \end{tabularx}

    \vspace{2cm}

    \begin{tabularx}{\textwidth}[t]{|X|c|l|}        % fig2
        \hline
        \lipsum[5] &
        $\mleft[\begin{tabular}[t]{@{}l@{}}
          First line\\
          Second line\\
          Third line
        \end{tabular}\mright]$
        &
        123\\
        \hline
    \end{tabularx}
    \end{document}

fig1: tabmat fig2: tabmat

  • So a paragraph, a matrix, and a line of text all aligned to the top of their cells? – Mike Renfro Mar 18 '16 at 1:24
1

Using adjustbox and a variation of egreg's answer to Alignment in inline math:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tabularx}
\usepackage[nopar]{lipsum}
\usepackage{adjustbox}

\begin{document}
\begin{tabularx}{\textwidth}[t]{|X|c|l|}
\hline
\lipsum[5] &
\begin{adjustbox}{valign=t}\( \begin{bmatrix} a\\ b\\ c\\ d\\ e\end{bmatrix} \)\end{adjustbox} &
\( 123 \) \\
\hline
\end{tabularx}
\end{document}

enter image description here

  • thank you so much for this, but can you explain to my why this happens in the first place? – cdx Mar 18 '16 at 17:06
  • I don't have a good explanation, but typically, math environments are centered vertically with respect to the main text baseline. That forces line 1 of columns 1 and 3, and line 3 of column 2 to be on the same baseline. As to why that forces your column 1 text down instead of having column 2 break out of the top of the table, I can't answer. – Mike Renfro Mar 19 '16 at 14:47
2

Like this? I changed the definition of X column type to m and added some vertical padding to the cells with cellspace (for column specifiers prefixed with S – compare the first and the second table):

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{tabularx}
\renewcommand{\tabularxcolumn}[1]{>{\arraybackslash}m{#1}}
\usepackage{cellspace}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}
\addparagraphcolumntypes{X}
\usepackage{amsmath,mleftright}
\usepackage[nopar]{lipsum}

\begin{document}
\begin{tabularx}{\textwidth}[t]{|S{X}|c|l|} % fig1
    \hline
    \lipsum[5] &
    $\begin{aligned}[t]\begin{bmatrix} a\\ b\\ c\\ d\\ e\end{bmatrix}\end{aligned}$ &
    $123$ \\
    \hline
\end{tabularx}

\vspace{2cm}

\begin{tabularx}{\textwidth}[t]{|X|c|l|} % fig2
    \hline
    \lipsum[5] &
    $\mleft[\begin{tabular}{@{}l@{}}
      First line\\
      Second line\\
      Third line
    \end{tabular}\mright]$
    &
    123\\
    \hline
\end{tabularx}
\end{document}

enter image description here

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.