複数列のフル幅テーブル

複数列のフル幅テーブル

複数列の全幅テーブルを作成するときに問題が発生します。

コードは次のとおりです:

\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}

残念ながら、期待していたものとは違いました。

  1. 「権限」列の幅が十分ではありません
  2. 右側に、本来あるべきではない余分なスペースがあります。

私の予想では、X右の列がテキスト幅(テーブル構成で定義されているとおり)まで拡張されるはずでした。

このテーブルをスクリプトから作成するつもりですが、列のサイズを定数として設定できません。

ここに画像の説明を入力してください

答え1

p固定列タイプまたはl列タイプのいずれかを使用した 2 つの可能性:

ここに画像の説明を入力してください

\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}

関連情報