3

Please take the following MWE into account :

\documentclass[a4paper]{article}

\usepackage{tabularx}
\usepackage{array}
\usepackage{booktabs}

\begin{document}

\textbf{Array A :} 
\vspace{\baselineskip}


\begin{tabularx}{\linewidth}{>{\small\sf}p{3cm}p{1cm}XX}
\toprule
col1 & \multicolumn{3}{X}{\begin{minipage}[t]{\linewidth}
Everything should be on the same line and this is fine
\end{minipage}}\\

\end{tabularx}

\vspace{3\baselineskip}

\textbf{Array B :} 
\vspace{\baselineskip}

\begin{tabularx}{\linewidth}{>{\small\sf}p{3cm}p{1cm}XX}
\toprule
col1 & \multicolumn{3}{X}{\begin{minipage}[t]{\linewidth}
Everything should be on the same line and this is fine
\end{minipage}}\\

a & b & c & d\\

\end{tabularx}

\end{document}

Why is the text cut at half the space in array B ? I would like it to take all the space available on the line, like in array A. Note that I'm using minipage inside my column cells because they may contains itemize or tabular.

  • It should be \multicolumn{3}{l}... I guess. – LaRiFaRi Mar 2 '15 at 8:46
  • @LaRiFaRi putting l will end up in my text not being wrapped at the end of the cell if it's too long. – user17424 Mar 2 '15 at 8:48
  • 1
    related: tex.stackexchange.com/q/159009 As you can see there and in karlkoellers answer, this is not easily possible. Just set your minipage to 8.5 cm or leave it away completely. – LaRiFaRi Mar 2 '15 at 9:10
3

The tabularx documentation suggests to not use the X specifier in \multicolumns and also to not use \multicolumn entries which cross any X column.

If you really want to do that, you'd better use a p column and you don't need a minipage. Also, without a \noindent, using \linewidth exceeds the margins.

Last, but not least, use \sffamily instead of the deprecated (in LaTeX) \sf.

\documentclass[a4paper]{article}

\usepackage{tabularx}
\usepackage{array}
\usepackage{booktabs}

\begin{document}

\textbf{Array A :}
\vspace{\baselineskip}

\noindent
\begin{tabularx}{\linewidth}{>{\small\sffamily}p{3cm}p{1cm}XX}
\toprule
col1 & \multicolumn{3}{p{8cm}}{Everything can't fit on the same line because it's too long}\\
\end{tabularx}

\vspace{3\baselineskip}

\textbf{Array B :}
\vspace{\baselineskip}

\noindent
\begin{tabularx}{\linewidth}{>{\small\sffamily}p{3cm}p{1cm}XX}
\toprule
col1 & \multicolumn{3}{p{8cm}}{Everything can't fit on the same line because it's too long}\\
a & b & c & d\\
\end{tabularx}

\end{document} 

enter image description here

  • Thank you for your input. How did you find the 8cm value ? I don't have the same margins, so if there's a technique to actually calculate this value easily, I'm very interrested. – user17424 Mar 2 '15 at 9:15
  • @NNzz You're welcome. The value of 8.2cm seems to be the maximum not to exceed the margin (found by trial) – karlkoeller Mar 2 '15 at 9:27
1

You can avoid guessing the required length by noticing you want to span 2 X columns, a 3cm p column and two intercolumn gaps each of width 2\tabcolsep

\documentclass[a4paper]{article}

\usepackage{tabularx}
\usepackage{array}
\usepackage{booktabs}

\begin{document}

\textbf{Array A :}
\vspace{\baselineskip}

\noindent
\begin{tabularx}{\linewidth}{>{\small\sffamily}p{3cm}p{1cm}XX}
\toprule
col1 & \multicolumn{3}{p{8cm}}{Everything can't fit on the same line because it's too long}\\
\end{tabularx}

\vspace{3\baselineskip}

\textbf{Array B :}
\vspace{\baselineskip}

\noindent
\begin{tabularx}{\linewidth}{>{\small\sffamily}p{3cm}p{1cm}XX}
\toprule
col1 & \multicolumn{3}{X}{\hsize=\dimexpr2\hsize+4\tabcolsep+3cm\relax
Everything can't fit on the same line because it's too long}\\
a & b & c & d\\
\end{tabularx}

\end{document}

Your Answer

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