我正在使用来自@Alain Matthes 的数字线代码,它运行完美,但我对它的理解不够,无法添加另一个节点,例如开始、中间、结束,而不仅仅是开始结束。有人能指出正确的包文档信息,以便我理解使用代码中包含的 pgfmathsmacro 时的 foreach 吗?任何帮助都将非常有帮助。
\documentclass{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\newcommand{\addsubnumlinetoright}[3]{%
\begin{tikzpicture}[out=45,in=135,relative,>=stealth]
\draw[<->] (#1-2,0)--(#2+2,0);
\foreach \x in {\number\numexpr#1-1\relax,...,\number\numexpr#2+1\relax}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\fill (#1,0) circle (2pt);
\fill (#2,0) circle (2pt);
\pgfmathsetmacro{\End}{#2-1}
\draw[#3,shorten >=2pt]
\foreach \i in {#1,...,\End}{%
(\i,0) to (\i+1,0)
} ;
\node[color=OrangeRed] at (#2,-0.75) {\small End};
\node[color=Cerulean] at (#1,-0.75) {\small Start};
\pgfmathsetmacro{\xtxt}{(#1+#2)/2}
\node at (\xtxt,0.5) {\small Move \number\numexpr#2-#1\relax\ units to the \emph{right}};
\end{tikzpicture}}
\newcommand{\addsubnumlinetoleft}[3]{%
\begin{tikzpicture}[out=135,in=45,>=stealth]
\draw[<->] (#2-2,0)--(#1+2,0);
\foreach \x in {\number\numexpr#2-1\relax,...,\number\numexpr#1+1\relax}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\fill (#1,0) circle (2pt);
\fill (#2,0) circle (2pt);
\pgfmathsetmacro{\End}{#2+1}
\draw[#3,shorten >=2pt]
\foreach \i in {#1,...,\End}{%
(\i,0) to (\i-1,0)
} ;
\node[color=OrangeRed] at (#2,-0.75) {\small End };
\node[color=Cerulean] at (#1,-0.75) {\small Start };
\pgfmathsetmacro{\xtxt}{(#1+#2)/2}
\node at (\xtxt,0.5) {\small Move \number\numexpr-#2+#1\relax\ units to the \emph{left}};
\end{tikzpicture}}
\begin{document}
\addsubnumlinetoright{-5}{-1}{->,color=MidnightBlue}
\addsubnumlinetoleft{2}{-4}{->,color=MidnightBlue}
\end{document}
答案1
我擅自重写了该\tikznumberline
命令。
您现在可以使用以下键来更改数字线及其标签:
start
并end
指定线上应连接的点(起点始终低于终点)。tick length
指定刻度高度的一半。overhang
定义了 左边start - 1
和 右边数轴的附加长度end + 1
。direction=left
或direction=right
指示宏箭头的方向。add edge label=<val>[<options>]<text>
可用于在<val>
具有必填项<options>
和文本的值下方添加另一个类似于“开始”和“结束”的节点<text>
。add edge dot=<val>[<options>]
<val>
在具有强制选项的值的行上添加一个点<options>
。add edge node=<val>[<options>]<text>
<val>-1
在连接和 以及<val>
强制选项<options>
和文本 的线上添加一个节点<text>
。
定义了更多键和样式,可用于改变数字线的外观:
start dot
并可end dot
用于更改 和start
处的小点end
,例如\tikznumberlineset{start dot/.append style={fill=Cerulean}
line
用于数轴和ticks
刻度(主要是颜色和线条粗细)。edge node
对于沿线的节点以及对于和edge node <val>
之间的沿线的节点(将由和添加的节点使用)。<val> - 1
<val>
add edge node
add edge label
代码
\PassOptionsToPackage{dvipsnames}{xcolor}
\documentclass[tikz]{standalone}
% arrows.meta for the uptodate arrow tips
% shapes.misc for the cross out shape
\usetikzlibrary{arrows.meta, shapes.misc, bending}
\newcommand*\tikznumberlineset{\pgfqkeys{/tikz/number line}}
\tikznumberlineset{
arrow/.style={/tikz/>={#1}},
start/.initial=1, end/.initial=5,
tick length/.initial=2pt, overhang/.initial=.75,
%
% Nodes
start-end dot/.style={% those are the dots
shape=circle, fill=black, minimum size=+4pt,
inner sep=+0pt, outer sep=+0pt, node contents=},
%% defaults for "start dot" and "end dot"
start dot/.style=number line/start-end dot,
end dot/.style =number line/start-end dot,
%% number below the ticks
value node/.style={
below=2pt, font=\footnotesize, node contents=$\tikznumberlinevalue$},
%% text above diagram
instruction node/.style={
anchor=south, yshift=5pt, font=\small, node contents=%
Move \nlvo{units text} to the \emph{\nlvo{direction text}}.},
%
% 'Start' and 'End' node, "start-end label"
% can be used for additional labels
start-end label/.style={font=\small,yshift=-.75cm},
start label/.style={number line/start-end label,
text=Cerulean,node contents=Start},
end label/.style ={number line/start-end label,
text=OrangeRed,node contents=End},
%
% Lines, can be used to change color, thickness, etc.
line/.style ={draw,<->},
ticks/.style=draw,
connection/.style={draw=MidnightBlue,out=45,in=135},
%
% direction=left or direction=right
%% the direction changes
%% * the arrow tip
%% * the text left/right in the instruction and
%% * where "Start" and "End" will be set.
direction text/.initial=, units text/.initial=,
direction/.is choice,
direction/left/.style={
connection/.append style=<-, direction text=left,
label left/.style =number line/end label,
label right/.style=number line/start label},
direction/right/.style={
connection/.append style=->, direction text=right,
label left/.style =number line/start label,
label right/.style=number line/end label},
direction=right,% default
%
% to doesn't like foreach, going to use .list
% which uses foreach internally
@toloop/.style={
/utils/exec=\def\tikznumberlinevalue{#1},% make #1 available
insert path={to[number line/edge/.try,number line/edge #1/.try] (#1,0)}},
add edge node/.style args={#1[#2]#3}{edge #1/.append style={
edge node={node
[number line/edge node/.try,number line/edge node #1/.try,#2]{#3}}}},
add edge label/.style args={#1[#2]#3}{
add edge node={#1[number line/start-end label,at end,#2]#3}},
add edge dot/.style args={#1[#2]}{
add edge node={#1[number line/start-end dot,behind path,at end,#2]}},
all dots/.style={edge/.append style={
edge node={node[number line/start-end dot,at end,#1]{}}}},
all dots/.default=}
\newcommand*\tikznumberline[1][]{%
\begin{tikzpicture}
\tikznumberlineset{#1}
\newcommand*\nlvo[1]{\pgfkeysvalueof{/tikz/number line/##1}}
\pgfmathint{\nlvo{end}-(\nlvo{start})}
\pgfkeyssetevalue{/tikz/number line/units text}{\pgfmathresult}
%
% 1. The lines and the ticks:
\path[number line/ticks] (\nlvo{start}-1-\nlvo{overhang},\nlvo{tick length})
grid[ystep=0] (\nlvo{end}+1+\nlvo{overhang},-\nlvo{tick length});
\path[number line/line] (\nlvo{start}-1-\nlvo{overhang},0)
-- (\nlvo{end}+1+\nlvo{overhang},0);
%
% 2. the black circles (these are nodes now)
% and the labels "Start" and "End"
\path (\nlvo{start},0) node (@number line@start) [number line/start dot]
node [number line/label left]
(\nlvo{end},0) node (@number line@end) [number line/end dot]
node [number line/label right];
%
% 3. the numbers below the ticks
\foreach \tikznumberlinevalue[expand list]
in {\the\numexpr\nlvo{start}-1\relax,...,\the\numexpr\nlvo{end}+1\relax}
\node at (\tikznumberlinevalue,0) [number line/value node];
%
% 4. the hopping line which is fully connected, no movetos anymore
% the last edge is available via the last edge key if one needs to add
\path[number line/connection](@number line@start)
[number line/@toloop/.list/.expanded={%
\the\numexpr\nlvo{start}+1\relax,...,\the\numexpr\nlvo{end}-1\relax}]
to[number line/last edge/.try] (@number line@end);
% 5.
\node at ({(\nlvo{start}+\nlvo{end})/2},\nlvo{tick length})
[number line/instruction node];
\end{tikzpicture}}
\tikznumberlineset{arrow=Stealth}
\tikzset{small cross out/.style={
cross out,at end,minimum size=2mm,draw=gray,ultra thick,line cap=round}}
\begin{document}
\tikznumberline[start=-5, end=-1,
add edge node={-4[small cross out]},
]
\tikznumberline[start=-4, end= 2, direction=left,
add edge label={0[green]Middle},
add edge node={-1[fill=white,font=\tiny\sffamily]Hip},
add edge node={-2[fill=white,font=\tiny\sffamily]Hop},
add edge dot={0[green]}
]
\tikznumberline[all dots=blue]
\tikznumberline[
start=-15, end=10, /tikz/x=5mm, /tikz/arrows={[bend]},
value node/.append style={rotate=90, anchor=east, inner xsep=.1em},
start dot/.append style=Cerulean, end dot/.append style=OrangeRed,
all dots={
minimum size=+3pt,
/utils/exec=\pgfmathparse{100*(\tikznumberlinevalue-\nlvo{start})/
(\nlvo{end}-\nlvo{start})},
fill/.expanded=OrangeRed!\pgfmathresult!Cerulean}
]
\end{document}
输出
答案2
像这样?
与您的代码相比,以下 MWE 中的更改标记为% <---
:
\documentclass[margin=3mm,
preview]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\newcommand{\addsubnumlinetoright}[3]{%
\begin{tikzpicture}[out=45,in=135,relative,>=stealth]
\draw[<->] (#1-2,0)--(#2+2,0);
\foreach \x in {\number\numexpr#1-1\relax,...,\number\numexpr#2+1\relax}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\fill (#1,0) circle (2pt);
\fill (#2,0) circle (2pt);
\pgfmathsetmacro{\End}{#2-1}
\draw[#3,shorten >=2pt]
\foreach \i in {#1,...,\End}{%
(\i,0) to (\i+1,0)
} ;
\pgfmathparse{(#2+#1)/2} % <---
\node[color=OrangeRed] at (#2,-0.75) {\small End};
\node[color=green] at (\pgfmathresult,-0.75) {\small Middle}; % <---
\node[color=Cerulean] at (#1,-0.75) {\small Start};
\pgfmathsetmacro{\xtxt}{(#1+#2)/2}
\node at (\xtxt,0.5) {\small Move \number\numexpr#2-#1\relax\ units to the \emph{right}};
\end{tikzpicture}}
\newcommand{\addsubnumlinetoleft}[3]{%
\begin{tikzpicture}[out=135,in=45,>=stealth]
\draw[<->] (#2-2,0)--(#1+2,0);
\foreach \x in {\number\numexpr#2-1\relax,...,\number\numexpr#1+1\relax}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\fill (#1,0) circle (2pt);
\fill (#2,0) circle (2pt);
\pgfmathsetmacro{\End}{#2+1}
\draw[#3,shorten >=2pt]
\foreach \i in {#1,...,\End}{%
(\i,0) to (\i-1,0)
} ;
\pgfmathparse{(#2+#1)/2} % <---
\node[color=OrangeRed] at (#2,-0.75) {\small End };
\node[color=green] at (\pgfmathresult,-0.75) {\small Middle}; % <---
\node[color=Cerulean] at (#1,-0.75) {\small Start };
\pgfmathsetmacro{\xtxt}{(#1+#2)/2}
\node at (\xtxt,0.5) {\small Move \number\numexpr-#2+#1\relax\ units to the \emph{left}};
\end{tikzpicture}}
\begin{document}
\addsubnumlinetoright{-5}{-1}{->,color=MidnightBlue}
\medskip\newline
\addsubnumlinetoleft{2}{-4}{->,color=MidnightBlue}
\end{document}