让单个单元格边框拥有自己的颜色

让单个单元格边框拥有自己的颜色

我在 LaTeX 中有以下表格:

\documentclass[]{article}
\begin{document}
\begin{center}
  \begin{tabular}{r *{10}{| c}}
    \cline{2-9}
    1 & & & & & & & & \cellcolor{black}  \\ \cline{2-9}
    2 & & & & & & & & \cellcolor{black} \\ \cline{2-9}
    3 & & & & & & & &  \\ \cline{2-9}
    4 & & & & & & & & \\ \cline{2-9}
    5 & & & & & & & &  \\ \cline{2-9}
    6 & & & & & & & &  \\ \cline{2-9}
    7 & & & & & & & &  \\ \cline{2-9}
    8 & & & & & & & &  \\ \cline{2-9}
    \multicolumn{1}{c}{}  & \multicolumn{1}{c}{a} & \multicolumn{1}{c}{b} & \multicolumn{1}{c}{c} & \multicolumn{1}{c}{d} & \multicolumn{1}{c}{e} & \multicolumn{1}{c}{f} & \multicolumn{1}{c}{g} & \multicolumn{1}{c}{h}
  \end{tabular}
\end{center}
\end{document}

我该如何让单个边框(不是行或列,而是特定单元格的左侧)具有特定的颜色?提前致谢。

答案1

修补hhline

\documentclass[]{article}

\usepackage{hhline,colortbl}

\begin{document}
\begin{center}
  \begin{tabular}{r *{10}{| c}}
    \cline{2-9}
    1 & & & & & & & & \cellcolor{black}  \\ \cline{2-9}
    2 & & & & & & & & \cellcolor{black} \\ \cline{2-9}
    3 & & & & & & & &  \\ \cline{2-9}
    4 & & & & & & & & \\ \cline{2-9}
    5 & & & & & & & &  \\ \cline{2-9}
    6 & & & & & & & &  \\ \cline{2-9}
    \hhline{~|-|-|>{\arrayrulecolor{red}}-|>{\arrayrulecolor{black}}-|-|-|-|-|-|}
    7 & & & \multicolumn{1}{!{\color{red}\vline}c!{\color{red}\vline}}{}  & & & & &  \\ 
        \hhline{~|-|-|>{\arrayrulecolor{red}}-|>{\arrayrulecolor{black}}-|-|-|-|-|-|}
    8 & & & & & & & &  \\ \cline{2-9}
    \multicolumn{1}{c}{}  & \multicolumn{1}{c}{a} & \multicolumn{1}{c}{b} & \multicolumn{1}{c}{c} & \multicolumn{1}{c}{d} & \multicolumn{1}{c}{e} & \multicolumn{1}{c}{f} & \multicolumn{1}{c}{g} & \multicolumn{1}{c}{h}
  \end{tabular}
\end{center}
\end{document}

在此处输入图片描述


也适用于右边缘

\documentclass[]{article}

\usepackage{hhline,colortbl}

\begin{document}
\begin{center}
  \begin{tabular}{r *{10}{| c}}
    \cline{2-9}
    1 & & & & & & & & \cellcolor{black}  \\ \cline{2-9}
    2 & & & & & & & & \cellcolor{black} \\ \cline{2-9}
    3 & & & & & & & &  \\ \cline{2-9}
    4 & & & & & & & & \\ \cline{2-9}
    5 & & & & & & & &  \\ \cline{2-9}
    6 & & & & & & & &  \\ \cline{2-9}
    \hhline{~|-|-|-|-|-|-|-|>{\arrayrulecolor{red}}-|>{\arrayrulecolor{black}}-|}
    7 & & &  & & & & & \multicolumn{1}{!{\color{red}\vline}c!{\color{red}\vline}}{}   \\ 
    \hhline{~|-|-|-|-|-|-|-|>{\arrayrulecolor{red}}-|>{\arrayrulecolor{black}}-|}
    8 & & & & & & & &  \\ \cline{2-9}
    \multicolumn{1}{c}{}  & \multicolumn{1}{c}{a} & \multicolumn{1}{c}{b} & \multicolumn{1}{c}{c} & \multicolumn{1}{c}{d} & \multicolumn{1}{c}{e} & \multicolumn{1}{c}{f} & \multicolumn{1}{c}{g} & \multicolumn{1}{c}{h}
  \end{tabular}
\end{center}
\end{document}

在此处输入图片描述


以及细胞颜色

\documentclass[]{article}

\usepackage{hhline,colortbl}

\begin{document}
\begin{center}
  \begin{tabular}{r *{10}{| c}}
    \cline{2-9}
    1 & & & & & & & & \cellcolor{black}  \\ \cline{2-9}
    2 & & & & & & & & \cellcolor{black} \\ \cline{2-9}
    3 & & & & & & & &  \\ \cline{2-9}
    4 & & & & & & & & \\ \cline{2-9}
    5 & & & & & & & &  \\ \cline{2-9}
    6 & & & & & & & &  \\ \cline{2-9}
    \hhline{~|-|-|-|-|-|-|-|>{\arrayrulecolor{red}}-|>{\arrayrulecolor{black}}-|}
    7 & & &  & & & & & \multicolumn{1}{!{\color{red}\vline}c!{\color{red}\vline}}{\cellcolor{yellow}}   \\ 
    \hhline{~|-|-|-|-|-|-|-|>{\arrayrulecolor{red}}-|>{\arrayrulecolor{black}}-|}
    8 & & & & & & & &  \\ \cline{2-9}
    \multicolumn{1}{c}{}  & \multicolumn{1}{c}{a} & \multicolumn{1}{c}{b} & \multicolumn{1}{c}{c} & \multicolumn{1}{c}{d} & \multicolumn{1}{c}{e} & \multicolumn{1}{c}{f} & \multicolumn{1}{c}{g} & \multicolumn{1}{c}{h}
  \end{tabular}
\end{center}
\end{document}

在此处输入图片描述

答案2

{NiceTabular}的环境与经典环境(由包提供)nicematrix类似,但在单元格、行和列下创建 PGF/TikZ 节点。{tabular}array

然后就可以使用这些节点与 TikZ 共同调用\CodeAfter来绘制你想要的任何规则。

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\begin{center}
  \begin{NiceTabular}{*{8}{c}}[first-col,last-row,hvlines]
    1 \\
    2 \\
    3 \\
    4 \\
    5 \\
    6 \\
    7 \\
    8 \\
      & a & b & c & d & e & f & g & h
  \CodeAfter
    \tikz \draw [red] (6-|3) -- (7-|3) ; 
  \end{NiceTabular}
\end{center}

\end{document}

第一个代码的输出

也可以使用命令\Block(由提供niceamtrix)及其键borders,在正确的位置插入命令来绘制单元格的边框。

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\begin{center}
  \begin{NiceTabular}{*{8}{c}}[first-col,last-row,hvlines]
    1 \\
    2 \\
    3 \\
    4 \\
    5 \\
    6 & & \Block[borders={tikz={color=red},right}]{}{}\\
    7 \\
    8 \\
      & a & b & c & d & e & f & g & h
  \end{NiceTabular}
\end{center}

\end{document}

上述代码的输出

相关内容