![TikZ 和 Tabular - 用线连接单元格](https://linux22.com/image/474464/TikZ%20%E5%92%8C%20Tabular%20-%20%E7%94%A8%E7%BA%BF%E8%BF%9E%E6%8E%A5%E5%8D%95%E5%85%83%E6%A0%BC.png)
我想用对角线连接颜色相同的单元格。如何使用 TikZ 实现?我尝试使用覆盖并记住图片,但它不知何故移动了整个表格。
\begin{center}
\begin{tabular}{c|c}
D & I\\
\hline
\color{red}$x^2$ & $\cos x$\\
\color{blue}$-2x$ & \color{red}$\sin x$ \\
\color{orange}$2$ & \color{blue}$-\cos x$ \\
$0$& \color{orange}$-\sin x$ \\
\end{tabular}
\end{center}
答案1
该包使得在矩阵内nicematrix
使用命令变得容易。tikz
\documentclass{article}
\usepackage{nicematrix, tikz}
\begin{document}
\[
\NiceMatrixOptions{cell-space-limits = 3pt}
\begin{NiceArray}{c|c}
D & I\\ \hline
\color{red}x^2 & \cos x\\
\color{blue}-2x & \color{red}\sin x \\
\color{orange}2 & \color{blue}-\cos x \\
0 & \color{orange}-\sin x
\CodeAfter
\tikz{
\draw[red] (2-1.east)--(3-2.west);
\draw[blue] (3-1.east)--(4-2.west);
\draw[orange] (4-1.east)--(5-2.west);
\draw (5-1.east)--(2-2.west);
}
\end{NiceArray}
\]
\end{document}