1

I'm in a situation where I have two tables, and for space reasons I need to show them beside each other. Most entries to each column of the table are short, but occasionally one is long.

What I'd like is to specify the width of the table, and if a row can't be fit into that width, it's broken up into two lines.

The tricky bit is that sometimes the cells contain math that can't easily be broken up, so in such cases, I'd like to allow the long math to go past the width of its column, and put the remaining columns of the row one line below, so the text doesn't overlap.

This answer suggests that tabularx might be helpful, but I'm not sure how to allow things to go past their columns.

Is this possible? Is there a way to put two tables side-by-side and handle long rows gracefully, without having to manually adjust the offending rows?

I've got a MWE below with two versions. The first is what I'm starting with, tabulars with minipages, and they just spill over.

The second is roughly what I want my result to look like. The biggest things I'm wondering are:

  1. Is there a way to achieve this automatically, i.e. without manually splitting things onto multiple lines and using mathrlap or such?
  2. Why is there extra vertical space in the tabularx version? Where do I need to put X in the layout specification to properly convert from tabular to tabularx?

Here's the code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\usepackage{amsmath}

\usepackage{mathtools}

\begin{document}
\begin{minipage}{0.48\textwidth}
\begin{tabular}{@{}l@{}l@{\ \ }c@{\ \ }ll@{}llll@{}}
\\ & & $\mid$ & $loooooonnnnngggg$ & & $info$ & $Comment$
\\ & & $\mid$ & $info$ & & $info$ & Long long long long comment
\\ & & $\mid$ & $info$ & & $info$ & $Comment$
\end{tabular}
\end{minipage}
\begin{minipage}{0.48\textwidth}
\begin{tabular}{@{}l@{}l@{\ \ }c@{\ \ }ll@{}llll@{}}
\\ & & $\mid$ & $info$ & & $info$ & $Comment$
\\ & & $\mid$ & $info$ & & $info$ & $Comment$
\\ & & $\mid$ & $info$ & & $info$ & $Comment$
\end{tabular}
\end{minipage}

\begin{minipage}{0.48\textwidth}
\begin{tabularx}{\textwidth}{@{}l@{}l@{\ \ }c@{\ \ }ll@{}XXXX@{}}
\\ & & $\mid$ & $\mathrlap{loooooonnnnngggg}$ & &  & 
\\ & &  &  & & $info$ & $Comment$

\\ & & $\mid$ & $info$ & & $info$ & Long long long long comment
\\ & & $\mid$ & $info$ & & $info$ & $Comment$
\end{tabularx}
\end{minipage}
\begin{minipage}{0.48\textwidth}
\begin{tabular}{@{}l@{}l@{\ \ }c@{\ \ }ll@{}llll@{}}
\\ & & $\mid$ & $info$ & & $info$ & $Comment$
\\ & & $\mid$ & $info$ & & $info$ & $Comment$
\\ & & $\mid$ & $info$ & & $info$ & $Comment$
\end{tabular}
\end{minipage}

\end{document}
  • 1
    just an initial comment, it's a bit odd to put \\ at the start of every row as it forces a blank line at the start, \\ ends a row. – David Carlisle Apr 7 '18 at 0:10
2

I removed the minipages which were not doing anything as a tabular is already a box and used .5\textwidth rather than .48

The macro \z measures its argument and ends the row early if long.

Your data only had three columns but the table specifications had 9 columns so I simplified it, perhaps your real data will need some of the column specs put back.

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}


\usepackage{mathtools}
\makeatletter
\def\z#1{\sbox{0}{$#1$}%
  \ifdim\wd0>0.2\textwidth
     \expandafter\@firstoftwo
   \else
     \expandafter\@secondoftwo
    \fi
    {\rlap{\usebox{0}}\\\multicolumn{1}{l}{}}{\usebox{0}}}
\makeatother
\begin{document}

\noindent
\begin{tabularx}{.5\textwidth}{!{$\mid$}llX}
\z{loooooonnnnngggg} & $info$ & $Comment$\\
\z{info} & $info$ & Long long long long comment\\
\z{info} & $info$ & $Comment$
\end{tabularx}%
\begin{tabularx}{.5\textwidth}{!{$\mid$}llX}
$info$ & $info$ & $Comment$\\
$info$ & $info$ & $Comment$\\
$info$ & $info$ & $Comment$
\end{tabularx}


\end{document}
  • Perfect! One quick question, is there any way to make the "info comment" in the first row right-aligned? – jmite Apr 7 '18 at 3:34
  • @jmite yes of course just use r rather than l in the column specification. – David Carlisle Apr 7 '18 at 7:31

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.