我怎样才能在边缘上方写文字?

我怎样才能在边缘上方写文字?

我有以下代码来绘制具有有向边的图形:

\begin{tikzpicture}
% vertices
\node[vertex] (1) at  (0,0) {$\times$};
\node[below left=1cm of 1] (A) {};
\node[below right=1cm of 1] (B) {};
\node[vertex] (2) at  (2,0) {$+$};
\node[below left=1cm of 2] (C) {};
\node[below right=1cm of 2] (D) {};
\node[vertex] (3) at  (4,0) {$\times$};
\node[below left=1cm of 3] (E) {};
\node[below right=1cm of 3] (F) {};
\node[vertex] (4) at  (1,2) {$+$};
\node[vertex] (5) at  (3,2) {$\times$};
\node[vertex] (6) at  (2,4) {$+$};
\node[above=1cm of 6] (G) {};
%edges
\draw[edge] (1) to (4);
\draw[edge] (1) to (5);
\draw[edge] (2) to (4);
\draw[edge] (3) to (5);
\draw[edge] (4) to (6);
\draw[edge] (5) to (6);
\draw[edge] (A) to (1);
\draw[edge] (B) to (1);
\draw[edge] (C) to (2);
\draw[edge] (D) to (2);
\draw[edge] (E) to (3);
\draw[edge] (F) to (3);
\draw[edge] (6) to (G);
\end{tikzpicture}

使用这些选项

\tikzset{vertex/.style = {shape=circle,draw,minimum size=1.5em}}
\tikzset{edge/.style = {->,> = latex'}}

得到下图

在此处输入图片描述

有什么方法可以在每条边的上方、下方添加文本吗?此外,我是否可以编写文本,使其相对于文档居中,而不是边缘本身居中?

答案1

在此处输入图片描述

\documentclass{standalone}
\usepackage{tikz}

\usetikzlibrary{calc,positioning,angles,arrows.meta,quotes,intersections}
\usetikzlibrary{through}

\begin{document}

\begin{tikzpicture}\tikzset{vertex/.style = {shape=circle,draw,minimum size=1.5em}}
\tikzset{edge/.style = {->}}
% vertices
\node[vertex] (1) at  (0,0) {$\times$};
\node[below left=1cm of 1] (A) {};
\node[below right=1cm of 1] (B) {};
\node[vertex] (2) at  (2,0) {$+$};
\node[below left=1cm of 2] (C) {};
\node[below right=1cm of 2] (D) {};
\node[vertex] (3) at  (4,0) {$\times$};
\node[below left=1cm of 3] (E) {};
\node[below right=1cm of 3] (F) {};
\node[vertex] (4) at  (1,2) {$+$};
\node[vertex] (5) at  (3,2) {$\times$};
\node[vertex] (6) at  (2,4) {$+$};
\node[above=1cm of 6] (G) {};
%edges
\draw[edge] (1) to node[left, sloped,xshift=4pt,yshift=4pt]{\tiny AA}(4);%<---instead of left can use right/ above/ below
\draw[edge] (1) to (5);
\draw[edge] (2) to (4);
\draw[edge] (3) to (5);
\draw[edge] (4) to (6);
\draw[edge] (5) to (6);
\draw[edge] (A) to (1);
\draw[edge] (B) to (1);
\draw[edge] (C) to (2);
\draw[edge] (D) to (2);
\draw[edge] (E) to (3);
\draw[edge] (F) to (3);
\draw[edge] (6) to (G);
\end{tikzpicture}
 \end{document}

相关内容