3

MWE

\documentclass[12pt]{article}

\usepackage[
    a4paper,
    left = 5mm,
    right = 5mm,
    landscape,
    %showframe,
    ]
    {geometry}

\usepackage{tabularx}
\usepackage{multirow}

% Thicker table lines for screenshot
\setlength{\arrayrulewidth}{1.5pt}
\setlength{\parindent}{0pt}

\usepackage[table]{xcolor}
\definecolor{myTableColor}{gray}{0.80}

% Nice sf font for screenshot
\usepackage[sfdefault]{cabin}

% https://tex.stackexchange.com/questions/163061
% https://tex.stackexchange.com/questions/89166
\newcolumntype{Y}{>{\centering\arraybackslash}X} 

\begin{document}

\section*{Red Text Causes Strange Column}

\begin{tabularx}{\textwidth}{lc|l|Y|Y|Y|Y}
& & & \multicolumn{3}{c}{\multirow{2}{*}{\parbox{130mm}{\centering Text}}} & \\ 
% New Row
& & & \multicolumn{3}{l}{} & \\ 
% New Row
Text & Text & & & & & \\ \hline
% New Row
\rowcolor{myTableColor}
Text & Text & & & & \textcolor{red}{Problem Column} & \\ \hline
% New Row
Text Text Text Text \textcolor{red}{Text} & Text & Text Text Text Text Text \textcolor{red}{Text} & & & & \\ \hline
% New Row
Text & Text & & & & & \\ \hline
\end{tabularx}

\section*{Red Text Removed}

\begin{tabularx}{\textwidth}{lc|l|Y|Y|Y|Y}
& & & \multicolumn{3}{c}{\multirow{2}{*}{\parbox{130mm}{\centering Text}}} & \\ 
% New Row
& & & \multicolumn{3}{l}{} & \\ 
% New Row
Text & Text & & & & & \\ \hline
% New Row
\rowcolor{myTableColor}
Text & Text & & & & \textcolor{blue}{No Problem Column} & \\ \hline
% New Row
Text Text Text Text & Text & Text Text Text Text Text & & & & \\ \hline
% New Row
Text & Text & & & & & \\ \hline
\end{tabularx}

\end{document}

MWE Output

enter image description here

Problem

  • I have a table in which I use tabularx and multirow.
  • When I add the red text in the MWE then I get a shift in one of the X columns.
  • I don't understand what happens.

Update

After the chat in the comments, I think that this may be the better question:

Can I use a multirow and multicolumn with automatic line break without having to specify the width explicitly (here 120mm)?

"Real" Table

This is the "real" table.

enter image description here

9
  • 1
    it seems that the width of \parbox in multirow cell is wider than sum of column width below of multicol cell in which you have multirow. If you will reduce its width from 130 mm to 90 mmm (as example), your problem will disappear. Or I misunderstanding your problem?
    – Zarko
    Commented Apr 22, 2017 at 23:48
  • @Zarko I thought that X columns have all the same width. I am confused that it affects only one column. Question: Can I use a multirow with automatic line break without having to specify the width explicitly (here 120mm)? And thanks for the comment. Commented Apr 22, 2017 at 23:51
  • 1
    you have used \multicolumn which by design replaces the column specification from the table preamble by the specification for this cell, so here you have a fixed width p column with a fixed width that is wider than the natural width of the columns being spanned, in such a case the tex \halign primitive always adds the excess width to the last spanned column. Commented Apr 22, 2017 at 23:55
  • 1
    meanwhile I will try to find the answer of @Mico, where he solve similar problem.
    – Zarko
    Commented Apr 23, 2017 at 0:05
  • 1
    @Zarko see below:-) Commented Apr 23, 2017 at 0:06

1 Answer 1

4

You want to use a Y column for your spanning entry

enter image description here

\documentclass[12pt]{article}

\usepackage[
    a4paper,
    left = 5mm,
    right = 5mm,
    landscape,
    %showframe,
    ]
    {geometry}


\usepackage{tabularx}
\usepackage{multirow}

% Thicker table lines for screenshot
\setlength{\arrayrulewidth}{1.5pt}
\setlength{\parindent}{0pt}

\usepackage[table]{xcolor}
\definecolor{myTableColor}{gray}{0.80}

% Nice sf font for screenshot
\usepackage[sfdefault]{cabin}

% https://tex.stackexchange.com/questions/163061
% https://tex.stackexchange.com/questions/89166
\newcolumntype{Y}{>{\centering\arraybackslash}X} 

\begin{document}

\section*{Red Text Causes Strange Column}

\begin{tabularx}{\textwidth}{lc|l|Y|Y|Y|Y}
& & & 
\multicolumn{3}{>{\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth\relax}Y}
{\multirow{2}{=}{\centering Text 
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
}} & \\ 
% New Row
& & & \multicolumn{3}{l}{} & \\ 
% New Row
Text & Text & & & & & \\ \hline
% New Row
\rowcolor{myTableColor}
Text & Text & & & & \textcolor{red}{Problem Column} & \\ \hline
% New Row
Text Text Text Text \textcolor{red}{Text} & Text & Text Text Text Text Text \textcolor{red}{Text} & & & & \\ \hline
% New Row
Text & Text & & & & & \\ \hline
\end{tabularx}

\section*{Red Text Removed}

\begin{tabularx}{\textwidth}{lc|l|Y|Y|Y|Y}
& & & \multicolumn{3}{c}{\multirow{2}{*}{\parbox{130mm}{\centering Text}}} & \\ 
% New Row
& & & \multicolumn{3}{l}{} & \\ 
% New Row
Text & Text & & & & & \\ \hline
% New Row
\rowcolor{myTableColor}
Text & Text & & & & \textcolor{blue}{No Problem Column} & \\ \hline
% New Row
Text Text Text Text & Text & Text Text Text Text Text & & & & \\ \hline
% New Row
Text & Text & & & & & \\ \hline
\end{tabularx}

\end{document}
4
  • Thanks! So in order to have a parbox with the natural length of the multicolumns I have to use\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth\relax}Y? Commented Apr 23, 2017 at 12:24
  • 1
    @Dr.ManuelKuehner yes three X columns would be 3\hsize but you are spanning two column separators each of which is a space or tabcolsep, a rule of width arrayrulewidth and another tabcolsep space Commented Apr 23, 2017 at 15:03
  • @DavidCarlisle, it should be pointed that text in multirow cells should not be longer then two lines, otherwise it will protrude into rows below this cell.
    – Zarko
    Commented Apr 23, 2017 at 15:58
  • @Zarlo well the multi row entry has to be smaller than the rows spanned Commented Apr 23, 2017 at 16:06

You must log in to answer this question.

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