TransWikia.com

Latex multiline multicol and multirow page-wide table

TeX - LaTeX Asked by Aleksei Liubimov on February 9, 2021

I have some trouble while creating complicated (for me) table.

To be:

table example

I wrote that code:

begin{tabularx}{linewidth}{|X|X|X|X|X|X|X|X|}
hline
multirow{2}{*}{Short text} & multicolumn{4}{|>{hsize=dimexpr4hsize+4tabcolsep+arrayrulewidthrelax}X|}{Some long multi column text that can be multi line} & multirow{2}{*}{Some long one column text that can be multi line}  & multirow{2}{*}{Some long one column text that can be multi line} &  multirow{2}{*}{Some long one column text that can be multi line}
hline
& long_word & long_word & long_word & long_word &  &  &  
hline
end{tabularx}

But words in second line are not hyphenated. And multirow doesn’t work.
What I’m doing wrong?

3 Answers

The table width calculations for your table are somewhat more involved than usual, since you're specifying ratios for the total column widths rather than for the usable column widths. LaTeX needs to be given information about the usable column widths. One has to subtract any whitespace padding and the widths of vertical rules from the total column widths in order to obtain the usable columns widths.

Let's label the usable column widths xlen, ylen, and zlen, respectively, and the total column widths xtotlen, ytotlen, and ztotlen, respectively. The total column widths must satisfy

5xtotlen+ytotlen+2ztotlen=textwidth
ytotlen=3xtotlen
ztotlen=2totxlen

(Aside: Observe that textwidth=12xtotlen.)

Next, recall that

xtotlen=xlen+2tabcolsep+1arrayrulewidth
ytotlen=ylen+2tabcolsep+1arrayrulewidth
ztotlen=zlen+2tabcolsep+1arrayrulewidth

After some rearranging, we may express the usable column widths as follows:

xlen = (textwidth-24tabcolsep-12arrayrulewidth)/12
ylen = 3xlen+4tabcolsep+2arrayrulewidth
zlen = 2xlen+2tabcolsep+1arrayrulewidth

Eagle-eyed readers may have noticed that these calculations have failed to incorporate the width of the very first vertical rule, located to the left of the first column; its width is equal to arrayrulewidth. To make the width calculations truly correct, then, one must change xlen = (textwidth-24tabcolsep-12arrayrulewidth)/12 to xlen = (textwidth-24tabcolsep-13arrayrulewidth)/12. (Given that the default value of tabcolsep is 0.4pt, I doubt anyone would ever notice the difference.)


OK, enough talk! Let's see how this all works out.

enter image description here

documentclass{article}
usepackage[T1]{fontenc}
usepackage[letterpaper,margin=1in]{geometry} % set page parameters suitably
usepackage{multirow}  
usepackage{array}    % for newcolumntype macro
usepackage{ragged2e} % for RaggedRight macro
newcolumntype{P}[1]{>{RaggedRighthspace{0pt}}p{#1}}

%% Compute the three column widths:
newlengthxlen
setlengthxlen{dimexprtextwidth-24tabcolsep-13arrayrulewidthrelax}
setlengthxlen{dimexpr0.08333333xlenrelax} % 1/12 approx 0.08333333
newlengthylen
setlengthylen{dimexpr3xlen+4tabcolsep+2arrayrulewidthrelax}
newlengthzlen
setlengthzlen{dimexpr2xlen+2tabcolsep+1arrayrulewidthrelax}

newcommandlongword{long_discretionary{}{}{}word}

begin{document}
noindent % <-- this is important
begin{tabular}{ | *{5}{P{xlen}|} P{ylen}| *{2}{P{zlen}|} }
hline
  multirow{4}{=}{Short text}
& multicolumn{4}{>{RaggedRight}P{dimexpr4xlen+6tabcolsep+3arrayrulewidth}|}{%
     Some long multi-column text which can be multi-line} 
& multirow{4}{=}{Some long one-column text which can be multi-line}
& multirow{4}{=}{Some long one-column text which can be multi-line}
& multirow{4}{=}{Some long one-column text which can be multi-line} 
cline{2-5} % not "hline"
& longword & longword & longword & longword &  &  &  

hline
end{tabular}
end{document}

Answered by Mico on February 9, 2021

For fixed width column, in a multirow,you should use the {=}width argument, not {*}. Furthermore, in case of multilined cells, the first argument, should be the total number of encompassed lines, not the number of rows. Furthermore again, for fine-tuning of the vertical position of the multirow contents, you can use a non-integer number of lines. Finally, as tabularx seems to have problems with fixed width column types in the argument of multicolumn}, I used a multicolumn{4}{c}{makecell{......}}, where line breaks are done by hand.

documentclass{article}
usepackage{geometry}
usepackage{calc}
usepackage{tabularx, multirow, makecell}

begin{document}

centeringsmall
setlength{extrarowheight}{3pt}
begin{tabularx}{linewidth}{|*{5}{>{hsize=0.66hsize}X|}>{hsize=2hsize}X|*{2}{>{hsize=1.35hsize}X|}}
hline
 multirow{3}{=}{Short text} &
multicolumn{4}{c|}{makecell{Some long multi column text that can be multi line}} 
& multirow{3}{=}{Some long one column text that can be multi line} & multirow{2}{=}{Some long one column text that can be multi line} & multirow{2}{=}{Some long one column text that can be multi line}
cline{2-5}
& long_ word & long_ word & long_ word & long_ word & & & 
hline
end{tabularx}

end{document} 

enter image description here

Answered by Bernard on February 9, 2021

just out of curiosity since the author said

  Proportionally columns should be so: 1-1-1-1-1-3-2-2 

dividing the textwidth into 12 columns --

the first 5 columns are 0.08 times textwidth --

the next is 3 times 0.08 --

and the last two are 2 times 0.08

enter image description here

documentclass{article}
%usepackage[showframe]{geometry}
usepackage{graphicx}
usepackage{ragged2e}
usepackage{tabularx}

begin{document}

begin{table}[!ht]
    centering
    renewcommand{arraystretch}{1.5} % 
    setlength{tabcolsep}{4pt}
begin{tabularx}{textwidth}
    {
        |*{5}{>{centeringarraybackslashhsize=0.08hsize}X|}
        *{1}{>{centeringarraybackslashhsize=0.24hsize}X|}
        *{2}{>{centeringarraybackslashhsize=0.16hsize}X|}
    }
            hline%
           G  & G & G & G & G & G & G & G 
            hline%
end{tabularx}
end{table}

end{document}

Answered by js bibra on February 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP