3

As the title tells, how to draw a graph with ring in latex? I have learned that I can use forest to draw a tree in latex. For example, using following code, I can have one simple binary tree.1->2, 1->3.

\begin{forest}
for tree={circle,draw}
[1[2][3]]
\end{forest}

Then, Im wondering how to draw a graph with ring. For example, how can I add another edge between 1 and 2 so that there is ring between node 1 and 2. 1 -> 2, and 2->1. Also, I'm wondering how to draw a self ring. i.e., 1->1(an egde starting from node 1 and ending at node 1)

migrated from stackoverflow.com Sep 18 '15 at 16:48

This question came from our site for professional and enthusiast programmers.

  • 1
    I think that helps. I would also highly recommend posting an image of what your current code produces, so that people can infer what this produces so far. I would also highly recommend showing the output that you get from LaTex when you try to do something that would cause a cycle to occur. Do you get an error? An image that makes no sense? What has been tried and what has occurred when it was tried? – Namey Sep 8 '15 at 2:56
  • 1
    Also, even without being very familiar with this library, if it is designed for trees (which tend to be defined as acyclic), it may simply not be designed for a generic graphs. – Namey Sep 8 '15 at 2:57
  • 1
    Welcome to TeX.SE! Your post was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. – Werner Sep 18 '15 at 16:58
  • Forest really can't do this easily. As @Namey says, it is designed to draw trees. If what you need is mostly a tree, you can still use it with a bit of manual intervention. If you really want to draw a generic graph, though, it is better to use something designed for that. – cfr Sep 12 '16 at 12:50
3

Forest is designed to draw trees. If what you need is mostly a tree, you can still use it with a bit of manual intervention. If you really want to draw a generic graph, though, it is better to use something designed for that.

In case you want basically-a-tree-with-the-odd-non-tree-bit, here's one solution.

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={%
    circle,
    draw,
    edge={->},
  }
  [1, tikz+={\draw () edge [loop above] ();}
    [2, edge path'={(!u.parent anchor) edge [bend right, <-]  (.north) (!u.parent anchor) edge [bend left] (.north east)}]
    [3]
  ]
\end{forest}
\end{document}

non-tree in Forest

If you need to draw generic graphs, TikZ offers built-in graph-drawing facilities, courtesy of LuaTeX. Here's one using the binary tree layout, which is not terribly fussy about whether the specification is technically a tree or not, as the following shows. Note that this code must be compiled with LuaTeX - it will not work with other engines.

\RequirePackage{luatex85}
\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}
  \graph
  [
    binary tree layout,
    nodes={draw, circle},
  ]
  {
    1 ->[bend right] 2 -> [bend right] 1 ->[loop above] 1 -> 3;
  }
  ;
\end{tikzpicture}
\end{document}

graph-drawing solution

Your Answer

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