![如何在 Tikz 中移动路径,范围不起作用](https://linux22.com/image/467179/%E5%A6%82%E4%BD%95%E5%9C%A8%20Tikz%20%E4%B8%AD%E7%A7%BB%E5%8A%A8%E8%B7%AF%E5%BE%84%EF%BC%8C%E8%8C%83%E5%9B%B4%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8.png)
给出下面的示例,scope 命令不会移动我所绘制的路径。我实现此目的的唯一方法是使用显式坐标,但使用矩阵 m 中的单元格编号会容易得多。
\documentclass[border=1cm]{standalone}
\usepackage{graphicx} % Required for inserting images
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit,calc, automata}
\usepackage{listofitems}
\tikzset{cross/.style={cross out, draw=black, minimum size=2*(#1-\pgflinewidth), inner sep=0pt, outer sep=0pt},
%default radius will be 1pt.
cross/.default={10pt}}
\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}]
{
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
};
\draw (m-1-1.north west) rectangle (m-12-12.south east);
\foreach \i in {3,6,9} {
\draw (m-\i-1.south west) -- (m-\i-12.south east);
}
\foreach \j in {3,6,9} {
\draw (m-1-\j.north east) -- (m-12-\j.south east);
}
\path[draw,purple, dashed,very thick]
(3.2,-5.8) node[circle,draw=purple!80, inner sep=1pt] {+}
-- (-2.8,-5.8) node[circle,draw=purple!80, inner sep=1pt] {-}
-- (-2.8,-2.8) node[circle,draw=purple!80, inner sep=1pt] {+}
-- (3.2,-2.8) node[circle,draw=purple!80, inner sep=1pt] {-}
-- (3.2,-5.8) node {};
\begin{scope}[xshift=-5mm,yshift=-15mm]
\path[draw,purple, dashed,very thick]
(m-3-1) node[circle,draw=purple!80, inner sep=1pt] {+}
-- (m-3-10) node[circle,draw=purple!80, inner sep=1pt] {-}
-- (m-12-10) node[circle,draw=purple!80, inner sep=1pt] {+}
-- (m-12-1) node[circle,draw=purple!80, inner sep=1pt] {-}
-- (m-3-1) node {};
\end{scope}
\end{tikzpicture}
\end{document}
答案1
你可以改变画布:
\documentclass[border=1cm]{standalone}
\usepackage{graphicx} % Required for inserting images
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit,calc, automata}
\usepackage{listofitems}
\tikzset{cross/.style={cross out, draw=black, minimum size=2*(#1-\pgflinewidth), inner sep=0pt, outer sep=0pt},
%default radius will be 1pt.
cross/.default={10pt}}
\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}]
{
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
};
\draw (m-1-1.north west) rectangle (m-12-12.south east);
\foreach \i in {3,6,9} {
\draw (m-\i-1.south west) -- (m-\i-12.south east);
}
\foreach \j in {3,6,9} {
\draw (m-1-\j.north east) -- (m-12-\j.south east);
}
\path[draw,purple, dashed,very thick]
(3.2,-5.8) node[circle,draw=purple!80, inner sep=1pt] {+}
-- (-2.8,-5.8) node[circle,draw=purple!80, inner sep=1pt] {-}
-- (-2.8,-2.8) node[circle,draw=purple!80, inner sep=1pt] {+}
-- (3.2,-2.8) node[circle,draw=purple!80, inner sep=1pt] {-}
-- (3.2,-5.8) node {};
\begin{scope}[transform canvas={xshift=-5mm,yshift=-15mm}]
\path[draw,purple, dashed,very thick]
(m-3-1) node[circle,draw=purple!80, inner sep=1pt] {+}
-- (m-3-10) node[circle,draw=purple!80, inner sep=1pt] {-}
-- (m-12-10) node[circle,draw=purple!80, inner sep=1pt] {+}
-- (m-12-1) node[circle,draw=purple!80, inner sep=1pt] {-}
-- (m-3-1) node {};
\end{scope}
\end{tikzpicture}
\end{document}