我想要绘制如下的图:
第一次研究用LaTeX画图,知道好几种画有向图的方法,但是都行不通,而且不知道怎么在两个节点之间画两个不同的箭头。
顺便说一句,为了使关系更清晰,节点有时可能不会像矩阵那样放置。
我用 Tikz 画了一个,但是有很多错误。
\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}
答案1
像这样吗?
\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}
答案2
这里还有两个解决方案:
– 一与pstricks
;
– 另一个是 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}
答案3
它也可以作为 MetaPost 及其boxes
软件包的工作。
包含在LuaLaTeX程序中,以方便排版。
\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}
答案4
另一个解决方案是tikz-cd
。
为了改进倾斜箭头的位置start anchor=..., end anchor=...
,我使用"/"{anchor=center,sloped}
了这个答案。
\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}