表の行の色がテキストを覆います

表の行の色がテキストを覆います

xcolorオプション付きのパッケージを使用して、行の色が交互に変わるテーブルを作成しようとしています[table]。次の例は、行の色付け ( のコメント解除) によってテーブルの内容が隠れたり覆われたりする可能性があることを示しています\rowcolors..。特に、@ 式に問題があるようです。

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}

\begin{document}

\begin{table}
    \footnotesize
    \centering
    %\rowcolors{2}{gray!15}{white}
    \begin{tabular}{lr@{\hspace{.1em}}c@{\hspace{.1em}}lcc}
        &&&& Col 1 & Col 2 \\ \toprule
        Row 1 & $(100$ & $\times$ & $100)$    & $1$ & $2$ \\
        Row 2 & $(100$ & $\times$ & $1000)$& $3$ & $4$ \\
     \bottomrule
\end{tabular}
\end{table}

\end{document}
  1. どうすればそれを防ぐことができるでしょうか?
  2. 2 番目の質問は、線全体を色で覆う簡単な方法があるかどうかです。灰色の線の中に白いパディングがあることがわかります。

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

答え1

最初の質問では、\tabcolsep次のように2つの列を別々に削除できます。

r<{\hspace{-\tabcolsep}}>{\hspace{-\tabcolsep}\,}c
                    <{\hspace{-\tabcolsep}\,}>{\hspace{-\tabcolsep}}lcc}

2番目は、\bottomrulec次のようなものを定義できます

\newcommand{\bottomrulec}{%
  \arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
  \arrayrulecolor{black}\specialrule{\heavyrulewidth}{0pt}{0pt}
  \arrayrulecolor{black}
}

の代わりに を使用してください。必要に応じて、と\bottomruleの色付きバージョンを以下に示します。\toprule\midrule

\newcommand{\toprulec}{%
  \arrayrulecolor{black}\specialrule{\heavyrulewidth}{\aboverulesep}{0pt}
  \arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
  \arrayrulecolor{black}
}
\newcommand{\midrulec}{%
  \arrayrulecolor{gray!15}\specialrule{\aboverulesep}{0pt}{0pt}
  \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{\belowrulesep}
}

コードが変更されました:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\newcommand{\bottomrulec}{% Coloured \toprule
  \arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
  \arrayrulecolor{black}\specialrule{\heavyrulewidth}{0pt}{0pt}
  \arrayrulecolor{black}
}

\begin{document}

\begin{table}
    \footnotesize
    \centering
    \rowcolors{2}{gray!15}{white}
    \begin{tabular}{lr<{\hspace{-\tabcolsep}}>{\hspace{-\tabcolsep}\,}c
                        <{\hspace{-\tabcolsep}\,}>{\hspace{-\tabcolsep}}lcc}
        &&&& Col 1 & Col 2 \\ \toprule
        Row 1 & $(100$ & $\times$ & $100)$    & $1$ & $2$ \\
        Row 2 & $(100$ & $\times$ & $1000)$& $3$ & $4$ \\
     \bottomrulec
\end{tabular}
\end{table}

\end{document}

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

答え2

空白は、booktabs表の罫線の上下に垂直方向のパディングを追加することで発生します。回避策としては、このパディングを$0$ ptに設定し、パッケージで垂直方向のスペースに置き換えることですcellspace最小限 このような垂直間隔は と互換性があります\rowcolors(列指定子の前には文字 が付く必要がありますS)。

の問題については、とを使用して@{}、2 列目のセルを離れるときと 3 列目に入るときに負の水平間隔を追加することに置き換えます。>{}<{}

\documentclass[preview]{article}
\usepackage{mathtools}
\usepackage{tabularx, booktabs, caption, array}
\usepackage{colortbl}
\usepackage[table]{xcolor}
\usepackage{cellspace}
\setlength\cellspacetoplimit{6pt}
\setlength\cellspacebottomlimit{6pt}

    \begin{document}

\begin{table}
\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt}
 \rowcolors{2}{gray!15}{white}% <{\hskip-\arraycolsep}>{\hskip-\arraycolsep{}}
    $ \begin{array}{Slr <{{}\hskip-\arraycolsep}>{\hskip-\arraycolsep\mkern-.5mu}lcc}
        && & \text{Col 1} & \text{Col 2} \\
        \toprule
        \text{Row 1} & (100 \times{} & 100) & 1 & 2 \\
        \text{Row 2} & (100 \times{} &1000) & 3 & 4 \\
        \bottomrule
    \end{array} $
\end{table}

\end{document} 

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

答え3

{NiceTabular}の環境は、nicematrixと同様のツールを提供しますcolortblが、描画には PGF/Tikz を使用します。

その環境を使用すると、必要なものを直接入手できます (ただし、nicematrixPGF/Tikz ノードを使用するため、複数のコンパイルが必要になります)。

\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{booktabs}

\begin{document}

\begin{table}
\footnotesize
\centering

\begin{NiceTabular}{lr@{\hspace{.1em}}c@{\hspace{.1em}}lcc}
\CodeBefore
   \rowcolors{2}{gray!15}{}
\Body
   &&&& Col 1 & Col 2 \\ \toprule
   Row 1 & $(100$ & $\times$ & $100)$    & $1$ & $2$ \\
   Row 2 & $(100$ & $\times$ & $1000)$& $3$ & $4$ \\
\bottomrule
\end{NiceTabular}
\end{table}

\end{document}

上記コードの出力

関連情報