Graphics, Figures & TablesToken on bent Edge

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
rajtendulkar
Posts: 3
Joined: Wed Aug 24, 2011 3:13 pm

Token on bent Edge

Post by rajtendulkar »

Dear Forum,

I am trying to make a data flow graph using tikZ package and related libraries.

Code: Select all

\usetikzlibrary{arrows,automata,calc}
...
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.0cm,thick]
  \tikzstyle{every state}=[fill=white,draw=black,text=black]

  \node[state]         (A) {$A$};
  \node[state]         (B) [right of=A] {$B$};
  \node[state]         (C) [right of=B] {$C$};

  \draw (A) to (B);
  \draw (B) to (C);
  \draw (C) to [bend left=45] (A);  
  \fill[black] ($ (C)!.45!(A) $) circle (3pt);

\end{tikzpicture}
What I want is to have a black circle on the edge from C to A which is bent. However, this code puts the token in the circle of B instead on the edge.
Can someone please help?

Thank You,
Raj

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Token on bent Edge

Post by localghost »

Please get used to always providing a complete example that is compilable out of the box for everybody. This increases your chance for getting help because it becomes easier to test possible solutions. In case of more complicated problems this is very helpful.

It works if you draw the edges slightly different. See code below for details.

Code: Select all

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}

\begin{document}
  \begin{tikzpicture}[%
    ->,
    >=stealth',
    shorten >=1pt,
    auto,
    node distance=2cm,
    thick,
    every state/.style={%
      fill=white,
      draw=black,
      text=black
    }
  ]

    \node[state] (A) {$A$};
    \node[state] (B) [right of=A] {$B$};
    \node[state] (C) [right of=B] {$C$};

    \path (A) edge                                                             (B)
          (B) edge                                                             (C)
          (C) edge[bend left=45] node[midway,below] {\tikz\fill circle (3pt);} (A);
  \end{tikzpicture}
\end{document}
Note that you can insert arbitrary tikZ code into a node as long as you introduce a new picture environment. Here this is done by the \tikz command because there is only a single command for drawing the circle. In case there are more such commands you might need a »tikzpicture« environment inside the node.


Best regards and welcome to the board
Thorsten
rajtendulkar
Posts: 3
Joined: Wed Aug 24, 2011 3:13 pm

Token on bent Edge

Post by rajtendulkar »

Dear Thorsten,

Thank you for your reply.
I will take care of your suggestion when I post the code next time.

Thanks for your solution.
I tried it out, however, the black circle is below the edge from C to A.
I want it exactly to be on the edge.

Hence I used the following solution.
I was wondering if there is any other solution which I will not require to use the hard-coded co-ordinate values.

Code: Select all

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,calc}

\begin{document}

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=3.0cm,thick]
  \tikzstyle{every state}=[fill=white,draw=black,text=black]

  \node[state]         (A) {$A$};
  \node[state]         (B) [right of=A] {$B$};
  \node[state]         (C) [right of=B] {$C$};

  \draw (A) to node 
        [xshift=0mm, very near start] {$2$} 
        node [xshift=-1mm, very near end] {$3$} (B);

  \draw (B) to node
        [xshift=0mm, very near start] {$1$} 
        node [xshift=-1mm, very near end] {$2$} (C);

  \draw (C) to [bend left=45] node 
        [yshift=2mm, xshift=2mm,very near start] {$3$} 
        node [yshift=1.5mm, xshift=-2mm,very near end] {$1$} (A);
 
 % Hard-coded the token location since i don't know about it.
  \fill[black] (3,-1.45) circle (3pt);
  \fill[black] (2.75,-1.43) circle (3pt);
  \fill[black] (3.25,-1.43) circle (3pt);
 

\end{tikzpicture}
\end{document}
Thank You,
Raj
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Token on bent Edge

Post by localghost »

rajtendulkar wrote:[…] I tried it out, however, the black circle is below the edge from C to A. I want it exactly to be on the edge. […]
Remove the »auto« option for the tikzpicture environment and the »below« option for the concerned node.

Code: Select all

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,calc}

\begin{document}
  \begin{tikzpicture}[%
    ->,
    >=stealth',
    shorten >=1pt,
    node distance=2cm,
    thick,
    every state/.style={%
      fill=white,
      draw=black,
      text=black
    }
  ]

    \node[state] (A) {$A$};
    \node[state] (B) [right of=A] {$B$};
    \node[state] (C) [right of=B] {$C$};

    \path (A) edge                                                       (B)
          (B) edge                                                       (C)
          (C) edge[bend left=45] node[midway] {\tikz\fill circle (3pt);} (A);
  \end{tikzpicture}
\end{document}
rajtendulkar
Posts: 3
Joined: Wed Aug 24, 2011 3:13 pm

Token on bent Edge

Post by rajtendulkar »

Dear localghost,

Thank you very much for the reply.
Your solution works indeed!
However, I came across yet another problem.
The solution does not work for self-loops.

Do you have any idea? :)

I have attached the code below.
You can un-comment the hard coded values in order to see what result is expected.

Code: Select all

  \begin{tikzpicture}[%
    ->, 
    >=stealth',
    shorten >=1pt,
    node distance=2cm,
    thick,
    every state/.style={%
      fill=white,
      draw=black,
      text=black
    }   
  ]

    \node[state] (A) {$A$};
    \node[state] (B) [right of=A] {$B$};
    \node[state] (C) [right of=B] {$C$};

    \path (A) edge [loop above] node[midway] {\tikz\fill circle (3pt);}   (A) 
          (A) edge node [above, very near start] {$2$} node [above,very near end] {$3$} (B) 
          (B) edge [loop above] node[midway] {\tikz\fill circle (3pt);}   (B) 
          (B) edge node [above, very near start] {$1$} node [above, very near end] {$2$} (C) 
          (C) edge [loop above] node[midway] {\tikz\fill circle (3pt);}   (C) 
          (C) edge [bend left=45] node [xshift=2mm,yshift=1mm,below,very near start] {$2$} node [xshift=-2mm,yshift=1mm,below,very near end] {$1$} 
                node[midway,below,yshift=-1mm] {2} node[midway] {\tikz\fill circle (3pt);} (B) 
          (B) edge [bend left=45] node [xshift=2mm,yshift=1mm,below,very near start] {$3$} node [xshift=-2mm,yshift=1mm,below,very near end] {$2$} 
                node[midway,below,yshift=-1mm] {4} node[midway] {\tikz\fill circle (3pt);} (A);

 % Hard-coded the token location since i don't know about it.
  %\fill[black] (0.00,1.00) circle  (2pt);
  %\fill[black] (2.00,1.00) circle  (2pt);
  %\fill[black] (4.00,1.00) circle  (2pt);

  \end{tikzpicture}

Thank You for reply !
Raj
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Token on bent Edge

Post by localghost »

At the moment I don't see an automatic solution. So you have to adjust the position manually.

Code: Select all

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}

\begin{document}
  \begin{tikzpicture}[%
    ->,
    >=stealth',
    shorten >=1pt,
    node distance=2cm,
    thick,
    every state/.style={%
      fill=white,
      draw=black,
      text=black
    }   
  ]

    \node[state] (A) {$A$};
    \node[state] (B) [right of=A] {$B$};
    \node[state] (C) [right of=B] {$C$};

    \path (A) edge [loop above] node[midway,above=-7pt] {\tikz\fill circle (3pt);}   ()
          (A) edge node [above, very near start] {$2$} node [above,very near end] {$3$} (B)
          (B) edge [loop above] node[midway,above=-7pt] {\tikz\fill circle (3pt);}   ()
          (B) edge node [above, very near start] {$1$} node [above, very near end] {$2$} (C)
          (C) edge [loop above] node[midway,above=-7pt] {\tikz\fill circle (3pt);}   ()
          (C) edge [bend left=45] node [xshift=2mm,yshift=1mm,below,very near start] {$2$} node [xshift=-2mm,yshift=1mm,below,very near end] {$1$}
                node[midway,below,yshift=-1mm] {2} node[midway] {\tikz\fill circle (3pt);} (B)
          (B) edge [bend left=45] node [xshift=2mm,yshift=1mm,below,very near start] {$3$} node [xshift=-2mm,yshift=1mm,below,very near end] {$2$}
                node[midway,below,yshift=-1mm] {4} node[midway] {\tikz\fill circle (3pt);} (A);
  \end{tikzpicture}
\end{document}
Post Reply