1

Bundled images link: https://i.stack.imgur.com/Ew3tL.jpg

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://i.stack.imgur.com/znu9V.jpg

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://i.stack.imgur.com/umTmX.jpg

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://i.stack.imgur.com/xU0la.jpg

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!

jaaq
  • 135

1 Answers1

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?

Zarko
  • 296,517
  • 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 https://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
  • For this question I used this table preambule to fine tune the columnn width with one parameter : `\newlength{\TbW} \setlength{\TbW}{.16\linewidth}

    \begin{table}[!ht] \begin{tabularx}{\linewidth}{% |X*{2}{|m{\TbW}}|m{2\TbW}|}\hline`

    – Tarass Mar 19 '18 at 19:37
  • a just asked the question. Thank you, it is very interesting. – Tarass Mar 19 '18 at 20:23
  • 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
  • although the mismatch 4!=5 accounts for why your spanning headings are not centred – David Carlisle Mar 19 '18 at 20:41
  • @DavidCarlisle, corrected. but still have some problems with my solution, which now avoided to restrict me to cases when \multicolumn contain one line contain and therefore can be simple \multicolumn{2}{c}{...} (this is also considered in multirow cells. thank you very much for your comment. – Zarko Mar 19 '18 at 22:29
  • If I take your code and try to compile it I get: "!File ended while scanning for use of @argdef. Suspect you have forgotten a '}'" Error.

    I also don't quite understand the parameters you give tabularx. Using Texmaker 4.3

    – jaaq Mar 19 '18 at 22:33
  • @jaaq, i try to correct code directly here, but it seems with this i introduce new errors. now i copy complete tested code from my editor which should work. – Zarko Mar 19 '18 at 22:52
  • @Zarko I am terribly sorry to bother you further but in which environment are you compiling this? I am running Texmaker4 on windows 10 and it throws out lots of "missing number, treated as a zero" errors. Also how does it look on your pc? – jaaq Mar 20 '18 at 10:14
  • @jaaq, i use 64-bit miktex (recent version) and for editor winedt 10.2. i tested my code again. it works without any warnings and errors. by it i generated shod table image. – Zarko Mar 20 '18 at 11:23