是否可以自动为网格的所有交叉点命名,以便我可以从特定的交叉点绘制到另一个交叉点。
在我的例子中,我手动给一些交叉点的坐标命名以说明我的观点。
所以我想从交点 a 到交点 b 绘制,而不是从位置 (1,1) 到 (2,2);从交点 x 到交点 z 绘制,而不是从位置 (3,3) 到 (4,3)
这可以在不使用额外包的情况下完成吗,因为出于安全原因,我无法安装新包(在我的办公电脑上)。
\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{N}
\begin{center}
\begin{tikzpicture}
\draw[step=.5cm,color=gray] (0,0) grid (8,8);
\draw<2->[ultra thick, green, -latex] (1,1) -- (2,2);
\draw[blue!40!black, thick, fill=blue!40!white, opacity=.5] (3,3) circle (.2cm) node (c){};
\draw<3->[ultra thick, red, -latex] (c.center) -- (4,3);
\node at (1,1)(a){a};
\node at (2,2)(b){b};
\node at (3,3)(x){x};
\node at (4,3)(z){z};
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}
答案1
第二次尝试。我希望我理解了你的意思:我在这里做的是手动构建网格,并自动命名交叉点。你可以更改网格参数(x 和 y 步长),其他一切都保持不变;交叉点称为g-<column>-<row>
。
另一种可能性是使用 TikZmatrix of nodes
来实现这一点。
\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{N}
\begin{center}
\begin{tikzpicture}
% \draw[step=.5cm,color=gray] (0,0) grid (8,8);
\def\mystepx{0.8cm}
\def\mystepy{0.5cm}
\foreach \x in {0,...,8} {
% draw rows and columns
\draw [red] (\x*\mystepx, 0) -- (\x*\mystepx,8*\mystepy);
\draw [blue] (0,\x*\mystepy) -- (8*\mystepx,\x*\mystepy);
\foreach \y in {0,...,8} {
\coordinate (g-\x-\y) at (\x*\mystepx,\y*\mystepy);
}
}
%
\node at (g-1-1){a};
\node at (g-2-2){b};
\node at (g-3-3){x};
\node at (g-4-3){z};
\draw<2->[ultra thick, green, -latex] (g-1-1) -- (g-2-2);
\draw[blue!40!black, thick, fill=blue!40!white, opacity=.5] (g-3-3) circle (.2cm) node (c){};
\draw<3->[ultra thick, red, -latex] (c.center) -- (g-4-3);
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}
答案2
如果你从步骤中移除单元,那么\begin{tikzpicture}[x=2cm,y=2cm]
这样做就会增加网格的步骤,同时保持相同的交叉点(或格节点)保持连接。
\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{N}
\begin{center}
\begin{tikzpicture}[x=2cm,y=2cm]
\draw[step=.5,color=gray] (0,0) grid (5,4);
\draw<2->[ultra thick, green, -latex] (1,1) -- (2,2);
\draw[blue!40!black, thick, fill=blue!40!white, opacity=.5] (3,3) circle (.2cm) node (c){};
\draw<3->[ultra thick, red, -latex] (c.center) -- (4,3);
\node at (1,1){a};
\node at (2,2){b};
\node at (3,3){x};
\node at (4,3){z};
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}
答案3
to path
以下是为网格的所有交叉点命名的样式示例。设置后named grid=test
,交叉点将被命名为test-1-1
、test-1-2
、... 等等。
\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{calc}
% The x-step and the y-step lengths
\newlength{\dx}\setlength{\dx}{1cm}
\newlength{\dy}\setlength{\dy}{1cm}
% recover the steps
\makeatletter
\let\pgf@pathgrid@original\pgf@pathgrid
\def\pgf@pathgrid[#1]#2#3{%
\pgfset{#1}%
\pgfmathsetlength\dx{\pgfkeysvalueof{/pgf/stepx}}%
\global\dx=\dx
\pgfmathsetlength\dy{\pgfkeysvalueof{/pgf/stepy}}%
\global\dy=\dy
\pgf@pathgrid@original[#1]{#2}{#3}
}
\makeatother
% define the `named grid` style
\tikzset{
named grid/.style={
to path={
(\tikztostart) grid (\tikztotarget)
let \p1=(\tikztostart), \p2=(\tikztotarget),
\n1={min(\x1,\x2)},\n2={max(\x1,\x2)},
\n3={ceil(\n1/\dx)},\n4={\n3+1},\n5={int(\n2/\dx)},
\n6={min(\y1,\y2)},\n7={max(\y1,\y2)},
\n8={ceil(\n6/\dy)},\n9={\n8+1},\n{10}={int(\n7/\dy)}
in
foreach[count=\nx from 0] \x in {\n3,\n4,...,\n5}{
foreach[count=\ny from 0] \y in {\n8,\n9,...,\n{10}}{
(\x*\dx,\y*\dy) coordinate (#1-\nx-\ny) % <- name the intersections
}
}
}
},
named grid/.default=grid
}
\begin{document}
\begin{tikzpicture}
\draw[rotate=35,xstep=.7,ystep=.35] (-.5,-1.5) to[named grid=test] (3.5,1.5)
(test-1-1) edge[-latex,red,thick] (test-5-7)
(test-0-0) circle(3pt) (test-5-8) circle(3pt);
\end{tikzpicture}
\end{document}
注意:
- 此代码不太稳定:它不适用于非线性变换。
- 奇怪的是,对我来说最困难的部分是恢复步伐。