How to generalize this graph?

If you know how to answer the question in pstricks I am open to that, but I am doing things in tikz now. I found the below snippet in a tikz manual here: http://pgf.sourceforge.net/pgf_CVS.pdf, on p. 737-8.

\documentclass[11pt]{article}
\usepackage{tikz}
\begin{tikzpicture}
\foreach \angle in {0,45,...,315}
\node[rectangle,draw=black!50] (\angle) at (\angle:2) {\angle};
\foreach \from/\to in {0/45,45/90,90/135,135/180,
180/225,225/270,270/315,315/0}
\path (\from) edge [->,bend right=22,looseness=0.8] (\to) edge [<-,bend left=22,looseness=0.8] (\to);
\end{tikzpicture}
\end{document}


It makes this:

I want to generalize this to allow for more vertices and edges. Given n vertices, I will be drawing arbitrary walks through the graph, of length n-1 edges. I want the edges to be distinguishable, and also to support labels 1, 2, ...

Help with three things would be great:

1) If I have 6 vertices I need to be able to make 5 loops on a single vertex distinguishable. I don't know how to loop other than above, right, etc.

2) If there are multiple edges going between two vertices (the arrows do NOT have to point all the same way), I want them to bend an even amount between each other.

3) Get a label on the edge (probably literally ON it) which is small and tasteful but also legible.

Because I don't know beforehand how many vertices I will be drawing, or the actual shape of the path, I need to express things rather generally. I can write the code for this if I can get help with the above.

• Please complete your code so that it will compile. Please also state where in the manual. It is rather long to look through. – cfr Sep 17 '16 at 22:57
• The code you show uses something different from bend left and bend right, so you already have the answer to (1) right there. This should also solve (2). For (3), if you load the quotes library, you can use "<label>" to add the labels and every edge label to style and position them. – cfr Sep 17 '16 at 23:01
• I updated the question to show the manual. The code is fine, exactly as in the manual, and compiles for me. As for your second comment: I can pretty easily draw a specific graph, true. My problem is how to get a GENERAL RECIPE for drawing a properly formatted graph given n vertices. I will be drawing a huge number of these graphs and need to automate this. – Wapiti Sep 17 '16 at 23:04
• To compile that code, you must add a suitable preamble and document environment. Please edit your question to provide a minimal working example people can compile. – cfr Sep 17 '16 at 23:06
• For labeling path individual, you need to draw them "manually". For labels position you can use option auto. Otherwise your question is not very clear to me. Do you looking for something to similar to tex.stackexchange.com/questions/281031/… ? – Zarko Sep 18 '16 at 2:02

You said that you could write the code for the general recipe given answers to the three specific questions. However, if you want general recipes, TikZ's graph-drawing facilities, especially the various algorithms available, must surely be the way to go.

I do not know this very well - it feels like alien territory - but LuaTeX provides a good deal of power and the documentation and libraries are extensive. If you'll be drawing lots of them, it will be worth your while to invest the time in reading the documentation in the manual.

circular, routing, graphs.standard (for standard graphs such as K_n etc.) should be of particular interest. Note the introduction is in the main bit of the manual and the detail is in the graphs drawing part proper.

Here's a mini-example based on your stated desiderata. (I'm not very clear what these are, so this really means your stated desiderata as best I could fathom them.)

\RequirePackage{luatex85}
\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{graphs,graphdrawing,arrows.meta,quotes}
\usegdlibrary{circular}
\begin{document}
\tikz[>=Stealth]{%
\graph [simple necklace layout, grow'=down, node sep=20mm, nodes=draw, edge quotes center, edge quotes={fill=white, inner sep=1pt, font=\scriptsize}]
{
6  ->[bend left, "a"] {5, 4, 3, 2, 1};
5  ->[bend left, "b"] {6, 4, 3, 2, 1};
4  ->[bend left, "c"] {6, 5, 3, 2, 1};
3  ->[bend left, "d"] {6, 4, 5, 2, 1};
2  ->[bend left, "e"] {6, 4, 3, 5, 1};
1  ->[bend left, "f"] {6, 4, 3, 2, 5};
};
}
\end{document}


Obviously, you'd need to create the edges individually if you needed different labels each time - or create a loop, but that would not really be worth it in that case. The subgraph is laying out the nodes.

You could also use e.g. subgraph C_n [n=6, ->], perhaps with necklace routing (libraries: graphs.standard, routing). It really depends on the details of what you are trying to do ...

EDIT

Note that I just provided an example of what's possible. If you use this, obviously you will want to look at the documentation to tweak it to your requirements.

Here are 3 examples using the standard graph drawing elements. A simple counter is used for labelling the edges which is reset between graphs. Use the edge counter style in the argument to edges for the part of the graph where you want paths to be labelled.

\RequirePackage{luatex85}
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs,graphs.standard,graphdrawing,arrows.meta,quotes}
\usegdlibrary{circular}
\begin{document}
\newcounter{countme}
\setcounter{countme}{0}
\newcommand*\countup{\stepcounter{countme}\thecountme}
\newcommand*\countset{\setcounter{countme}{0}}
\tikzset{%
edge counter/.style={"\countup"},
}
\tikz[>=Stealth]{%
\graph [simple necklace layout, grow'=down, node sep=20mm, nodes=draw, edge quotes center, edge quotes={fill=white, inner sep=1pt, font=\scriptsize}]
{
subgraph C_n [n=6, -!-];
{ [edges={edge counter, bend left}]
6  -> 5 ->[loop below] 5 -> 2 -> 6 -> 3;
}
};
}
\countset
\tikz[>=Stealth]{%
\graph [simple necklace layout, grow'=down, node sep=20mm, nodes=draw, edge quotes center, edge quotes={fill=white, inner sep=1pt, font=\scriptsize}]
{
subgraph C_n [n=6, -!-];
{ [edges={edge counter, bend left}]
3  -> 4 ->3 -> 1 -> 2 -> 3;
};
};
}
\countset
\tikz[>=Stealth]{%
\graph [simple necklace layout, grow'=down, node sep=20mm, nodes=draw, edge quotes center, edge quotes={fill=white, inner sep=1pt, font=\scriptsize}]
{
subgraph C_n [n=6, -!-];
{ [edges={edge counter, bend right, blue}]
1  -> {2, 3, 4} ->  5;
}
};
}
\end{document}


Note that older installations of TeX will not have luatex85. If your system doesn't have it, just leave it out and it should work fine.

• This is cool. I have a few problems though. I tried for over an hour last night to get luatex85, so I can't test this easily. Also, as I said in the question, given n vertices, I will have n-1 edges, as I am drawing walks through n vertices. That is, you start at a vertex, go to another one, from that one go to another, and so on, n times. Since the vertices you visit are arbitrary, this includes loops. I don't know how to handle loops in the code you gave. Finally, the edge labels need to be the edge number in the walk, so 1,2,3...n-1. I should have made that more clear. Thanks. – Wapiti Sep 18 '16 at 19:28
• @Wapiti If you have an older installation of TeX, just leave luatex85 out. You only need it with recent installations. You can add whichever edges you want, including loops, with whatever labels you want. I thought you meant n-1 edges for each node i.e. an edge from each node to each of the others. So I drew that. But the method is perfectly flexible: just specify the walks, edges and labels you need. – cfr Sep 19 '16 at 0:24
• Ok, that's encouraging. But I have a newer installation of TeX, evidently. The original error is File luatex85.sty not found. and the error when I omit \RequirePackage{luatex85} is You need to run LuaTeX to use the graph drawing library. So I have to figure out how to get the package... – Wapiti Sep 19 '16 at 0:57
• @Wapiti No. You can ignore the package. (You don't have the current LuaTeX - you might have the one from TeX Live 2015, for example, and still won't need the package.) But you do need to use lualatex to compile. It looks as if you are using pdflatex or xelatex or latex. This will only work with lualatex. But you almost certainly can remove the \RequirePackage line. – cfr Sep 19 '16 at 1:11
• @Wapiti Please see also edit above to give you a bit more of an idea of how you might use this and what you can do. (You can do much more with this than I know how to do, however! Much, much more!) – cfr Sep 19 '16 at 1:12

Here is pstricks code for the figure in the O.P.'s post. It uses the pst-poly package: \documentclass[12pt, x11names, border=10pt]{standalone} \usepackage[utf8]{inputenc} \usepackage{fourier} \usepackage{pst-poly} \usepackage{auto-pst-pdf}

\begin{document}

\psset{unit=2cm, dimen=m, arrowinset=0.15}
\begin{pspicture}%
\newcounter{angle}
%%%
\providecommand{\PstPolygonNode}{%
\psnode(1;\INode){B\INode}{\psframebox[fillstyle=solid, fillcolor=white, linestyle=solid, linecolor=LightSteelBlue3!50]{\theangle}}%
\addtocounter{angle}{45}}       \psset{ linewidth=0.6pt, linecolor=LightSkyBlue3, arrows=->, arcangle=-20}
\rput{-22.50}(0,0){\PstOctogon[PolyName =A, linestyle=none]}
\multido{\ibeg=0+1, \iend=1+1}{7}{\ncarc{B\ibeg}{B\iend}\ncarc{B\iend}{B\ibeg}}
\everypsbox{\scriptsize}
\ncarc[shiftB = 15mm]{B7}{B0}\ncarc{B0}{B7}
\ncarc[angleB=-100]{B4}{B1}\ncput*{a}
\ncarc[angleA=20, angle=150,arrows=<-]{B6}{B4}\nbput[labelsep=1pt]{b}
\ncarc[linestyle=dashed,  angleA=-30]{B7}{B3}\ncput*[framesep=1pt, npos=0.4]{c}
\uput{1.3em}[-150]{-150}(B5){\pscurve[unit=0.35](O)(0.75; -27)(1;0)(0.7; 30)(O)}%% loop
\end{pspicture}

\end{document}


The angle parameter for \ncarc (= node connection arc) denote the angles made by the arc with the nodes line (angleA for the beginning of the arc, angleB for the end, angle for both).

• This is nice, though I don't understand any of it. Also, my problem is to generalize the graph in the way I tried to specify, not recreate it in pstricks. Can pstricks help me with the problems I am having with the other answer? Thanks. – Wapiti Sep 18 '16 at 20:58
• @Wapiti: I started with your image, since there was no code at the time. I'm not sure I fully understand all of your requirements. I'll try to see what can be done. For the loops, there should be no problem, nor for the labels. F. y. i., when a polygon is created, its vertices are automatically numbered (that's what \INodeis about), and we may give them a name-prefix (PolyName=A). So we have (dimensionless) vertices Ai, and at these vertices I describe what is done in \providecommand{\PstPolygonNode}{…} – inserting rectangular nodes with the labels 0, 45, 90, …, as nodes named B_i. – Bernard Sep 18 '16 at 21:15
• @Wapiti: I've updated my code to show what can be done. Please take a look. – Bernard Sep 21 '16 at 23:36