我试图从矩阵中删除一列和一行,如果我tikzmark
自己定义它(来自我在互联网上找到的帖子),它就会工作。然而,我明白,这tikzmark
现在默认包含在 tikz 库中,所以我尝试使用它。但我得到了未定义名称的错误。这是 MWE:
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc, tikzmark}
%% draw horizontal and vertical lines in tables
%\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,baseline] \node [anchor=base] (#1) {$#2$};}
\newcommand{\DrawVLine}[3][]{%
\begin{tikzpicture}[overlay,remember picture]
\draw[#1] (#2.north) -- (#3.south);
\end{tikzpicture}
}
\newcommand{\DrawHLine}[3][]{%
\begin{tikzpicture}[overlay,remember picture]
\draw[#1] (#2.west) -- (#3.east);
\end{tikzpicture}
}
\begin{document}
\begin{equation*}
\begin{bmatrix}
\tikzmark{top}{ a } & 0 & -a & \tikzmark{right}{-c} \\
0 & b & 0 & -b \\
-a & 0 & a & k \\
\tikzmark{bottom}{d} & -b & d & c \\
\end{bmatrix}
\end{equation*}
\DrawVLine[red, ultra thick, opacity=0.5]{top}{bottom}
\DrawHLine[red, ultra thick, opacity=0.5]{top}{right}
\end{document}
这不起作用。但是如果你取消注释定义tikzmark
并将其从中删除,\usetikzlibrary
那么我就会得到预期的结果:
答案1
我认为你没有tikzmark
正确使用库。首先,tikzmark 命令需要一个强制参数(而不是注释掉的版本中的两个参数)和一个选项。更重要的是,要引用坐标您需要使用pic cs:
。
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc,tikzmark}
%% draw horizontal and vertical lines in tables
%\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,baseline] \node [anchor=base] (#1) {$#2$};}
\newcommand{\DrawVLine}[3][]{%
\begin{tikzpicture}[overlay,remember picture]
\draw[#1] (pic cs:#2) -- (pic cs:#3);
\end{tikzpicture}
}
\newcommand{\DrawHLine}[3][]{%
\begin{tikzpicture}[overlay,remember picture]
\draw[#1] (pic cs:#2) -- (pic cs:#3);
\end{tikzpicture}
}
\begin{document}
\begin{equation*}
\begin{bmatrix}
\tikzmark{top}a & 0 & -a & -c\tikzmark{right} \\
0 & b & 0 & -b \\
-a & 0 & a & k \\
\tikzmark{bottom}d & -b & d & c \\
\end{bmatrix}
\end{equation*}
\DrawVLine[red, ultra thick, opacity=0.5]{top}{bottom}
\DrawHLine[red, ultra thick, opacity=0.5]{top}{right}
\end{document}
如你所见,这做可以工作,但输出结果并不理想。也许最好的方法是调用\tikzmark
命令\tikznode
,然后按照建议使用它(使用基线选项,如在\tikznode
命令中可以找到的,例如这里)。
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\newcommand{\tikznode}[2]{%
\ifmmode%
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$#2$};%
\else
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {#2};%
\fi}
%% draw horizontal and vertical lines in tables
%\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,baseline] \node [anchor=base] (#1) {$#2$};}
\newcommand{\DrawVLine}[3][]{%
\begin{tikzpicture}[overlay,remember picture]
\draw[#1] (#2.north) -- (#3.south);
\end{tikzpicture}
}
\newcommand{\DrawHLine}[3][]{%
\begin{tikzpicture}[overlay,remember picture]
\draw[#1] (#2.west) -- (#3.east);
\end{tikzpicture}
}
\begin{document}
\begin{equation*}
\begin{bmatrix}
{}\tikznode{top}{a} & 0 & -a & {}\tikznode{right}{-c} \\
0 & b & 0 & -b \\
-a & 0 & a & k \\
{}\tikznode{bottom}{d} & -b & d & c \\
\end{bmatrix}
\end{equation*}
\DrawVLine[red, ultra thick, opacity=0.5]{top}{bottom}
\DrawHLine[red, ultra thick, opacity=0.5]{top}{right}
\end{document}
我要强调的是,该\tikzmark
命令相当复杂,因为它区分是在内部调用还是外部调用tikzpicture
。
答案2
我个人认为使用\matrix
Tikz 中的环境更简单,您可以在其中为整个矩阵分配一个名称,进而为节点分配一个名称。
输出
代码
\documentclass[margin=1cm]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}
\newcommand{\DrawV}[3][]{%
\draw[#1, line width=3mm, opacity=.2] (#2.north) -- (#3.south);
}
\newcommand{\DrawH}[3][]{%
\draw[#1, line width=3mm, opacity=.2] (#2.west) -- (#3.east);
}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes,left delimiter={[}, right delimiter={]}] {%
a & 0 & -a & -c \\
0 & b & 0 & -b \\
-a & 0 & a & k \\
d & -b & d & c \\
};
\DrawV[red]{m-1-1}{m-4-1}
\DrawH[green]{m-1-2}{m-1-4}
\end{tikzpicture}
\end{document}