# How to draw a directed graph to show the relations among some notions?

I want to draw a picture as follow:

It is my first time to study how to draw a picture using LaTeX. I know several methods to draw a directed graph, but no one works. And I do not know how to draw two different arrows between two nodes.

By the way, in order to make the relation be clear, the nodes may not be placed like a matrix sometimes.

I used the Tikz to draw one, but there are many mistakes.

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,chains,matrix,positioning,scopes}
\begin{tikzpicture}[>=triangle 60]
\matrix[matrix of math nodes,column sep={60pt,between origins},row
sep={60pt,between origins},nodes={asymmetrical rectangle}] (s)
{
&|[name=NM1]| NM-CPA &|[name=NM2]| NM-CCA1 &|[name=NM3]| NM-CCA2 \\
&|[name=IND1]| IND-CPA &|[name=IND2]| IND-CCA1 &|[name=IND3]| IND-CCA2 \\
};
\draw[->] (NM3) edge (NM2)
(NM2) edge (NM1)
(IND3) edge (IND2)
(IND2) edge (IND1)
(NM3) edge (IND3)
(NM2) edge (IND2)
(NM1) edge (IND1)
;
\end{tikzpicture}

• Welcome to TeX - LaTeX! You can pass shifting options (xshift,yshift) to the edges to move them – Andrew Swann Jan 9 '18 at 8:35

something like this?

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, matrix, positioning}

\begin{document}

\begin{tikzpicture}[>={Triangle[]}]

\matrix[matrix of nodes,
column sep={60pt},
row sep={60pt},
nodes={rectangle, anchor=center}] (s)
{
|(NM1)| NM-CPA & |(NM2)| NM-CCA1 &|(NM3)| NM-CCA1 \\
|(IND1)| IND-CPA &|(IND2)| IND-CCA1 &|(IND3)| IND-CCA2 \\
};
\draw[->] (NM2) edge (NM1)
(IND3) edge (IND2)
(IND2) edge (IND1);

\draw[->] ([xshift=-3mm]NM1.south) -- node[midway, left] {3.1}([xshift=-3mm]IND1.north);

\draw[->] ([xshift=3mm]NM2.south) -- node[midway, left] {3.1}([xshift=3mm]IND2.north);

\draw[->] ([xshift=-3mm]NM3.south) -- node[midway, left] {3.1}([xshift=-3mm]IND3.north);
\draw[<-] ([xshift=3mm]NM3.south) -- node[midway, right] {3.3}([xshift=3mm]IND3.north);

\draw[->] (NM2.north east)--node[midway,above left]{3.7} coordinate (aux) (NM3.north west);
\draw ([shift={(-1mm,-2mm)}]aux)--([shift={(1mm,2mm)}]aux);

\draw[<-] (NM2.south east)--(NM3.south west);

\draw[<-] (NM1.south east)-- node[pos=.6, above right] {3.5}coordinate (aux) (IND2.north);
\draw ([shift={(-1mm,-2mm)}]aux)--([shift={(1mm,2mm)}]aux);

\draw[->] (NM1.south)-- node[pos=.4, below left] {3.6}coordinate (aux) (IND2.north west);
\draw ([shift={(-1mm,-2mm)}]aux)--([shift={(1mm,2mm)}]aux);
\draw ([shift={(-1mm,-2mm)}]aux)--([shift={(1mm,2mm)}]aux);

\end{tikzpicture}
\end{document}


• Thanks a lot, it looks good. I have some questions: What dose the "(s)" mean? And how can I put this picture at the center? how can I modify the position of the label? Can I use something like "[xshift=2mm]" instead of "above" or "left"? – TeamBright Jan 9 '18 at 15:09
• @TeamBright (s) (which comes from your code) is the name of the matrix. A tikzpicture is like a char, do you know how to center a letter? You can use above= x of ... with positioning library. Did you read introduction tutorials in pgfmanual. It's worth to read them if you want to use TikZ. – Ignasi Jan 9 '18 at 16:23

Here are two more solutions:

– one with pstricks;

– another with tikz-cd.

\documentclass{article}
\usepackage{pst-node}
\usepackage{tikz-cd}
\usetikzlibrary{decorations.markings}
\tikzset{degil/.style={
decoration={markings,
mark= at position 0.5 with {
\node[transform shape] (tempnode) {/};}},
postaction={decorate}}}

\begin{document}
%
$\texttt{pstricks: }\qquad\psset{arrows=->, arrowinset=0.25, linewidth=0.6pt, nodesep=5pt, labelsep=2pt, shortput =tablr} \everypsbox{\scriptstyle} \begin{psmatrix}[colsep = 2cm, rowsep = 1.5cm] \textrm{NM-CPA} & \textrm{NM-CCA1} & \textrm{NM-CCA1} \\ \textrm{IND-CPA} & \textrm{IND-CCA1} & \textrm{IND-CCA1} %% horizontal rules \ncline{1,2}{1,1} \ncline[offset = 1ex]{1,2}{1,3}\ncput{/}\naput[npos = 0.35]{3.7} \ncline[offset = 1ex]{1,3} {1,2} \ncline{2,2}{2,1}\ncline{2,3}{2,2} %% vertical rules \ncline{1,1}{2,1}\nbput{3.1} \ncline{1,2}{2,2}\naput{3.1} \psset{labelsep = 2.5ex} \ncline[offset = 1ex]{1,3}{2,3}\nbput{3.1} \ncline[offset = 1ex]{2,3}{1,3}\nbput{3.1} %% oblique rules \psset{nodesepA = 8.5pt, nodesepB = 8pt, offset = -1.4ex, nrot =: 0, labelsep = 0.5ex}% \ncline{1,1}{2,2}\nbput[npos = 0.3]{3.6}\ncput[nrot =: 22]{/} \ncline{2,2}{1,1}\nbput[npos = 0.3]{\rotatedown{3.5}}\ncput[nrot =: 22]{/} \end{psmatrix}$
\bigskip

$\texttt{tikz-cd: }\qquad\begin{tikzcd}[arrows =-stealth, column sep = 1.5cm, row sep = 1.2cm, sep = huge, cells = {inner ysep = 2ex}] \textrm{NM-CPA} \dar["3.1",swap]\drar[shift left = -1.4ex, "3.6", near start, swap, degil] & \textrm{NM-CCA1}\lar \rar[shift left = 1ex, "3.7", near start,degil] \dar["3.1"] & \textrm{NM-CCA1}\lar[shift left = 1ex]\dar[shift left = 1ex, "3.1"]\\% \textrm{IND-CPA} & \textrm{IND-CCA1}\lar\ular[shift left = -1.4ex, " 3.5", near start, swap, degil] & \textrm{IND-CCA1} \lar \uar[shift left = 1ex, "3.1"] \end{tikzcd}$

\end{document}


• – (en dash) instead of - (hyphen) makes the bullet not work. – Artificial Stupidity Jan 9 '18 at 22:33
• I prefer an en dash (French traditional typography). It's more discreet here. – Bernard Jan 9 '18 at 22:38

Another solution with tikz-cd.

To refine the position of the sloped arrows I've used start anchor=..., end anchor=..., whereas "/"{anchor=center,sloped} is taken from this answer.

\documentclass[a4paper,10pt]{article}
\usepackage{amsmath}
\usepackage{tikz-cd}
\usetikzlibrary{positioning, arrows.meta}
\tikzcdset{arrow style=tikz, diagrams={>=Stealth}}

\begin{document}
$\begin{tikzcd}[row sep=10ex,column sep=4em] \textrm{NM-CPA} \ar[d,"3.1"{left=2pt}] \ar[rd,"/"{anchor=center,sloped}, swap, "3.6"{below left=0pt and 6pt}, start anchor=-40, end anchor=160] & \textrm{NM-CCA1} \ar[l] \ar[d,"3.1"{right=2pt}] \ar[r,"/"{anchor=center,sloped},"3.7"{above=4pt}, shift left] & \textrm{NM-CCA1} \ar[l,shift left] \ar[d,"3.1"{left=2pt}, shift right] \\ \textrm{IND-CPA} & \textrm{IND-CCA1} \ar[l] \ar[lu,"/"{anchor=center,sloped}, start anchor=140, end anchor=-17, swap,"3.5"{above right=0pt and 6pt}] & \textrm{IND-CCA2} \ar[l] \ar[u,"3.3"{right=2pt}, shift right] \\ \end{tikzcd}$
\end{document}


• Very good for your job. Also I wanted use tikz-cd but my free time is very short. +1 – Sebastiano Jan 9 '18 at 21:34
• @Sebastiano You're welcome! – CarLaTeX Jan 9 '18 at 21:35

It can be a job for MetaPost as well, with its boxes package.

Included in a LuaLaTeX program for typesetting convenience.

\documentclass[border=5mm]{standalone}
\usepackage{luatex85,luamplib}
\begin{document}
\begin{mplibcode}

def drawmidbar(expr line, lmark, angl) =
for t = 0, 1:
draw (left -- right)
zscaled (.5lmark*unitvector(point 1 of line - point 0 of line))
rotated angl shifted point .5 of line;
endfor;
enddef;

input boxes; defaultdx := 2bp; defaultdy := 3bp;
len := 4mm; path harrow[], varrow[], darrow[];

beginfig(1);

boxit.ic1("IND-CCA1"); boxit.ic2("IND-CCA2"); boxit.ic("IND-CPA");
boxit.nc1("NM-CCA1"); boxit.nc2("NM-CCA2"); boxit.nc("NM-CPA");
nc1.c = .5[nc.c, nc2.c];
ic1.c = .5[ic.c, ic2.c];
nc.c - ic.c = (0, 2.5cm);
ic2.c - ic.c = nc2.c - nc.c = (9cm, 0);
drawunboxed(ic1, ic2, ic, nc1, nc2, nc);

drawarrow ic1.w--ic.e; drawarrow ic2.w -- ic1.e;
drawarrow nc1.w--nc.e;
harrow1 = .4[nc1.ne, nc1.e] -- .4[nc2.nw, nc2.w]; drawarrow harrow1;
label.ulft(btex 3.7 etex, point .5 of harrow1);
drawmidbar(harrow1, len, 60);
harrow2 = .4[nc2.sw, nc2.w] -- .4[nc1.se, nc1.e]; drawarrow harrow2;

varrow1 = .5[nc.sw, nc.s] -- .5[ic.nw, ic.n];
drawarrow varrow1; label.lft(btex 3.1 etex, point .5 of varrow1);
varrow2 = .5[nc1.s, nc1.se] -- .5[ic1.n, ic1.ne];
drawarrow varrow2; label.rt(btex 3.1 etex, point .5 of varrow2);
varrow3 = nc2.s -- ic2.n;
drawarrow varrow3; label.lft(btex 3.1 etex, point .5 of varrow3);
varrow4 = .4[ic2.n, ic2.ne] -- .4[nc2.s, nc2.se] ;
drawarrow varrow4; label.rt(btex 3.3 etex, point .5 of varrow4);

darrow1 = .75[nc.sw, nc.s] -- ic1.nw;
drawarrow darrow1; drawmidbar(darrow1, len, 90);
label.llft(btex 3.6 etex, point .3 of darrow1);
darrow2 = ic1.n -- nc.se;
drawarrow darrow2; drawmidbar(darrow2, len, 90);
label.urt(btex 3.5 etex, point .3 of darrow2);

endfig;

\end{mplibcode}
\end{document}