0

I have a problem with creating a full-width table with multicolumn.

The code is:

\documentclass[]{article}
\usepackage{longtable,booktabs}
\usepackage{geometry}
 \geometry{
 a4paper,
 left=20mm,
 right=20mm,
 top=20mm,
 bottom=20mm
 }

\usepackage{ltablex}
\usepackage{calc}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{lipsum}

\definecolor{header-color}{HTML}{C0C0C0}
\definecolor{color-reserved}{HTML}{E0E0E0}
\definecolor{color1}{HTML}{F0F0F0}
\definecolor{color2}{HTML}{FFFFFF}

\begin{document}

\begin{tabularx}{\linewidth}%
   {% col 1
   |p{\widthof{Bit(s)}}|%
   % col 2
   p{\widthof{FIELD1\_NAME}}|%
   % col 3
   p{\widthof{Permissions}}|%
   % col 4
   p{\widthof{Reset Value}}|%
   % col 5
   X|
   }

    \hline
\rowcolor{header-color}
    \rowcolor{header-color}{\bf Bit(s)} & {\bf Field Name} & {\bf Permissions} & {\bf Reset Value} & {\bf Attributes} \\
    \hline
    \hline
    \endfirsthead
    \hline
    \rowcolor{header-color}{\bf Bit(s)} & {\bf Field Name} & {\bf Permissions} & {\bf Reset Value} & {\bf Attributes} \\
    \hline
    \hline
    \endhead

\rowcolor{color-reserved}0        & Reserved   & --          & --          & \\\hline  
\rowcolor{color2}
1         & FIELD1\_NAME & RW & 0x0 & \\\hline 
\rowcolor{color2}
             & \multicolumn{4}{p{\hsize}|}{
\lipsum[1-2]

} \\\hline 
\end{tabularx}

\end{document}

Unfortunately, it is not what I expected.

  1. The "Permission" column width is not wide enough
  2. There is an additional space on the right side which should not be there.

My expectations were that X will stretch the right column to the text width (as defined in the table configuration).

I intend to do create this table from the script and I can't set columns' sizes as constants.

enter image description here

leandriis
  • 62,593
Michael
  • 197

1 Answers1

0

Two possibilities with either fixed with p type columns or l type columns:

enter image description here

\documentclass[]{article}
\usepackage{longtable,booktabs}
\usepackage{geometry}
 \geometry{
 a4paper,
 left=20mm,
 right=20mm,
 top=20mm,
 bottom=20mm
 }

\usepackage{ltablex}
\usepackage{calc}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{lipsum}

\definecolor{header-color}{HTML}{C0C0C0}
\definecolor{color-reserved}{HTML}{E0E0E0}
\definecolor{color1}{HTML}{F0F0F0}
\definecolor{color2}{HTML}{FFFFFF}
\usepackage{makecell}
\renewcommand{\theadfont}{\normalsize\bfseries}
\begin{document}

\begin{tabularx}{\linewidth}%
   {% col 1
   |p{\widthof{\bfseries Bit(s)}}|%
   % col 2
   p{\widthof{FIELD1\_NAME}}|%
   % col 3
   p{\widthof{\bfseries Permissions}}|%
   % col 4
   p{\widthof{\bfseries Reset Value}}|%
   % col 5
   X|
   }

    \hline
    \rowcolor{header-color}{\bfseries Bit(s)} & {\bfseries Field Name} & {\bfseries Permissions} & {\bfseries Reset Value} & {\bfseries Attributes} \\
    \hline
    \hline
    \endfirsthead
    \hline
    \rowcolor{header-color}{\bfseries Bit(s)} & {\bfseries Field Name} & {\bfseries Permissions} & {\bfseries Reset Value} & {\bfseries Attributes} \\
    \hline
    \hline
    \endhead

\rowcolor{color-reserved}0        & Reserved   & --          & --          & \\\hline  
\rowcolor{color2}
1         & FIELD1\_NAME & RW & 0x0 & \\\hline 
\rowcolor{color2}
             & \multicolumn{4}{p{\textwidth-4\tabcolsep-3\arrayrulewidth-\widthof{\bfseries Bit(s)}}|}{
\lipsum[1-2]

} \\\hline 
\end{tabularx}


\begin{tabularx}{\linewidth}%
   {% col 1
   |p{\widthof{\bfseries Bit(s)}}|%
   l|%
   l|%
   l|%
   % col 5
   X|
   }

    \hline
    \rowcolor{header-color}{\bfseries Bit(s)} & {\bfseries Field Name} & {\bfseries Permissions} & {\bfseries Reset Value} & {\bfseries Attributes} \\
    \hline
    \hline
    \endfirsthead
    \hline
    \rowcolor{header-color}{\bfseries Bit(s)} & {\bfseries Field Name} & {\bfseries Permissions} & {\bfseries Reset Value} & {\bfseries Attributes} \\
    \hline
    \hline
    \endhead

\rowcolor{color-reserved}0        & Reserved   & --          & --          & \\\hline  
\rowcolor{color2}
1         & FIELD1\_NAME & RW & 0x0 & \\\hline 
\rowcolor{color2}
             & \multicolumn{4}{p{\textwidth-4\tabcolsep-3\arrayrulewidth-\widthof{\bfseries Bit(s)}}|}{
\lipsum[1-2]

} \\\hline 
\end{tabularx}


\end{document}
leandriis
  • 62,593
  • Thank you. It works. Is there a way to eliminate using a \widthof{\bfseries Bit(s)}} in multicolumn? If, for some reason, text in the first column is longer than the "Bit(s)", the text not be seen in full. – Michael Mar 22 '20 at 23:27
  • If you used an l type column for the first column, the with would automatically adapt to the widest entry in this column. This would however mess up the width calculation of the multicolumn. You could either change the content of the \widthof command according to the widest entry in the first column. If you have multiple multicolumns, you could also store this length in order to avoid having to edit the \widthof command on multiple places in the code. ... – leandriis Mar 23 '20 at 09:33
  • ... Alternatively, you could just use something like p{2cm} with an appropriate width of your choice and adapt the width calculation of the multicolumn accordingly. – leandriis Mar 23 '20 at 09:33
  • I see. Another problem I encountered with this approach is that following cell will exceed table and page: ` & \multicolumn{4}{p{\textwidth-4\tabcolsep-3\arrayrulewidth-\widthof{\bfseries Bit(s)}}|}{

    } \\hline `

    – Michael Mar 26 '20 at 05:01
  • @Michael: That's why I wrote "adapt the width calculation of the multicolumn accordingly". If you use p{2cm} for your first column, you will have to use \multicolumn{4}{p{\textwidth-4\tabcolsep-3\arrayrulewidth-2cm}|} for your multicolumn. If you use p{5cm} for your first column, your multicolumn should lookmlike \multicolumn{4}{p{\textwidth-4\tabcolsep-3\arrayrulewidth-5cm}|} instead. – leandriis Mar 26 '20 at 08:36