答案1
欢迎来到 TeX.SE!类似这样的事?
\documentclass{article}
\usepackage{colortbl}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
\begin{center}
\tikzmarknode{tab}{\begin{tabular}{|c!{\color{red}\vline}c|c|c|c|}\hline
$ \star $ & 1 & 2 & 3 & 4\\
\arrayrulecolor{red}\hline\arrayrulecolor{black}
1 & 4 & 3 & 2 & 1\\ \hline
2 & 3 & 1 & 4 & 2\\ \hline
3 & 2 & 4 & 1 & 3\\ \hline
4 & 1 & 2 & 3 & 4\\ \hline
\end{tabular}}
\begin{tikzpicture}[overlay,remember picture]
\draw[thick,green!60!black,shorten <=6mm] (tab.north west) -- (tab.south east);
\end{tikzpicture}
\end{center}
\end{document}
或者用选定的线条更粗但稍微复杂一点的代码。
\documentclass{article}
\usepackage{colortbl}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\newlength{\savedwidth}
\begin{document}
\begin{center}
\tikzmarknode{tab}{\global\savedwidth\arrayrulewidth % p. 6 of colortbl docu
\arrayrulewidth0.2pt
\begin{tabular}{|c!{\arrayrulewidth0.8pt\color{red}\vline}c|c|c|c|}\hline
$ \star $ & 1 & 2 & 3 & 4\\
\noalign{\global\setlength\arrayrulewidth{0.8pt}}%
\arrayrulecolor{red}\hline\arrayrulecolor{black}\noalign{\global\arrayrulewidth0.2pt}
1 & 4 & 3 & 2 & 1\\ \hline
2 & 3 & 1 & 4 & 2\\ \hline
3 & 2 & 4 & 1 & 3\\ \hline
4 & 1 & 2 & 3 & 4\\ \hline
\end{tabular}}\global\arrayrulewidth\savedwidth
\begin{tikzpicture}[overlay,remember picture]
\draw[line width=0.8pt,green!60!black,shorten <=6mm,shorten >=1pt] (tab.north west) -- (tab.south east);
\end{tikzpicture}
\end{center}
\end{document}
答案2
{NiceTabular}
这是带有的解决方案nicematrix
(≥ 6.23 2023-09-02)。该环境类似于经典环境{tabular}
(由包提供array
),但在单元格、节点和列下创建 PGF/TikZ 节点。然后就可以在所谓的中使用 TikZ\CodeAfter
来绘制您想要的任何规则。
\documentclass{article}
\usepackage{nicematrix,tikz}
\begin{document}
\begin{center}
\begin{NiceTabular}{|c|[color=red]c|c|c|c|}\hline
$ \star $ & 1 & 2 & 3 & 4\\ \Hline[color=red]
1 & 4 & 3 & 2 & 1\\ \hline
2 & 3 & 1 & 4 & 2\\ \hline
3 & 2 & 4 & 1 & 3\\ \hline
4 & 1 & 2 & 3 & 4\\ \hline
\CodeAfter
\tikz \draw [thick, green, shorten < = -1mm ] (2-|2) -- (last-|last) ;
\end{NiceTabular}
\end{center}
\end{document}