0

In the code below, I poorly put a text below the middle of a polyline. I would like to automate this: the parameter is -.75 in ++(0,-.75) used to draw the polyline. What is the best way to achieve that?

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

projetmbc
  • 13,315

1 Answers1

1

i'm not sure is what you want. The text is no more positioning by hand, but the information -0.75cm is necessary. I propose to repair the node (0b) (below the node 0) and (5b) below the node (5). The text is at th midway of the draw.

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

pascal974
  • 4,652