26

I've been using LaTeX since three years, but I've always avoided the Tikz package for drawing. Now I am writing my degree thesis and I'm forced to use it to achieve a better result. The problem is just I don't have time right now to start a deep walkthrough.

I have to draw these simple patterns of graphs: a triangle and a complete bipartite graph (see pictures below). I found the sample code of a simple triangle graph on the web and I tried to rearrange nodes using relative positioning, but the result was not quite good. This is my attempt.

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=3cm,
        thick,main node/.style={circle,fill=blue!20,draw,minimum size=1cm,inner sep=0pt]}]

    \node[main node] (1) {$1$};
    \node[main node] (2) [below left of=1]  {$2$};
    \node[main node] (3) [below right of=1] {$3$};

    \path[-]
    (1) edge node {} (2)
        edge node {} (3)
    (2) edge node {} (1)
        edge node {} (3)
    (3) edge node {} (1)
        edge node {} (2);
\end{tikzpicture}

Could you please give me any help to improve this result?

Thank you.

Graph pattern

4
  • 17
    @downvoters: Please don't pile on downvotes. A net score of -1 is enough to show that the question needs to be improved. More downvotes should be reserved for questions that are unsalvageable or spam. Commented Feb 7, 2014 at 15:04
  • 3
    I think your question is reasonable and you've shown some effort, especially compared to some of the others that we have seen on the site. The down-voting policy can be pretty inconsistent, it really depends on who is looking at it; looks like you're on the way up now :) I think it was probably the line when you said The problem is just I don't have time right now to start a deep walkthrough. that might have set the tone for some folks.
    – cmhughes
    Commented Feb 7, 2014 at 16:56
  • @cmhughes You're completely right! Well, maybe the downvoters didn't understand the meaning of I don't have time right now to start a deep walkthrough. To me this means that I tried to edit the code found on the web but I failed due to my poor Tikz background. I'm really interested in this package and I'm surely going to learn it in the future.
    – Driu
    Commented Feb 7, 2014 at 17:50
  • 3
    For what it's worth, I don't think anyone learns TikZ by doing a deep walkthrough. It's not something you sit down and dedicate time to study. People learn TikZ bit by bit, as needed, in order to create good-looking pictures. So I think what you're doing now is the perfect way to start learning. Just start with the goal of making a diagram, and then learn whatever little piece of TikZ syntax you need to in order to do it. Then repeat as necessary.
    – David Z
    Commented Feb 7, 2014 at 18:27

5 Answers 5

30

Well, we answer many questions with "Do it for me!" Why not this one!

TikZ mafia here won't forgive people having no time to learn tikz;). The code you got from elsewhere makes wrong use of below of. This is how it is used with positioning library.

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{positioning}
\tikzset{main node/.style={circle,fill=blue!20,draw,minimum size=1cm,inner sep=0pt},
            }
\begin{document}
  \begin{tikzpicture}
    \node[main node] (1) {$1$};
    \node[main node] (2) [below left = 2.3cm and 1.5cm of 1]  {$2$};
    \node[main node] (3) [below right = 2.3cm and 1.5cm of 1] {$3$};

    \path[draw,thick]
    (1) edge node {} (2)
    (2) edge node {} (3)
    (3) edge node {} (1);
    %%
    \begin{scope}[xshift=4cm]
    \node[main node] (1) {$1$};
    \node[main node] (2) [right = 2cm  of 1]  {$2$};
    \node[main node] (3) [below = 2cm  of 1] {$3$};
    \node[main node] (4) [right = 2cm  of 3] {$4$};

    \path[draw,thick]
    (1) edge node {} (2)
    (1) edge node {} (4)
    (3) edge node {} (2)
    (3) edge node {} (4)
    ;
    \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

4
  • I am a member of that mafia :)
    – user11232
    Commented Feb 7, 2014 at 15:24
  • Thank you very much! You're a rescuer, not an helper! ;)
    – Driu
    Commented Feb 7, 2014 at 15:33
  • Appreciate your honesty, Don Kumar. There is kind of a double moral on this site sometimes: questions are closed because they are of the type "do this for me", but if it sounds fun, other questions are answered nevertheless.
    – Ingo
    Commented Feb 12, 2014 at 11:01
  • 4
    @Ingo Thanks. As I am a teacher by profession, I always feel that beginners need to be given a hand. :)
    – user11232
    Commented Feb 12, 2014 at 22:52
13

You're not going to get really popular here with questions like "I don't have time for this, do it for me". However I've been a newbie and I've got a lot of help from here. I'm still a newbie =).

Triangle with numbers:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[main_node/.style={circle,fill=blue!20,draw,minimum size=1em,inner sep=3pt]}]

    \node[main_node] (1) at (0,0) {1};
    \node[main_node] (2) at (-1, -1.5)  {2};
    \node[main_node] (3) at (1, -1.5) {3};

    \draw (1) -- (2) -- (3) -- (1);
\end{tikzpicture}
\end{document}

The syntax is really simple and self-explanatory: Put these node here, there and there. The the parentheses after the \node command is the id of that node. Then, to draw the connections you can use those ids: connect 1 with 2 with a straight line, 2 with 3 and 3 with 1.

If you are looking something more like the image you put down there, this will do:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[main_node/.style={circle,fill=blue!60,minimum size=1em,inner sep=3pt]}]

    \node[main_node] (1) at (0,0) {};
    \node[main_node] (2) at (-1, -1.5)  {};
    \node[main_node] (3) at (1, -1.5) {};

    \draw (1) -- (2) -- (3) -- (1);
\end{tikzpicture}
\end{document}

Note that:

  • No text is given in the braces {}
  • blue!60 means "a 60% of blue". That's why your blue!20 was lighter.
  • I didn't draw the border. Maybe it could be improved by drawing the border in the same color as the node.

Having this, the other figure is easy.

8

You could try the new graph drawing library (which requires lualatex):

\documentclass[tikz,border=0.1cm]{standalone}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{trees}
\begin{document}
\tikz\path [graphs/.cd, nodes={shape=circle, fill=blue!40, draw=none, outer sep=0pt}, empty nodes]
  graph [tree layout] { A1 -- {B1 -- C1} -- A1 }
  [shift=(0:1)]
  graph { A2 -- B2; C2 -- D2; A2 -- D2;  B2 -- C2 };
\end{document}

enter image description here

2
  • This library is simply amazing. It simplifies the workflow a lot!
    – Ingo
    Commented Feb 13, 2014 at 10:10
  • I'm struggling to get this working in a document, I have lualatex installed (sudo apt-get install texlive-luatex) How would this code be used without creating a separate standalone document? A tikZ floating section or something? I'm unfamiliar, but it looks nice to use. Thanks
    – baxx
    Commented Apr 13, 2015 at 1:24
7

The answers are never complete without PSTricks.

\documentclass[preview,border=12pt]{standalone}
\usepackage{pst-node}
\psset{colsep=1cm,rowsep=2cm,mnode=C,fillstyle=solid,fillcolor=blue!40,linecolor=blue!40}
\begin{document}
\begin{psmatrix}
&[]&&[]&&[]\\ []&&[]&[]&&[]
\end{psmatrix}
\psset{linecolor=.}\ncline{1,2}{2,1}\ncline{1,2}{2,3}\ncline{2,1}{2,3}\ncline{1,4}{1,6}\ncline{2,4}{2,6}\ncline{1,4}{2,4}\ncline{1,6}{2,6}
\end{document}

enter image description here

Note:

linecolor=. sets the color to black. The same notation used by Mathematica to reset the variables.

3
  • 1
    Very True: "answers are never complete without PSTricks" Commented Feb 7, 2014 at 18:16
  • Any idea why :fillcolor=blue!40 does not fill color in nodes using xelatex engine Commented Feb 7, 2014 at 18:21
  • @texenthusiast: I need to pass this issue to PSTricks mailing list. Commented Feb 7, 2014 at 18:24
5

With tkz-graph

\documentclass[11pt]{article}
\usepackage{tkz-graph}

\begin{document}
  \SetGraphUnit{4}
  \GraphInit[vstyle=Simple]
  \SetVertexSimple[MinSize    = 16pt, LineColor = blue!60, FillColor = blue!60]

\begin{tikzpicture}[rotate=90] 
  \Vertices[NoLabel]{circle}{A,B,C}
  \Edges(A,B,C,A)
\end{tikzpicture}
\begin{tikzpicture}[rotate=-45] 
  \GraphInit[vstyle=Simple]
  \Vertices{circle}{A,B,C,D}
  \Edges(A,C,B,D,A)
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .