我目前正在绘制具有所需样式的折线,但无法更改它们的标签。我不想到处都显示“1”,而是想在每条线上放置 a、b、c 或其他任何内容,同时保持简单和自动化。
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,decorations.markings}
\begin{tikzpicture}[>=stealth,
node at every point/.style={%
decoration={%
show path construction,
lineto code={%
\path [decoration={markings,
mark=at position 0 with {\fill circle [radius=1pt];},
mark=at position .5 with {\arrow{>};},
mark=at position .5 with {\node[shape=circle,draw,swap,auto,inner sep=2pt] at (0,0.4) {1};},
mark=at position 1 with {\fill circle [radius=1pt];},
}, decorate] (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
},
},
postaction=decorate
}
]
\draw [node at every point](0,0) -- (1,2) -- (2.3,0) -- (2.5,1);
\end{tikzpicture}
我也尝试过\foreach
,但无法找到任何解决方案。理想的解决方案是为节点添加第三个参数等(0,0) -- (1,2,first line label) -- (2.3,0,second line label)
。
答案1
好的,我们开始吧。它非常类似于这个答案除了我使用\alph
将数字转换为之外a
...b
我想指出的是,auto
和swap
对您的代码没有影响。如果您不使用标记来绘制带圆圈的字母,它们就会有影响。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.pathreplacing,decorations.markings}
\newcounter{coordinateindex}
\begin{document}
\begin{tikzpicture}[>=stealth,initialize counter/.code={
\setcounter{coordinateindex}{0}
},
node at every point/.style={%
decoration={show path construction,
lineto code={%
\path [decoration={markings,
mark=at position 0 with {\fill circle [radius=1pt];},
mark=at position .5 with {\arrow{>};},
mark=at position .5 with {\stepcounter{coordinateindex}
\node[shape=circle,draw,inner sep=2pt] at (0,0.4)
{\alph{coordinateindex}};},
mark=at position 1 with {\fill circle [radius=1pt];},
}, decorate] (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
},
},
postaction=decorate
}
]
\draw [node at every point] (0,0) -- (1,2) -- (2.3,0) -- (2.5,1);
\end{tikzpicture}
\end{document}
如果您想使用auto
和swap
,这里有一个建议。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.pathreplacing,decorations.markings}
\newcounter{coordinateindex}
\begin{document}
\begin{tikzpicture}[>=stealth,initialize counter/.code={
\setcounter{coordinateindex}{0}
},
node at every point/.style={%
decoration={show path construction,
lineto code={%
\path [decoration={markings,
mark=at position 0 with {\fill circle [radius=1pt];},
mark=at position .5 with {\arrow{>};},
mark=at position 1 with {\fill circle [radius=1pt];},
}, decorate] (\tikzinputsegmentfirst) --
node[pos=0.5,auto,swap,shape=circle,draw,inner sep=2pt]
{\stepcounter{coordinateindex}\vphantom{bg}\alph{coordinateindex}} (\tikzinputsegmentlast);
},
},
postaction=decorate
}
]
\draw [node at every point] (0,0) -- (1,2) -- (2.3,0) -- (2.5,1);
\end{tikzpicture}
\end{document}
(我可能会放弃交换。)
以下是输入文本数组元素的一种方法。(请注意,数组的第一个元素的索引为 0,这就是计数器比上述代码更晚的原因。)
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.pathreplacing,decorations.markings}
\newcounter{coordinateindex}
\def\mytexts{{"koala","duck","marmot","mouse","moles","penguin"}}
\begin{document}
\begin{tikzpicture}[>=stealth,initialize counter/.code={
\setcounter{coordinateindex}{0}
},
node at every point/.style={%
decoration={show path construction,
lineto code={%
\path [decoration={markings,
mark=at position 0 with {\fill circle [radius=1pt];},
mark=at position .5 with {\arrow{>};},
mark=at position 1 with {\fill circle [radius=1pt];},
}, decorate] (\tikzinputsegmentfirst) --
node[midway,above,sloped]
{\pgfmathparse{\mytexts[\thecoordinateindex]}
\pgfmathresult
\stepcounter{coordinateindex}} (\tikzinputsegmentlast);
},
},
postaction=decorate
}
]
\draw [node at every point] (0,0) -- (1,2) -- (2.3,0) -- (2.5,1);
\end{tikzpicture}
\end{document}