TikZ-计算折线的中间点

TikZ-计算折线的中间点

在下面的代码中,我错误地将文本放在折线中间下方。我想自动执行此操作:参数-.75用于++(0,-.75)绘制折线。实现此操作的最佳方法是什么?

\documentclass[12pt]{article}

\usepackage{tikz}
\usetikzlibrary{automata, positioning, arrows.meta, calc}

\tikzset{
    ->,
    >=Stealth, 
    node distance=3cm,
    every state/.style={thick}, 
    initial text =,
}

\newenvironment{tikzautomata}{
    \begin{center}
    \begin{tikzpicture}[shorten >=1pt, on grid, auto]
}{
    \end{tikzpicture}
    \end{center}
}

\begin{document}

    \begin{tikzautomata}
        \node[state] 
            (0) {$0$};
        \node[state, right of=0] 
            (1) {$1$};
        \node[state, right of=1]
            (2) {$2$};
        \node[state, right of=2]
            (3) {$3$};
        \node[state, right of=3]
            (4) {$4$};
        \node[state, right of=4]
            (5) {$5$};
    
        % Poor positioning by hand... :-(
        \draw
            (0.south) |- ++(0,-.75) -| (5.south);
        \node (tag) at ([yshift=-47.5pt]$(0)!0.5!(5)$) {Some text};
    \end{tikzautomata}

\end{document}

答案1

我不确定你想要什么。文本不再需要手动定位,但信息 -0.75cm 是必要的。我建议修复节点 (0b)(节点 0 下方)和节点 (5) 下方的 (5b)。文本位于绘制的中间位置。

            \documentclass[12pt]{article}

    \usepackage{tikz}
    \usetikzlibrary{automata, positioning, arrows.meta, calc}

    \tikzset{
        ->,
        >=Stealth, 
        node distance=3cm,
        every state/.style={thick}, 
        initial text =,
    }

    \newenvironment{tikzautomata}{
        \begin{center}
        \begin{tikzpicture}[shorten >=1pt, on grid, auto]
    }{
        \end{tikzpicture}
        \end{center}
    }

    \begin{document}
    \begin{tikzautomata}
        \node[state]
        (0) {$0$};
        \node[state, right of=0]
        (1) {$1$};
        \node[state, right of=1]
        (2) {$2$};
        \node[state, right of=2]
        (3) {$3$};
        \node[state, right of=3]
        (4) {$4$};
        \node[state, right of=4]
        (5) {$5$};

        \path ($(0.south)+(0,-0.75)$) node [shape=coordinate] (0b){};
        \path ($(5.south)+(0,-0.75)$) node [shape=coordinate] (5b){};

        \draw (0.south) -- (0b) --node[midway,below] (tag) {Some text}(5b)--(5.south);
    \end{tikzautomata}

    \end{document}

相关内容