
かなり複雑なテーブルがあり、境界線とパディングを追加する必要がありました。これらの「特別な」セルに背景で色を付けたいのですが、色がパディングまで広がりません。拡張する方法はありますか? これは私の MWE コードです:
\documentclass{report}
\usepackage[table]{xcolor}
\begin{document}
\begin{tabular}{cccc}
a & b & & d \\
& & & \\ \cline{1-2}
\multicolumn{1}{|@{\hspace{3em}}c@{\hspace{1em}}}{\cellcolor{red!50} E }
&
\multicolumn{1}{@{}c@{\hspace{3em}}|}{\cellcolor{red!50} F }
& & h
\\[15pt] \cline{1-2}
\end{tabular}
\end{document}
そして、それは次のように表示されます。
次の画像の黄色でハイライトされた部分も色付けされるはずです。さらに、上部の境界線が欠けていることに気付きました。なぜでしょうか? どうすれば彼女を取り戻せますか?
答え1
これを取得するには 2 つの方法があります。hhline
色付きのセルには注意が必要です。
\documentclass{report}
\usepackage[table]{xcolor}
\usepackage{booktabs, hhline}
\begin{document}
\begin{tabular}{cccc}
a & b & & d \\
& & & \\ \hhline{--~~}
\multicolumn{1}{| @{\color{red!50}\vrule width 3em}c@{\color{red!50}\vrule width 1em}}{\cellcolor{red!50} E }
&
\multicolumn{1}{@{}c@{\color{red!50}\vrule width 3em}|}{\cellcolor{red!50} F }
& & h
\\[15pt] \cline{1-2}
\end{tabular}
\vspace{1cm}
\begin{tabular}{cccc}
a & b & & d \\
& & & \\ \hhline{--~~}%
\multicolumn{1}{|@{\hspace{3em}}>{\columncolor{red!50}[3em][1em]}c@{\hspace{1em}}}{ E }
&
\multicolumn{1}{@{}>{\columncolor{red!50}[0.4pt][3em]}c@{\hspace{3em}}|}{ F }
& & h
\\[15pt] \cline{1-2}
\end{tabular}
\end{document}
答え2
通常、私は tikz と \tikzmark を使用してこのようなことを行います。水平座標は通常、取得するのは非常に簡単です。垂直座標は、場合によっては計算や \tikzmark の追加が必要になります。便利なのは、表の前に背景を描画できるため、常に背景に表示されることです。
\documentclass{report}
\usepackage[table]{xcolor}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
\tikz[overlay,remember picture]
\fill[red!50] ([yshift=-\dimexpr\dp\strutbox+15pt]pic cs:A)
rectangle ([yshift=\ht\strutbox]pic cs:B);
\begin{tabular}{cccc}
a & b & & d \\
& & & \\ \cline{1-2}
\multicolumn{1}{|@{\tikzmark{A}\hspace{3em}}c@{\hspace{1em}}}{ E }
&
\multicolumn{1}{@{}c@{\hspace{3em}\tikzmark{B}}|}{ F }
& & h
\\[15pt] \cline{1-2}
\end{tabular}
\end{document}
答え3
{NiceTabular}
環境{tabular}
とキーの代わりに環境を使用するとcolortbl-like
( の構文自体colortbl
は使用されませんが、 が使用されますcolortbl
)、期待される出力が直接得られます。
\documentclass{report}
\usepackage{xcolor}
\usepackage{nicematrix}
\begin{document}
\begin{NiceTabular}{cccc}[colortbl-like]
a & b & & d \\
& & & \\ \cline{1-2}
\multicolumn{1}{|@{\hspace{3em}}c@{\hspace{1em}}}{\cellcolor{red!50} E }
&
\multicolumn{1}{@{}c@{\hspace{3em}}|}{\cellcolor{red!50} F }
& & h
\\[15pt] \cline{1-2}
\end{NiceTabular}
\end{document}
複数のコンパイルが必要です (nicematrix
内部で PGF/Tikz ノードが使用されるため)。