在 TikZ Drawing 中写入文本

在 TikZ Drawing 中写入文本

我需要在节点 4、0 和 3 下添加一个小文本。有人知道怎么做吗?这是我有的

\documentclass{article}

\usepackage{tikz}  
\usetikzlibrary{arrows.meta}  
\begin{document}  
\begin{tikzpicture}  
\begin{scope}[every node/.style={circle,thick,draw}]  
    \node (0) at (0,0) {0};  
    \node (1) at (1,1) {1};  
    \node (2) at (1,-1) {2};  
    \node (3) at (2,0) {3};  
    \node (4) at (-2,0) {4};  
\end{scope}  

\begin{scope}[>={Stealth[black]},
              every node/.style={fill=white,circle},
              every edge/.style={draw=red,very thick}]  
    \path [->] (0) edge (4);  
    \path [->] (0) edge (1);  
    \path [->] (1) edge (2);  
    \path [->] (0) edge (2);  
    \path [->] (1) edge (3);  
    \path [->] (2) edge (3);  
\end{scope}  
\end{tikzpicture}  
\end{document}  

答案1

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta}

\begin{document}
    \begin{tikzpicture}[
         C/.style = {circle,thick,draw, inner sep=2pt},
every edge/.style = {draw=red,very thick,-{Stealth[black]}},
                        ]
\node[C,label=below:text]   (0) at (0, 0) {0};
\node[C]                    (1) at (1, 1) {1};
\node[C]                    (2) at (1,-1) {2};
\node[C,label=below:text]   (3) at (2, 0) {3};
\node[C,label=below:text]   (4) at (-2,0) {4};
%
\draw[->]   (0) edge (4) 
            (0) edge (1)
            (1) edge (2) 
            (0) edge (2) 
            (1) edge (3)
            (2) edge (3);
    \end{tikzpicture}
\end{document}

在使用标签时,您需要考虑到标签实际上是节点,因此要为every node/.style受影响的标签定义主节点的样式。上面的代码给出:

在此处输入图片描述

答案2

[label=<position>:<text>]将选项格式与节点一起使用,如下所示:

\documentclass{article}

\usepackage{tikz}  
\usetikzlibrary{arrows.meta}  
\begin{document}  
\begin{tikzpicture}  
\begin{scope}[every node/.style={circle,thick,draw}]  
    \node[label=below:text] (0) at (0,0) {0};  
    \node (1) at (1,1) {1};  
    \node (2) at (1,-1) {2};  
    \node[label=below:text] (3) at (2,0) {3};  
    \node[label=below:text] (4) at (-2,0) {4};  
\end{scope}  

\begin{scope}[>={Stealth[black]},
              every node/.style={fill=white,circle},
              every edge/.style={draw=red,very thick}]  
    \path [->] (0) edge (4);  
    \path [->] (0) edge (1);  
    \path [->] (1) edge (2);  
    \path [->] (0) edge (2);  
    \path [->] (1) edge (3);  
    \path [->] (2) edge (3);  
\end{scope}  
\end{tikzpicture}  
\end{document} 

结果

相关内容