3

I want to draw the table below in latex using the tabularx environment. However, the \multicolumn{1.5} is unfortunately not working. Do you have any idea how to draw this kind of tables where the multicolumn command uses a decimal number (to preserve the same cell width)?

enter image description here

I drew a table using the below code, but the cells don't have the same width.

\begin{table}[h!]
\centering
\begin{tabularx} {0.3\textwidth}
    { l  l  l l}
    \toprule[2pt]
    Parameter & \multicolumn{3}{c}{value} \\
    \midrule[0.5pt]
    P1 & \multicolumn{1}{c}{V1} & \multicolumn{2}{c}{V2}\\
    P2 & V1 & V2 & V3 \\        
    \bottomrule[2pt]
\end{tabularx}
\end{table}

Result:

enter image description here

Kind regards. Widad.

Mico
  • 506,678

1 Answers1

4

In order to specify a multicolumn of 1.5 cells, you can multiply the widths until you reach an integer - multiply by 2 to get 3 in this case. The other rows need to be multiplied as well.

Now, the next problem is that no cells of width 1 remain to compute the size of the multicolumns. You can insert a phantom row in a longtable to obtain the right sizes and \kill it afterwards. A bit overkill - but nobody said LaTeX code was pretty :D

MWE:

\documentclass{article}
\usepackage{longtable}
\begin{document}
\begin{longtable}{|l|llllll|}
\hline
 & V1 & V2 & V3 & V4 & V5 & V6\kill 
Parameter & \multicolumn{6}{|l|}{value}\\
\hline
P1 & \multicolumn{3}{|l|}{V1} & \multicolumn{3}{|l|}{V2}\\
\hline
P2 & \multicolumn{2}{|l|}{V1} & \multicolumn{2}{|l|}{V2} & \multicolumn{2}{|l|}{V3}\\
\hline
\end{longtable}
\end{document}

Result:

enter image description here

Marijn
  • 37,699
  • Marijn, this is exactly what I was looking for. It's a smart and easy solution, thank you! – Widad ES-SOUFI Mar 01 '18 at 07:39
  • I used Marjin's solution, but the longtable exceeds my textwidth. Is there any solution to force its width to textwidth? – Widad ES-SOUFI Mar 01 '18 at 09:47
  • From this answer from the author of longtable: put \setlength\LTleft{0pt} \setlength\LTright{0pt} before the table. – Marijn Mar 01 '18 at 10:19
  • Yes I already tried it, but it doesn't work for me. – Widad ES-SOUFI Mar 01 '18 at 12:18
  • I that case you could post a new question with an example that shows the textwidth problem (it might be closed as a duplicate but you can try it anyway if you believe that your problem is different). – Marijn Mar 01 '18 at 14:07
  • @WidadES-SOUFI a table is as wide as the data you put in it, so without seeing your data impossible to say. However you can put \small or \footnotesize before the longtable so it will be smaller and might fit (depending how much too wide it is of course) – David Carlisle Mar 06 '18 at 23:00