我在乳胶中画了一个这样的图表。但我也需要添加带有数字的垂直线。我该如何添加它?
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}[hbt!]
\begin{center}
\centering
\begin{tikzpicture}
[xshift=-3cm,ultra thick,node distance=1cm]
\node[rednode](c1)[xshift=0cm]{$5$};
\node[rednode](c2)[xshift=3cm]{$4$};
\node[rednode](r1)[xshift=+2cm,yshift=+4cm]{$7$};
\node[rednode](r2)[xshift=-2cm,yshift=3cm]{$6$};
\node[rednode](r3)[xshift=-5.4cm,yshift=-3cm]{${3}$};
\node[rednode](r4)[xshift=0cm,yshift=-4cm]{${2}$};
\node[rednode](r5)[xshift=5cm,yshift=-5cm]{${1}$};
\draw[<->,black] (r1)-- (c2);
\draw[<->,black] (r1)-- (r3);
\draw[<->,black] (r1)-- (r4);
\draw[<->,black] (r2)-- (r1);
\draw[<->,black] (r2)-- (r3);
\draw[<->,black] (r2)-- (r4);
\draw[<->,black] (r3)-- (r4);
\draw[<->,black] (r3)-- (c1);
\draw[<->,black] (r4)-- (r5);
\draw[<->,black] (r5)-- (c1);
\draw[<->,black] (r5)-- (c2);
\draw[->,black](r3)edge[in=-20,out=60,loop below]node[below right]{}();
\draw[->,black](r5)edge[in=-20,out=60,loop below]node[below right]{}();
\end{tikzpicture}
\caption{ }
\label{}
\end{center}
\end{figure}
\end{document
答案1
一般来说,你已经知道了画出这样的线所需的一切。你知道如何添加节点,也知道如何在节点(或坐标)之间画箭头。不需要再多说什么了。
但是还有其他技巧可以(可能)简化事情,例如使用 for 循环来绘制线条旁边的数字。下面我使用循环来绘制图形中的连接和线条旁边的数字。
我也没有使用xshift
和yshift
,而是直接指定节点的坐标。(对我来说似乎更自然,但可能主要是个人喜好问题。)
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
ultra thick,
node distance=1cm,
rednode/.style={fill=red!20,circle,draw=red} % placeholder style
]
\node[rednode] (c1) {$5$};
\node[rednode] (c2) at (3,0) {$4$};
\node[rednode] (r1) at (2,4) {$7$};
\node[rednode] (r2) at (-2,3) {$6$};
\node[rednode] (r3) at (-5.4, -3) {$3$};
\node[rednode] (r4) at (0,-4) {$2$};
\node[rednode] (r5) at (5,-5) {$1$};
\foreach \x/\y in {r1/c2,r1/r3,r1/r4,
r2/r1,r2/r3,r2/r4,
r3/r4,r3/c1,
r4/r5,
r5/c1,r5/c2}
\draw [<->] (\x) -- (\y);
% loop below sets the in and out angles
\draw[->] (r3) edge[loop below] ();
\draw[->] (r5) edge[loop below] ();
\draw [->] (-6,-6) -- +(0,11) node[left, font=\Large] {$s$};
% specify the y-values where you want numbers on the axis here
\foreach [count=\i] \y in {-5,-2,1,4}
\node[left=5pt,font=\Large] at (-6,\y) {$\i$};
\end{tikzpicture}
\end{document}
答案2
您的代码没有显示您提供的图表。我也不知道rednode
是什么。由于您已经用箭头连接了节点,我不确定您的实际问题是什么?我认为您可能想要的是:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}[hbt!]
\begin{center}
\centering
\begin{tikzpicture}
[xshift=-3cm,ultra thick,node distance=1cm]
\node[](c1)[xshift=0cm]{$5$};
\node[](c2)[xshift=3cm]{$4$};
\node[](r1)[xshift=+2cm,yshift=+4cm]{$7$};
\node[](r2)[xshift=-2cm,yshift=3cm]{$6$};
\node[](r3)[xshift=-5.4cm,yshift=-3cm]{${3}$};
\node[](r4)[xshift=0cm,yshift=-4cm]{${2}$};
\node[](r5)[xshift=5cm,yshift=-5cm]{${1}$};
%nodes of arrow
\node[](s)[xshift=-7cm,yshift=3cm,label={[xshift=-0.5cm, yshift=-0.5cm]\huge{s}}]{};
\node[](s0)[xshift=-7cm,yshift=-5cm]{};
\draw[<->,black] (r1)-- (c2);
\draw[<->,black] (r1)-- (r3);
\draw[<->,black] (r1)-- (r4);
\draw[<->,black] (r2)-- (r1);
\draw[<->,black] (r2)-- (r3);
\draw[<->,black] (r2)-- (r4);
\draw[<->,black] (r3)-- (r4);
\draw[<->,black] (r3)-- (c1);
\draw[<->,black] (r4)-- (r5);
\draw[<->,black] (r5)-- (c1);
\draw[<->,black] (r5)-- (c2);
\draw[->,black](r3)edge[in=-20,out=60,loop below]node[below right]{}();
\draw[->,black](r5)edge[in=-20,out=60,loop below]node[below right]{}();
%arrow plus labels
\draw[->,black] (s0)-- (s) node[xshift=-0.5cm, yshift=-0.5cm,label={[xshift=-0.5cm, yshift=-7.5cm]\huge1},label={[xshift=-0.5cm, yshift=-5.5cm]\huge2},label={[xshift=-0.5cm, yshift=-3.5cm]\huge3},label={[xshift=-0.5cm, yshift=-1.5cm]\huge4}]{};
\end{tikzpicture}
\caption{ }
\label{}
\end{center}
\end{figure}
\end{document}