![在 Tikz 中移动表格的多边形和中心数字](https://linux22.com/image/424747/%E5%9C%A8%20Tikz%20%E4%B8%AD%E7%A7%BB%E5%8A%A8%E8%A1%A8%E6%A0%BC%E7%9A%84%E5%A4%9A%E8%BE%B9%E5%BD%A2%E5%92%8C%E4%B8%AD%E5%BF%83%E6%95%B0%E5%AD%97.png)
我对为练习创建的表格有几个疑问。
- 圆圈内的数字。如何让它们居中?
- 我想将紫色多边形稍微向左和向下移动一点。
- 有没有更简单的方法来制作表格。请记住,我使用 Tikz 在表格顶部绘图,并且我还需要进行多次迭代。
提前谢谢了!
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit,automata}
\usepackage{adjustbox}
\usepackage[utf8]{inputenc}
\begin{document}
\begin{adjustbox}{max totalsize={.9\textwidth}{.9\textheight},center}
\begin{tikzpicture}
\matrix(m) [matrix of math nodes, nodes in empty cells, nodes={minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex}]
{
1 & + & 2 & & 3 & & 5 & & 0 & - \\
&\textcolor{blue}{0}& &\textcolor{blue}{1}& &\textcolor{blue}{2}& &\textcolor{blue}{4}& &\textcolor{blue}{0}\\
0 & - & 1 & + & 5 & & 3 & & 3 & \\
&\textcolor{blue}{0}& &\textcolor{blue}{1}& &\textcolor{blue}{5}& &\textcolor{blue}{3}& &\textcolor{blue}{4}\\
3 & & 0 & & 4 & & 0 & & 3 & \\
&\textcolor{blue}{3}& &\textcolor{blue}{0}& &\textcolor{blue}{4}& &\textcolor{blue}{0}& &\textcolor{blue}{4}\\
5 & & 7 & & 0 & & 0 & & 2 & \\
&\textcolor{blue}{5}& &\textcolor{blue}{7}& &\textcolor{blue}{0}& &\textcolor{blue}{0}& &\textcolor{blue}{3}\\
3 & & 1 & - & 4 & & 5 & & 0 & + \\
&\textcolor{blue}{3}& &\textcolor{blue}{0}& &\textcolor{blue}{3}& &\textcolor{blue}{5}& &\textcolor{blue}{0}\\
};
\draw (m-1-1.north west) rectangle (m-10-10.south east);
% Lineas horizontales
\foreach \i in {2,4,6,8,10} {
\draw (m-\i-1.south west) -- (m-\i-10.south east);
}
% Lineas verticales
\foreach \j in {2,4,6,8,10} {
\draw (m-1-\j.north east) -- (m-10-\j.south east);
}
% Solucion
\node[draw=blue,circle,fit=(m-1-9)(m-2-10),inner sep = -19pt] {1};
\node[draw=blue,circle,fit=(m-3-1)(m-4-2),inner sep = -19pt] {1};
\node[draw=blue,circle,fit=(m-5-7)(m-6-8),inner sep = -19pt] {1};
\node[draw=blue,circle,fit=(m-7-5)(m-8-6),inner sep = -19pt] {1};
\node[draw=blue,circle,fit=(m-9-3)(m-10-4),inner sep = -19pt] {1};
\node[draw=blue,circle,fit=(m-1-1)(m-2-2),inner sep = -19pt] {0};
\node[draw=blue,circle,fit=(m-5-3)(m-6-4),inner sep = -19pt] {0};
\node[draw=blue,circle,fit=(m-7-7)(m-8-8),inner sep = -19pt] {0};
\node[draw=blue,circle,fit=(m-9-9)(m-10-10),inner sep = -19pt] {0};
% dual ui
\node [font=\color{red}] at (6,4) {0};
\node [font=\color{red}] at (6,2) {-1};
\node [font=\color{red}] at (6,0) {-1};
\node [font=\color{red}] at (6,-2) {-1};
\node [font=\color{red}] at (6,-4) {0};
% dual vj
\node [font=\color{red}] at (-4,-6) {1};
\node [font=\color{red}] at (-2,-6) {1};
\node [font=\color{red}] at (0,-6) {1};
\node [font=\color{red}] at (2,-6) {1};
\node [font=\color{red}] at (4,-6) {0};
% poligono
\draw [purple, dashed,very thick](m-2-1.north) -- (m-2-9.north) -- (m-10-9.north) -- (m-10-3.north) -- (m-4-3.north) -- (m-4-1.north) -- (m-2-1.north);
\end{tikzpicture}
\end{adjustbox}
\end{document}
答案1
- 加载 TikZ 库。它允许您为和之间的点
calc
写入。($(a)!0.5!(b)$)
(a)
(b)
- 将带圆圈的数字绘制为带圆圈的节点,而不是拟合节点。
- 用相对于起点的坐标定义多边形,然后可以通过移动起点来移动它。
- 使用
\foreach
循环避免重复。 - 绘制矩形多边形时,可以使用
-|
和|-
代替 来删除每个第二个节点--
。
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\begin{document}
\begin{tikzpicture}
\matrix(m) [matrix of math nodes, nodes in empty cells, nodes={minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex}]
{
1 & + & 2 & & 3 & & 5 & & 0 & - \\
&\textcolor{blue}{0}& &\textcolor{blue}{1}& &\textcolor{blue}{2}& &\textcolor{blue}{4}& &\textcolor{blue}{0}\\
0 & - & 1 & + & 5 & & 3 & & 3 & \\
&\textcolor{blue}{0}& &\textcolor{blue}{1}& &\textcolor{blue}{5}& &\textcolor{blue}{3}& &\textcolor{blue}{4}\\
3 & & 0 & & 4 & & 0 & & 3 & \\
&\textcolor{blue}{3}& &\textcolor{blue}{0}& &\textcolor{blue}{4}& &\textcolor{blue}{0}& &\textcolor{blue}{4}\\
5 & & 7 & & 0 & & 0 & & 2 & \\
&\textcolor{blue}{5}& &\textcolor{blue}{7}& &\textcolor{blue}{0}& &\textcolor{blue}{0}& &\textcolor{blue}{3}\\
3 & & 1 & - & 4 & & 5 & & 0 & + \\
&\textcolor{blue}{3}& &\textcolor{blue}{0}& &\textcolor{blue}{3}& &\textcolor{blue}{5}& &\textcolor{blue}{0}\\
};
\draw (m-1-1.north west) rectangle (m-10-10.south east);
% Lineas horizontales
\foreach \i in {2,4,6,8,10} {
\draw (m-\i-1.south west) -- (m-\i-10.south east);
}
% Lineas verticales
\foreach \j in {2,4,6,8,10} {
\draw (m-1-\j.north east) -- (m-10-\j.south east);
}
% Solucion
\foreach \a/\b in {1-9/2-10,3-1/4-2,5-7/6-8,7-5/8-6,9-3/10-4}
\node[draw=blue,circle] at ($(m-\a)!0.5!(m-\b)$) {\color{black}1};
\foreach \a/\b in {1-1/2-2,5-3/6-4,7-7/8-8,9-9/10-10}
\node[draw=blue,circle] at ($(m-\a)!0.5!(m-\b)$) {\color{black}0};
% dual ui
\foreach \a/\b in {4/0,2/-1,0/-1,-2/-1,-4/0}
\node [font=\color{red}] at (6,\a) {\b};
% dual vj
\foreach \a/\b in {4/0,2/1,0/1,-2/1,-4/1}
\node [font=\color{red}] at (\a,-6) {\b};
% poligono
% shift it by changing (-0.3,-0.3)
\draw [purple, dashed,very thick] (m-2-1.north)++(-0.3,-0.3)coordinate(X) -| ++(8,-8) -| ++(-6,6) -| (X);
\end{tikzpicture}
\end{document}