7

Right now, what I have is

\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{tkz-graph}
\tikzstyle{vertex}=[circle, draw, inner sep=0pt, minimum size=6pt]
\newcommand{\vertex}{\node[vertex]}
\begin{document}

\begin{center}
    \begin{tikzpicture}[scale=0.7]
        \vertex[label=$p_1$](p1) at (-1,1.5) {};
        \vertex[label=$p_2$](p2) at (1,1.5) {};
        \vertex[label=$p_3$](p3) at (-1,0) {};
        \vertex[label=$p_4$](p4) at (-1,-1.5) {};
        \vertex[label=$p_5$](p5) at (1,-1.5) {};
    \tikzset{EdgeStyle/.style={->}}
        \Edge(p1)(p3)
        \Edge(p3)(p4)
        \Edge(p1)(p5)
        \Edge(p2)(p4)
        \Edge(p2)(p5);
    \end{tikzpicture}
\end{center}

\end{document}

which produces the following:

bad things

I am fairly new to tikz so I apologize if this is a dumb question, but how do I get it so that, instead of circles, the vertices will only be displayed as the text labels $p_1$, $p_2$, etc. (which should be positioned where the circles currently are)? There will also probably need some padding around the labels so the arrows don't run overlap with them. I'm not sure how to do either of these things.

10
\documentclass{amsart}
\usepackage{tikz} 
\begin{document}
\begin{center}
\begin{tikzpicture}
    \node (p1) at ( 0, 0) {$p_1$}; 
    \node (p2) at ( 1, 0) {$p_2$};
    \node (p3) at ( 0,-1) {$p_3$};
    \node (p4) at ( 0,-2) {$p_4$};
    \node (p5) at ( 1,-2) {$p_5$};

    \begin{scope}[every path/.style={->}]
       \draw (p1) -- (p3);
       \draw (p3) -- (p4); 
       \draw (p1) -- (p5);
       \draw (p2) -- (p4);
       \draw (p2) -- (p5);
    \end{scope}  
\end{tikzpicture}
\end{center}
\end{document}

Output

graph.png


Things of this kind remind me immediately of Commutative diagrams, so here is the incredibly simple tikz-cd code to achieve your desired goal:

\documentclass{amsart} 
\usepackage{tikz-cd} 
\begin{document}
\begin{tikzcd}
p_1 \arrow{d} \arrow{rdd} & p_2 \arrow{ldd} \arrow{dd}\\
p_3 \arrow{d} &     \\
p_4 & p_5 
\end{tikzcd}
\end{document} 

Output

graphcd.png

  • How to scale the picture with tikz-cd without scaling the labels ? I try to find this in the doc, but without success ? – Alain Matthes May 9 '13 at 6:21
  • @AlainMatthes I think you can use xscale and yscale (or simply scale, which sets both the scales to the same specified value, I think). +1 for showing OP what (s)he wanted to do... – kan May 9 '13 at 9:09
  • I tried scale but without success. I made a search on the net but with no result – Alain Matthes May 9 '13 at 9:43
  • 2
    @AlainMatthes: I believe you want the row sep and column sep keys. – Charles Staats Feb 5 '14 at 16:00
11

A solution with tkz-graph is also simple but you need to avoid to mix tikz and tkz-graph You need if you work with tkz-graph to know some principles.

With the example you give, it's easy to use tikz or tikz-cd, tkz-graph is only useful if you need to set up automatically some styles and if you need to draw some complex graph but with some geometrical requests (vertices on a line , on a square, a circle

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

\begin{document}

\begin{center}
    \begin{tikzpicture}%[scale=0.7]
    % initialization
      \SetGraphUnit{2}
      \SetVertexMath
      \GraphInit[vstyle=Empty]
      % vertices
      \Vertex[L=p_1]{p1} \EA[L=p_2](p1){p2}
      \SO[L=p_3](p1){p3}  
      \SO[L=p_4](p3){p4} \EA[L=p_5](p4){p5}
      % edges
      \tikzset{EdgeStyle/.style = {->}}
     \Edges(p1,p3,p4)
     \Edges (p1,p5)  \Edges (p2,p5)  \Edges (p2,p4)
    \end{tikzpicture}
\end{center}

\end{document}

Comments :

  • \SetGraphUnit If you use some automatic placement, you need to fix the node distance with tkz-graph you can use this macro but possible is d=2 cm inside the options of a Vertex for a local modification
  • \SetVertexMath All the label are in Math Mode, so L=p_3 is enough
  • \GraphInit[vstyle=Empty]the choice of the style. Here the node are circle but no draw
  • \Vertex[L=p_1]{p1} the first vertex as you can see, no need to add ; at the end of the command. The reference is p1 but the label is $p_1$.
  • \EA is for east of (I made this package before the existence of the positioning library). \SOis for south etc... you have also SOEA NOWE etc.
  • It's possible to use personal styles with \tikzset{EdgeStyle/.style = {->}} or \tikzset{VertexStyle/.style = {...}} or \tikzset{EdgeStyle/.append style = {->}} etc.

enter image description here

2

Considering your purpose, it could be easier to use the XY package, just as:

\documentclass{article}
\usepackage[all]{xy}
\begin{document}

\xymatrix{
p_1 \ar[d] \ar[ddrr] & & p_2 \ar[dd] \ar[ddll]\\
p_3 \ar[d] & & \\
p_4 & & p_5 
}

\end{document}

Output:

enter image description here

0

Thanks for above.

I needed nodes in a circle so wrote a python program to produce numbers for it.

#!/usr/bin/python
#
# create circular coordinates for diagrams in Latex


import math
import re

for i in range (0,9):
    # start at the top so add 90 degrees
    # want it so go clockwise so negate i
    y = ((360*-i/9.0) + 90.0) % 360.0
    #print "degrees", y
    y = y * (3.142*2.0)/ 360.0
    #print "radians",y
    scale = 5
    xx= scale + scale*math.cos(y)
    yy= scale + scale*math.sin(y)
    print "\\node (nn",i,") at (",xx,",",yy,") {$n",i,"$};"

I ran it through sed to clean up some spaces from the python print statement as well.

$ python latex_circle.py | sed 's/nn /nn/' | sed 's/ )/)/'
  • 2
    I am not sure whether this answers the question or not, but anyway you do not need sed tidy up the spaces: just write "\\node (nn"+str(i) etc. – Andrew Swann Aug 8 at 14:48
  • @AndrewSwann my python/regex is a bit old school. I ought to do it in python. Thanks for the pointer. – Robin Aug 13 at 11:30

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.