![我怎样才能在边缘上方写文字?](https://linux22.com/image/419614/%E6%88%91%E6%80%8E%E6%A0%B7%E6%89%8D%E8%83%BD%E5%9C%A8%E8%BE%B9%E7%BC%98%E4%B8%8A%E6%96%B9%E5%86%99%E6%96%87%E5%AD%97%EF%BC%9F.png)
我有以下代码来绘制具有有向边的图形:
\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}