1

This question already has an answer here:

Bundled images link: https://imgur.com/a/4CiGi

I want to achieve this formatting of cells(incuding automatic linebreaks working or alternatively a good way to manually do it):

What I want to end up with(made in MS Word) :

enter image description here

However halfway in I saw that linebreaks apparently do not work within multirow cells:

What happens with the linebreaks? https://imgur.com/6WqnZuf

enter image description here

Then I tried splitting the text up to the 2 different cells but couldn't find a way to move down the one cell down so the text looks as if it was inside one cell

What happens with split text: https://imgur.com/WKUN9m1

enter image description here

Can you please provide me a working example for this table that includes automatic linebreaks? The last column really is relatively long.

I hacked something together but it isn't quite what I wanted: https://imgur.com/a/mDrru

Corrections are written in red. The very right column works great but the others don't quite do what I want them to do. Here is the code:

    \documentclass[12pt,a4paper]{article}
    \usepackage[utf8]{inputenc}
    \usepackage{amsmath}
    \usepackage{amsfonts}
    \usepackage{amssymb}
    \usepackage{multirow, tabularx, ragged2e}
    \newcolumntype{C}{>{\Centering\arraybackslash}X}

    \begin{document}

    \begin{table}[!ht]
    \begin{tabularx}{\textwidth}{|c|C|C|C|C}
    \hline
    \multirow{2}{*}{Text} & \multicolumn{2}{C|}{Text} & Very long text \\ \cline{2-4}
    & \multicolumn{2}{C|}{Text} & Very long text \\ \hline

    \multirow{1}{*}[-3.9em]{Text} & \multicolumn{2}{C|}{Text} & Very long text \\ \cline{2-4}

     Text& \multicolumn{2}{C|}{Text} & Very long text \\ \hline

    & \multirow{2}{*}{text} & text & text \\ \cline{3-4}

    & & text & very long text \\ \cline{2-4}

    Text & \multirow{1}{*}{Text} & Text & text \\ \cline{3-4}

    & \multirow{1}{*}{Text} & text & Very long text \\ \hline
    \end{tabularx}
    \end{table}
    \end{document}

THX in advance!

marked as duplicate by Tarass, Sebastiano, Phelype Oleinik, Troy, Bobyandbob Mar 19 '18 at 18:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

enter image description here

your question is very similar to questions in links provided in comments below your question. with small effort you should on given examples design your code. anyway, here we go:

  • your table has for columns but you define five
  • \multirow{1}{*}{...} hasn't sense. of one row cell its use is superflous. only make clutter in your code
  • for multi line text in \multirow cells you should define width of this cells. this you can done locally, for example as \multirow{4}{2cm}{....} or define width of column (as is done in mwe below) and than use \multieow{4}{=}{...} which take over defined width of column
  • use \multicolumn{2}{C|}{Text} means that text in this column can have width of one column. for wide width of one line text in cells is better to use \multicolumn{2}{c|}{Text}. however, in cases, that you like to have multi line text in this cells, you need increase its width. for this i use

\newlength\colwidth % new length
\setlength\colwidth{\dimexpr\hsize/4+2\tabcolsep-3\arrayrulewidth\relax}% calculation width 
                                                                       % of one column as
                                                                       % calculated by tabularx
\newcommand\mcxx[1]{\multicolumn{2}{>{\hsize=2\colwidth}C|}{#1}}%

complete mwe is:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow, tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newlength\colwidth
\setlength\colwidth{\dimexpr\hsize/4+2\tabcolsep-3\arrayrulewidth\relax}
\newcommand\mcxx[1]{\multicolumn{2}{>{\hsize=2\colwidth}C|}{#1}}%

\usepackage{lipsum}
\begin{document}

\begin{table}[!ht]
\renewcommand\arraystretch{1.3}
\begin{tabularx}{\textwidth}{|>{\hsize=0.4\hsize}C|
                        *{3}{ >{\hsize=1.2\hsize}C|}
                            }
    \hline
\multirow{2}{=}{Text}   & \mcxx{Text}   & Very long text    \\ \cline{2-4}
                        & \mcxx{Text}   & Very long text    \\ \hline
\multirow{2}{=}{Text}   & \mcxx{Text}   & Very long text    \\ \cline{2-4}
                        & \mcxx{Text}   & Very long text    \\ \hline
\multirow{4}{=}{Test text}
                        & \multirow{2}{=}{text}
                            & Text      & Text              \\ \cline{3-4}
                        &   & Text      & very long text    \\ \cline{2-4}
                        & \multirow{2}{=}{Text\\ text}
                            & Text      & Text              \\ \cline{3-4}
                        &   & text      & Very long text    \\
    \hline
\end{tabularx}
\end{table}
\end{document}

is this what you looking for?

  • I appreciate your example but I don't understand the meaning of >{\hsize=0.5\hsize} and >{\hsize=1.5\hsize}? What is \hsize here? – Tarass Mar 19 '18 at 18:08
  • 1
    @Tarass, as i seid in answer: column width. \hsize=0.5\hsize means, that this column has width equal to 0.5 width calculated by tabularx. by this you can change width of X columns. for details see documentation for tabularx ctan.org/pkg/tabularx?lang=en – Zarko Mar 19 '18 at 18:22
  • I wonder because the sum of width must stay inchanged : in the doc example {>{\hsize=.5\hsize}X>{\hsize=1.5\hsize}X} the sum is 2 for 2 columns, but in your example the sum is 5 for 4 columns ... – Tarass Mar 19 '18 at 19:05
  • How does one know about 'textwidth/5' with 4 declared columns? – Tarass Mar 19 '18 at 19:33
  • 1
    @Zarko I think you are crucially relying on this part of the TX documentation: As with most rules, these may be broken if you know what you are doing. – David Carlisle Mar 19 '18 at 20:35

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