3

I am making a picture of an automaton where the transitions have comma-separated tuples for weights. This isn't a problem until I try to space out transitions in the same part of the graph using the for-loop syntax (hat-tip to: How to draw a many edges loop above - tikz). Because the edge labels for the for-loop are separated by commas, each element in the tuple is grabbed as part of a new label by the loop, rather than being part of the label.

\documentclass{article}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}

\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,
                semithick]

\node[initial,state, accepting, align = center] (A) {0};

\path (A)    edge [loop above, align=center] node {a:a \\ $\langle 0, 0, 0, 0 \rangle$} (A);
\begin{pgfinterruptboundingbox}
\foreach \looseness/\label [count=\n] in {15/i:a \\ $\langle 2 1 1 1\rangle$ }
    \path [->] (A) edge [
        loop above, align = center,
        every loop/.append style={
        looseness=\looseness,
        in=60-0.8*\looseness,
        out=120+0.8*\looseness
    }] node {\label} ();
\end{pgfinterruptboundingbox}
\end{tikzpicture}
\end{document}

To avoid this I have omitted commas. The result is that the manually specified arc looks the way I want it to, but the one created by the for loop does not (no commas between elements). I get the impression from Using double for-loop in tikz that the for-loop just does not support what I am trying to do, but I'd like to make sure.

P.S. The graph I am making does use several arcs, so there is some reason to use a for-loop.

single state automaton with comma separated and non-comma separated weight vectors

  • Can you not just use {$\langle 2, 1, 1, 1\rangle$} in specifying the label? The curly brackets will protect the commas. This is the standard way to include commas in \foreach loops with TikZ as far as I know. – cfr Jun 29 '15 at 0:49
  • Hey, it works! Thank you. If you write it as an answer, I'll mark it as the preferred one. – sautedman Jun 29 '15 at 0:51
3

The usual way to use values which include commas in \foreach loops in TikZ is to protect them using curly brackets. (Note that coordinates can be handled specially so don't use this method for those.)

For example:

\documentclass[tikz, border=50pt]{standalone}
\usetikzlibrary{arrows,automata}

\begin{document}
  \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,
    semithick]

    \node[initial,state, accepting, align = center] (A) {0};

    \path (A)    edge [loop above, align=center] node {a:a \\ $\langle 0, 0, 0, 0 \rangle$} (A);
    \begin{pgfinterruptboundingbox}
    \foreach \looseness/\label [count=\n] in {12/i:a \\ {$\langle 2, 1, 1, 1\rangle$} }
      \path [->] (A) edge [
      loop above, align = center,
      every loop/.append style={
        looseness=\looseness,
        in=60-.8*\looseness,
        out=120+.8*\looseness
      }] node {\label} ();
    \end{pgfinterruptboundingbox}
  \end{tikzpicture}
\end{document}

commas in loops

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.