我如何使用 tikz 制作这样的图表?
我可以绘制圆圈节点和箭头,但无法改变它们的位置。(例如,从节点 1 开始并在节点 3 结束的位置)。
编辑:我更喜欢直箭头,而不是弯箭头,就像图中那样。
答案1
更直接的方法可能是使用graph
库(但需要 LuaLaTeX)。以下是使用库的代码graph
:
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{graphs,graphdrawing,arrows.meta}
\usegdlibrary{circular}
\begin{document}
\tikz[>=Stealth]\graph[simple necklace layout, nodes={circle,minimum size=.7cm,draw}, node sep=1cm]{
1,2,6,4,5,3;
1->[xshift=1pt,yshift=2pt]3 -> 2, 3 ->[xshift=-1pt,yshift=-2pt]1 -> 2;
3 -> 5 -> 6 ->[xshift=-1pt,yshift=-2pt] 4 ->[xshift=1pt,yshift=-2pt]5, 5 ->[xshift=-1pt,yshift=2pt] 4 ->[xshift=1pt,yshift=2pt] 6;
};
\end{document}
输出:
另一种可能性是使用matrix of nodes
如下内容:
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{matrix,arrows.meta}
\begin{document}
\begin{tikzpicture}[>=Stealth,->,line width=.7pt]
\matrix [matrix of math nodes,
column sep={1.5cm,between origins},
row sep={2.2cm,between origins},
nodes={circle, draw, minimum size=1cm}]
{
& |(1)| 1 & & |(2)| 2 & \\
& & |(3)| 3 & \\
& |(6)| 6 & & |(5)| 5 & \\
& & |(4)| 4 & \\};
\draw (1)--(2);
\draw (3)--(2);
\draw[transform canvas={xshift=2pt,yshift=2pt},shorten <= -1pt] (3)--(1);
\draw[transform canvas={xshift=-2pt,yshift=-2pt},shorten <= -1pt] (1)--(3);
\draw (3) -- (5);
\draw (5) -- (6);
\draw[transform canvas={xshift=2pt,yshift=-2pt},shorten >= -1pt] (4) -- (5);
\draw[transform canvas={xshift=-2pt,yshift=2pt},shorten >= -1pt] (5) -- (4);
\draw[transform canvas={xshift=2pt,yshift=2pt},shorten <= -1pt] (4) -- (6);
\draw[transform canvas={xshift=-2pt,yshift=-2pt},shorten <= -1pt] (6) -- (4);
\end{tikzpicture}
\end{document}
现在给出:
答案2
您可以使用问题评论中所述的 tikz 定位库。我在绘制箭头时使用了缩短代码来将它们连接到圆圈。因为当您移动它们时,箭头和圆圈之间会出现一个难看的白色空间。
%pdflatex
\documentclass[margin=0.5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\shift}{0.25ex}
\node [draw , circle] (1) at (-0.5,3) {1};
\node [draw , circle] (2) at (0.5,3) {2};
\node [draw , circle] (3) at (0,2) {3};
\node [draw , circle] (4) at (0,0) {$4$};
\node [draw , circle] (5) at (0.5,1) {$5$};
\node [draw , circle] (6) at (-0.5,1) {$6$};
\draw [->] (1) -- (2);
\draw [->,shorten <=-1pt, transform canvas={xshift=-\shift,yshift=-\shift}] (1) -- (3);
\draw [->,shorten <=-1pt, transform canvas={xshift=\shift,yshift=\shift}] (3) -- (1);
\draw [->] (3) -- (2);
\draw [->] (3) -- (5);
\draw [->] (5) -- (6);
\draw [->,shorten <=-1pt, transform canvas={xshift=-\shift,yshift=\shift}] (4) -- (5);
\draw [->,shorten <=-1pt, transform canvas={xshift=\shift,yshift=-\shift}] (5) -- (4);
\draw [->,shorten <=-1pt, transform canvas={xshift=-\shift,yshift=-\shift}] (6) -- (4);
\draw [->,shorten <=-1pt, transform canvas={xshift=\shift,yshift=\shift}] (4) -- (6);
\end{tikzpicture}
\end{document}
答案3
在第一条评论的帮助下,我做出了我想要的东西(我做了一些与原始绘图相似的东西)!这是代码。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,shapes.misc,patterns,shapes.symbols,decorations.pathreplacing,decorations}
\begin{document}
%===Credits to JLDiaz for the following part=========
\pgfdeclaredecoration{sl}{initial}{
\state{initial}[width=\pgfdecoratedpathlength-1sp]{
\pgfmoveto{\pgfpointorigin}
}
\state{final}{
\pgflineto{\pgfpointorigin}
}
}
\tikzset{parallel arrow/.style={->,
shorten >=2mm, shorten <=2mm,
decoration={sl,raise=1mm},decorate}}
%===================================================================
\tikzset{shorten arrow/.style = {->, shorten >=2 mm, shorten <=2mm}}
\begin{tikzpicture}
\draw (0,3) circle (8pt)node[](1){1} ;
\draw (3,6) circle (8pt)node[](4){4} ;
\draw (6,3) circle (8pt)node[](3){3} ;
\draw (0,0) circle (8pt)node[](2){2} ;
\draw (3,3) circle (8pt)node[](5){5} ;
\draw (3,0) circle (8pt)node[](6){6} ;
\foreach \x in {1,3,5}
\path (4) edge[shorten arrow] (\x);
\foreach \x/\y in {1/2,2/1,2/6,6/2,5/6,6/5}
\path (\x) edge [parallel arrow] (\y);
\path (2)edge[shorten arrow] (5);
\path (5)edge[shorten arrow] (3);
\end{tikzpicture}
\end{document}
答案4
解决方案tikz-cd:
% arara: pdflatex
\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\[\begin{tikzcd}[%
row sep = 20mm, column sep = 20mm, % both optional, adapt as you please
cells={nodes={circle, draw}},
every arrow/.append style={shorten <= 1mm, shorten >= 1mm}] % both optional. Default connects the arrow directly to the circles
& 4 \arrow{dl} \arrow{d} \arrow{dr} & \\
1 \arrow[shift left]{d} & 5 \arrow[shift left]{d} \arrow{r} & 3 \\
2 \arrow[shift left]{u} \arrow{ur} \arrow[shift left]{r} & 6 \arrow[shift left]{l} \arrow[shift left]{u} &
\end{tikzcd}\]
\end{document}
如果默认距离shift left
不适合您,您可以添加=any_measure
它。在这种情况下,我将定义一种样式shifted
,以便只在一个地方更改值。