带标签的折线

带标签的折线

我目前正在绘制具有所需样式的折线,但无法更改它们的标签。我不想到处都显示“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我想指出的是,autoswap对您的代码没有影响。如果您不使用标记来绘制带圆圈的字母,它们就会有影响。

\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}

在此处输入图片描述

如果您想使用autoswap,这里有一个建议。

\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}

在此处输入图片描述

相关内容