我想在数组中某一列/行的条目上绘制垂直/水平线。我找到了绘制水平线的方法在数组中某一行的条目上绘制一条水平线。但是当我用同样的方式画垂直线时。效果不太好。我的代码是:
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawLine}[3][]{%
\begin{tikzpicture}[overlay,remember picture]
\draw [#1] ($(#2)+(0,0.6ex)$) -- ($(#3)+(0,0.6ex)$);
\end{tikzpicture}%
}%
\begin{displaymath}
\left[
\begin{array}{cccc}
\tikzmark{11}{15} & 0 & \tikzmark{13}{0} &0\tikzmark{14} \\
0 & 50 & 20 & 25 \\
35 & 5 & 0 & 10\\
\tikzmark{41}{0} & 65 & \tikzmark{43}{50} & 65
\end{array}
\right].
\DrawLine[black,thick]{11}{14}
\DrawLine[black,thick]{11}{41}
\DrawLine[black,thick]{13}{43}
\end{displaymath}
结果如下
但我想要的结果是
有人知道如何解决这个问题吗?谢谢。
答案1
以下是画一条穿过矩阵一列的线:
笔记:
- 这确实需要两次运行。第一次确定位置,第二次进行绘图。
- 另一个选择是使用
tikzmark
包,但对于这种特殊情况,更简单的是不是使用该包:自定义定义\MyTikzmark
提供锚点.north
和,.south
它们自动垂直居中。类似地,.east
和.west
垂直居中。在tikzmark
包版本中,线条通过shorten >=
和进行扩展,shorten <=
并使用进行移动yshift
以提供类似的结果。
代码:无tikzmark
包装。
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\newcommand{\MyTikzmark}[2]{%
\tikz[overlay,remember picture,baseline] \node [anchor=base] (#1) {$#2$};%
}
\newcommand{\DrawVLine}[3][]{%
\begin{tikzpicture}[overlay,remember picture]
\draw[shorten <=0.3ex, #1] (#2.north) -- (#3.south);
\end{tikzpicture}
}
\newcommand{\DrawHLine}[3][]{%
\begin{tikzpicture}[overlay,remember picture]
\draw[shorten <=0.2em, #1] (#2.west) -- (#3.east);
\end{tikzpicture}
}
\begin{document}
\[
\begin{bmatrix}
\MyTikzmark{leftA}{15} & 0 & \MyTikzmark{topB}{0} & \MyTikzmark{rightA}{0} \\
0 & 50 & 20 & 25 \\
35 & 5 & 0 & 10 \\
\MyTikzmark{bottomA}{0} & 65 & \MyTikzmark{bottomB}{50} & 65
\end{bmatrix}
\]
\DrawVLine[red, thick, opacity=0.5]{leftA}{bottomA}
\DrawVLine[orange, thick, opacity=0.5]{topB}{bottomB}
\DrawHLine[blue, thick, opacity=0.5]{leftA}{rightA}
\end{document}
代码:带tikzmark
包装。
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc,tikzmark}
\newcommand{\MyTikzmark}[2]{%
\tikz[remember picture,baseline]
\node [anchor=base, inner sep=0pt, outer sep=0pt]
{\tikzmark{#1 LEFT}$#2$\tikzmark{#1 RIGHT}};%
}
\newcommand{\DrawVLine}[3][]{%
\begin{tikzpicture}[overlay,remember picture]
\draw[shorten <=-1.7ex, shorten >=-0.3ex, #1]
($(pic cs:#2 LEFT)!0.5!(pic cs:#2 RIGHT)$) --
($(pic cs:#3 LEFT)!0.5!(pic cs:#3 RIGHT)$);
\end{tikzpicture}
}
\newcommand{\DrawHLine}[3][]{%
\begin{tikzpicture}[overlay,remember picture]
\draw[shorten <=-0.2em, shorten >=-0.3em, yshift=0.7ex, #1]
(pic cs:#2 LEFT) -- (pic cs:#3 RIGHT);
\end{tikzpicture}
}
\begin{document}
\[
\begin{bmatrix}
\MyTikzmark{leftA}{15} & 0 & \MyTikzmark{topB}{0} & \MyTikzmark{rightA}{0} \\
0 & 50 & 20 & 25 \\
35 & 5 & 0 & 10 \\
\MyTikzmark{bottomA}{0} & 65 & \MyTikzmark{bottomB}{50} & 65
\end{bmatrix}
\]
\DrawVLine[red, thick, opacity=0.5]{leftA}{bottomA}
\DrawVLine[orange,thick, opacity=0.5]{topB}{bottomB}
\DrawHLine[blue, thick, opacity=0.5]{leftA}{rightA}
\end{document}
答案2
这里实际上不需要 TikZ,尽管我不会假装这个解决方案非常强大。
\documentclass{article}
\begin{document}
$$\left[\begin{array}{cccc}
\smash{\vrule width .5pt depth 42pt height 4pt}&&
\smash{\vrule width .5pt depth 42pt height 4pt}\\[-8pt]
\hbox to 0pt{\vrule width 1in depth -3pt height 3.5pt\hss}
15&0&0&0\\
0&50&20&25\\
35&5&0&10\\
0&65&50&65
\end{array}\right]$$
\end{document}
答案3
我会使用不同的方法来做这件事matrix of math nodes
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{center}
\begin{tikzpicture}
\matrix (M)[matrix of math nodes,left delimiter={[},right delimiter={]}]{
15 & 0 & 0 & 0\\
0 & 50 & 20 & 25 \\
35 & 5 & 0 & 10\\
0 & 65 & 50 & 65\\
};
\draw[thick,black](M-1-1.west)--(M-1-4.east);
\draw[thick,black](M-1-1.north)--(M-4-1.south);
\draw[thick,black](M-1-3.north)--(M-4-3.south);
\end{tikzpicture}
\end{center}
\end{document}
参数(M)
为\matrix
意味着您可以将节点引用为(M-row-col)
。
请注意,您需要将分隔符转义为{[}
和{]}
。这得到:
答案4
我也想尝试一下。我尝试计算尽可能多的内容,但仍然需要一些调整因子来将线条垂直居中在数字上。至少它应该与字体大小无关。
\documentclass{article}
\usepackage{mathtools}
\newsavebox{\tempbox}
\newlength{\tempwidth}
\newlength{\tempheight}
% drwa horizontal line across matrix
\newcommand{\hmline}{\rlap{\rule[.6ex]{\tempwidth}{.5ex}}}
% draw vertical line up from bottom row of matrix
\newcommand{\vmline}[1]% #1 = comun entry (bottom row)
{\begingroup%
\settowidth{\tempwidth}{$#1$}%
\addtolength{\tempwidth}{-.5ex}% line width
\rlap{\hspace{.5\tempwidth}\raisebox{-.8ex}[0pt][0pt]{\rule{.5ex}{\tempheight}}}#1%
\endgroup}
\begin{document}
% compute size of matrix (no brackets}
\savebox{\tempbox}{$\displaystyle\begin{matrix}
15 & 0 & 0 & 0 \\
0 & 50 & 20 & 25 \\
35 & 5 & 0 & 10 \\
0 & 65 & 50 & 65
\end{matrix}$}%
\settoheight{\tempheight}{\usebox{\tempbox}}%
\settodepth{\tempwidth}{\usebox{\tempbox}}%
\addtolength{\tempheight}{\tempwidth}%
\settowidth{\tempwidth}{\usebox{\tempbox}}%
\[
\begin{bmatrix}
\hmline
15 & 0 & 0 & 0 \\
0 & 50 & 20 & 25 \\
35 & 5 & 0 & 10 \\
\vmline{0} & 65 & \vmline{50} & 65
\end{bmatrix}
\]
\end{document}