我的自定义绘制线不是指向节点而是指向其他地方,我不知道为什么以及如何修复它:
\documentclass{standalone}
\usepackage{tikz}
\tikzset{
every label/.style={
font=\scriptsize,
inner sep=1pt
}
}
\newcommand{\stencilptreg}[4][]{\node[circle,fill,draw,inner sep=1.5pt,label={below:#4},#1] at (#2) (#3) {}}
\newcommand{\stencilptstg}[4][]{\node[circle,fill,white,draw=black,inner sep=1.5pt,label={below:#4},#1] at (#2) (#3) {}}
\newcommand{\difflinefw}[2]{\draw [red,->,shorten <= 0.1cm, shorten >= 0.2cm] (#1) to[out=30,in=150] (#2)}
\newcommand{\difflinebw}[2]{\draw [red,->,shorten <= 0.1cm, shorten >= 0.2cm] (#1) to[out=150,in=30] (#2)}
\begin{document}
\begin{tikzpicture}[x=2cm,y=2cm]
% First point
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfmathsetmacro\j{0}
\draw [black!50,thin] (0,\j) -- (5,\j);
\foreach \i in {0,1,...,5} {
\stencilptreg{\i,\j}{\i,\j}{$\i$};
}
\foreach \i in {0.5,1.5,...,4.5} {
\stencilptstg{\i,\j}{\i,\j}{$\i$};
}
\stencilptstg[blue]{.5, \j}{}{};
\difflinefw{0,\j}{0.5,\j};
\difflinebw{1,\j}{0.5,\j};
% SECOND POINT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfmathsetmacro\j{1}
\draw [black!50,thin] (0,\j) -- (5,\j);
\foreach \i in {0,1,...,5} {
\stencilptreg{\i,\j}{\i,\j}{$\i$};
}
\foreach \i in {0.5,1.5,...,4.5} {
\stencilptstg{\i,\j}{\i,\j}{$\i$};
}
\stencilptreg[blue]{1, \j}{}{};
\difflinefw{0.5,\j}{1,\j};
\difflinebw{1.5,\j}{1,\j};
% Third POINT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfmathsetmacro\j{2}
\draw [black!50,thin] (0,\j) -- (5,\j);
\foreach \i in {0,1,...,5} {
\stencilptreg{\i,\j}{\i,\j}{$\i$};
}
\foreach \i in {0.5,1.5,...,4.5} {
\stencilptstg{\i,\j}{\i,\j}{$\i$};
}
\stencilptreg[blue]{1.5, \j}{}{};
\difflinefw{1,\j}{1.5,\j};
\difflinebw{2,\j}{1.5,\j};
\end{tikzpicture}
\end{document}
我还想使用数学来迭代 $j$,但我需要写类似这样的内容:
\foreach \j in {0,...,9} {
...
\stencilptreg[blue]{\j/2, \j}{}{};
\difflinefw{(\j+1)/2-.5,\j}{(\j+1)/2-.5,\j};
\difflinebw{(\j+1)/2+.5,\j}{(\j+1)/2+.5,\j};
}
但我不知道如何进行数学运算。
不需要但是想要,有没有办法将浮点数转换为分数?
答案1
问题是,当你执行 时,你给节点赋予了与坐标相同的名称\stencilptreg{\i,\j}{\i,\j}{$\i$};
,如果你改用\stencilptreg{\i,\j}{\i-\j}{$\i$};
,这种混乱就会消失。当然,如果节点名称包含 ,就会出现问题.
。这里有一个解决所有这些问题的建议,并执行循环。
\documentclass[tikz,border=3.14mm]{standalone}
\tikzset{
every label/.style={
font=\scriptsize,
inner sep=1pt
}
}
\newcommand{\stencilptreg}[4][]{\node[circle,fill,draw,inner sep=1.5pt,label={below:#4},#1] at (#2) (#3) {}}
\newcommand{\stencilptstg}[4][]{\node[circle,fill,white,draw=black,inner sep=1.5pt,label={below:#4},#1] at (#2) (#3) {}}
\newcommand{\difflinefw}[2]{\draw [red,->,shorten <= 0.1cm, shorten >= 0.2cm] (#1) to[out=30,in=150] (#2)}
\newcommand{\difflinebw}[2]{\draw [red,->,shorten <= 0.1cm, shorten >= 0.2cm] (#1) to[out=150,in=30] (#2)}
\begin{document}
\begin{tikzpicture}[x=2cm,y=2cm]
\foreach \j [evaluate=\j as \k using {int(\j/2)},
evaluate=\j as \l using {int((\j+2)/2)}] in {0,...,8}
{\draw [black!50,thin] (0,\j) -- (5,\j);
\foreach \i in {0,1,...,5} {
\stencilptreg{\i,\j}{A-\i-\j}{$\i$};
}
\foreach \i in {1,3,...,9} {
\pgfmathsetmacro{\m}{\i/2}
\stencilptstg{\i/2,\j}{B-\i-\j}{$\m$};
}
\stencilptstg[blue]{{(\j+1)/2},\j}{target-\j}{};
\ifodd\j
\pgfmathtruncatemacro{\m}{\j+2}
\difflinefw{B-\j-\j}{target-\j};
\difflinebw{B-\m-\j}{target-\j};
\else
\difflinefw{A-\k-\j}{target-\j};
\difflinebw{A-\l-\j}{target-\j};
\fi}
\end{tikzpicture}
\end{document}