4

I am using the following code (thanks to several answers on TeX.Stackexchange) to draw a small bipartite graph:

\documentclass{article}
\usepackage{tkz-berge}
\usetikzlibrary{fit,shapes,arrows}

\definecolor{myblue}{RGB}{80,80,160}
\definecolor{mygreen}{RGB}{80,160,80}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[scale=0.5]
  \GraphInit[vstyle=Classic]
  \tikzset{VertexStyle/.style =
    {shape=circle,fill=mygreen,minimum size = 6pt,inner sep=0pt}
  }
  \grEmptyPath[Math,x=0,y=0,prefix=d,RA=3,rotation=270]{2}
  \tikzset{VertexStyle/.style =
    {shape=circle,fill=myblue,minimum size = 6pt,inner sep=0pt}
  }  
  \grEmptyPath[Math,x=6,y=0,prefix=s,RA=3,rotation=270]{2}
  \tikzset{EdgeStyle/.style={->,font=\scriptsize}}
  \Edge[label = $0.005$](d0)(s0)
  \Edge[label = $0.002$](d0)(s1)
  \Edge[label = $0.100$](d1)(s1)    
\end{tikzpicture}
\caption{graph}
\label{fig:graph}
\end{figure}
\end{document}

which yields

small bipartite graph example

I want to make a few more changes. These are as follows:

  1. Move the vertex labels d0 and d1 to the left so that the edges don't cut across them.
  2. Move the edge weights so that the numbers are places right above the edge and parallel to the slope of the edge.

Note: I was consulting this manual, but didn't find helpful information regarding my issues. If you can provide a link to a more exhaustive document, it will be really helpful for me in future.

Progress: Added the line \SetUpVertex[Lpos=-180] before defining the vertex style for the left column, and then added \SetUpVertex[Lpos=0] before defining the vertex style for the right column (i.e., the s0 and s1 vertices). That solves the problem I had with vertex label positions.

5
  • It's a bit hard to help you when we don't know which packages you're using. Please provide a full MWE.
    – A.Ellett
    Commented May 17, 2015 at 21:34
  • Sorry about that. Added the packages. Commented May 17, 2015 at 21:42
  • You still haven't provided a MWE. Commented May 17, 2015 at 23:21
  • My most sincere apology for not adding a \documentclass line at the top. I am aware of the definition of an MWE, but isn't this nitpicking a bit too much? Commented May 18, 2015 at 0:17
  • Indeed it is. I just wasn't sure you knew what a MWE was since you didn't add the document class. Sorry for that. Commented May 18, 2015 at 0:30

3 Answers 3

2

You already solve the difficult part of problem: position of vertex label. The easy part is labels of edges. There you just add option sloped. So, the code of your MWE is:

    \documentclass{article}

\usepackage{tkz-berge}
\usetikzlibrary{fit,shapes,arrows}

\definecolor{myblue}{RGB}{80,80,160}
\definecolor{mygreen}{RGB}{80,160,80}

\usepackage[active,floats,tightpage]{preview}%<-- just to show figure only
    \setlength\PreviewBorder{1em}


\begin{document}
    \begin{figure}
\centering
\begin{tikzpicture}[scale=0.5]
  \GraphInit[vstyle=Classic]
\SetUpVertex[Lpos=-180]
\tikzset{VertexStyle/.style = {shape=circle, fill=mygreen,
                               minimum size=6pt,inner sep=0pt}
  }
\grEmptyPath[Math,x=0,y=0,prefix=d,RA=3,rotation=270]{2}
%
\SetUpVertex[Lpos=0]
\tikzset{VertexStyle/.style = {shape=circle, fill=myblue,
                               minimum size=6pt, inner sep=0pt}
  }
\grEmptyPath[Math,x=6,y=0,prefix=s,RA=3,rotation=270]{2}
\tikzset{EdgeStyle/.style={->,font=\scriptsize,above,sloped,midway}}%<-- new options
  \Edge[label = $0.005$](d0)(s0)
  \Edge[label = $0.002$](d0)(s1)
  \Edge[label = $0.100$](d1)(s1)
\end{tikzpicture}
    \caption{graph}
\label{fig:graph}
    \end{figure}
\end{document}

enter image description here

In conversion from pdf to jpg are lost transparency of shapes with edge labels, so the picture is a little bit damaged.

8
  • Ah, perfect! But I also want the edge labels to be placed above the line. Commented May 17, 2015 at 22:37
  • no problem, just add option auto into EdgeStyle. I will edit my answer accordingly.
    – Zarko
    Commented May 17, 2015 at 22:45
  • The labels are still poorly placed; they overlap the lines and are not centered between the two relevant dots. Commented May 17, 2015 at 23:21
  • In pdf picture the nodes of edge are just fine. This overlapping is caused by conversion from pdf to jpg. Labels are positioned as defined option auto. This can be changed to select option above. I will edit the code and image again ...
    – Zarko
    Commented May 17, 2015 at 23:36
  • Instead of converting, you can take a screenshot of the pdf.
    – Alenanno
    Commented May 18, 2015 at 0:03
3

A PSTricks solution:

\documentclass{article}

\usepackage{pstricks-add}
\usepackage{xfp}

\def\Label(#1)(#2)#3{%
  \psline{->}(#1)(#2)
  \pcline[linestyle = none, offset = 8pt](#1)(#2)
  \ncput*[nrot = :U]{$#3$}}
\def\pointL(#1)#2{%
  \psdot[dotstyle = o, fillcolor = green!80!blue](#1)
  \uput[180](#1){$#2$}}
\def\pointR(#1)#2{%
  \psdot[dotstyle = o, fillcolor = blue!60!red](#1)
  \uput[0](#1){$#2$}}

% parameters
\def\width{4}
\def\height{2}

\begin{document}

\begin{pspicture}(-0.5,-0.15)(\fpeval{\width+0.45},\fpeval{\height+0.4})
  \pnodes{P}(0,0)(\width,0)(0,\height)(\width,\height)
  \Label(P0)(P1){0.100}
  \Label(P2)(P1){0.200}
  \Label(P2)(P3){0.500}
  \pointL(P0){d_{1}}
  \pointL(P2){d_{0}}
  \pointR(P1){s_{1}}
  \pointR(P3){s_{0}}
\end{pspicture}

\end{document}

output

All you have to do is change the values of the parameters and the drawing will be adjusted accordingly.

1
  • This is impressive! I didn't know about pstricks. I just did a side-by-side comparison of this and tkz-berge, and the edges definitely look neater in this approach. Commented May 18, 2015 at 4:29
2

A try with MetaPost and its boxes package, included in a LuaLaTeX program for convenience. (It can be made independent of LuaLaTeX if desired.)

Edit Note that it needs luamplib version 2.10.1 or higher. Previous versions had a bug concerning the vardef macros defined with a suffix (i.e. the @# of the dot@# macro here).

\documentclass[border=2mm]{standalone}
\usepackage{luamplib}
  \mplibtextextlabel{enable}
\begin{document}
  \begin{mplibcode}
    input boxes
    color mygreen, myblue;
    mygreen = 16/51(red+2green+blue); myblue = 16/51(red+green+2blue);
    ahangle := 30; ahlength := 7bp; % Arrowheads stretched: more visible at s1
    numeric r, h, v; h = 4cm; v = 2.5cm;
    circmargin := 3pt;
    vardef dot@#(expr c) =
      fill bpath@# withcolor c; drawboxed(@#); draw bpath@#;
    enddef;
    beginfig(1);
      forsuffixes s = d0, d1, s0, s1: circleit.s(""); endfor
      s0.c - d0.c = (h, 0); d0.c - d1.c = s0.c - s1.c = (0, v);  
      forsuffixes i = 0, 1:
        dot.d[i](mygreen); dot.s[i](myblue);
        drawarrow d[i].e -- s[i].w;
      endfor
      drawarrow d0.c -- s1.c cutbefore bpath.d0 cutafter bpath.s1;
      label.lft("$d_0$", d0.w); label.rt("$s_0$", s0.e);
      label.lft("$d_1$", d1.w); label.rt("$s_1$", s1.e); 
      label.top("$0.005$", .5[d0.c, s0.c]);
      label.top("$0.002$", .5[d0.c, s1.c]) 
        rotatedaround (.5[d0.c, s1.c], angle(s1.c-d0.c)); 
      label.top("$0.100$", .5[d1.c, s1.c]);
    endfig; 
  \end{mplibcode}
\end{document}

The dot d0 is centered at the origin, the positions of the other nodes can be changed at will via the h and v parameters, as the dots' radius (value given to the circmargin variable defined in the boxes package).

enter image description here

You must log in to answer this question.

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