TikZ-将图的顶点的标签放置在节点内

TikZ-将图的顶点的标签放置在节点内

使用此代码,

\usepackage{tkz-base}
\usepackage{tkz-graph}
\usetikzlibrary{arrows.meta}

\begin{document}
\begin{tikzpicture}
    \tikzset{->-/.style={decoration={markings, mark=at position 0.5 with {\arrow{Latex}}},postaction={decorate}}}
    \GraphInit[vstyle=Classic]
    \renewcommand*{\VertexInnerSep}{0pt}
    \renewcommand*{\VertexSmallMinSize}{4pt}

    \Vertex[x=0,y=0,L=\footnotesize{Label 1},Lpos=below]{A}
    \Vertex[x=5,y=0,L=\footnotesize{Label 2},Lpos=below]{B}

    \Edge[label=\footnotesize{50},labelstyle={below,fill=none},style={->-,thick}](A)(B);
\end{tikzpicture}
\end{document}

我得到以下

1

然而,我想要的是这种风格的图表,但标签包含在圆角矩形中像这样

但是,此代码确实给出了圆角矩形标签,但是标签应用于顶点,而不是在顶点旁边,而非圆角矩形的标签只是大圆圈。

\usepackage{tkz-base}
\usepackage{tkz-graph}
\usetikzlibrary{arrows.meta}

\begin{document}
\begin{tikzpicture}[xscale=0.75,yscale=1]
    \tikzset{->-/.style={decoration={markings, mark=at position 0.5 with {\arrow{Latex}}},postaction={decorate}}}

    \Vertex[x=0,y=0,L=\footnotesize{Label 1},Lpos=below]{S}

    \tikzset{VertexStyle/.append style = {rectangle,rounded corners=9pt,inner sep=5pt}}
    \Vertex[x=5,y=0,L=\footnotesize{Label 2},Lpos=below]{T}

    \Edge[label=\footnotesize{50},labelstyle={below,fill=none},style={->-,thick}](S)(T)
\end{tikzpicture}
\end{document}

正如这里所见

2

我尝试使用\node[draw, rounded rectangle]{Label 1}标签,但是却出现错误:

\tikz@scope@opt 的使用与其定义不匹配。\pgfutil@ifnextchar ...1\def \pgfutil@reserved@a { #2}\def \pgfutil@reserved@...

答案1

我找不到合适的键/钩子来改变 Vertex 标签的样式,但由于它显然使用了 TikZ 自己的label键,所以我们可以使用该every label样式。

在这里,我只改变包含顶点的样式,scope以便其他标签不会受到影响。


我还冒昧地用普通的 TikZ 绘制了您的示例。

代碼 (tkz)

\documentclass[tikz]{standalone}
\usepackage{tkz-base}
\usepackage{tkz-graph}
\usetikzlibrary{arrows.meta, shapes.misc}

\begin{document}
\begin{tikzpicture}[
  ->-/.style={
    decoration={markings, mark=at position 0.5 with {\arrow{Latex}}},
    postaction={decorate}}]
\GraphInit[vstyle=Classic]
\renewcommand*{\VertexInnerSep}{0pt}
\renewcommand*{\VertexSmallMinSize}{4pt}

\begin{scope}[
  every label/.append style={draw, rounded rectangle, font=\footnotesize}]
\Vertex[x=0,y=0, L=Label 1, Lpos=below]{A}
\Vertex[x=5,y=0, L=Label 2, Lpos=below]{B}
\end{scope}

\SetUpEdge[labelstyle={below=2pt, font=\footnotesize, fill=none}]
\Edge[label=50, style={->-,thick}](A)(B);
\end{tikzpicture}
\end{document}

代码(TikZ)

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta, shapes.misc}
\tikzset{
  pics/arrow/.default=>,
  pics/arrow/.style={
    /tikz/sloped, /tikz/allow upside down, code=\pgfarrowdraw{#1}},
  dot/.style={circle, fill, draw, inner sep=.55mm},
  L/.style={label={[labelstyle, label distance=1mm]below:{#1}}},
  labelstyle/.style={draw, rounded rectangle, font=\footnotesize},
  ar/.style={edge node={
      pic {arrow=Latex}
      node[sloped, below=.5mm*sqrt 2+.5\pgflinewidth+1mm,
           font=\footnotesize] {#1}}}}
\begin{document}
\begin{tikzpicture}
\node[dot, L=Label 1] (l1) at (0,0) {};
\node[dot, L=Label 2] (l2) at (5,0) {};
\path[thick] (l1) edge[ar=50] (l2);
\end{tikzpicture}
\end{document}

输出(tkz)

enter image description here

输出(TikZ)

enter image description here

答案2

纯钛Z,您可以使用该选项label={[<style>]<position>:<text>}]使您的标签具有与节点一样的自己的样式。

enter image description here

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows.meta, decorations.markings}

\tikzset{->-/.style={decoration={markings, mark=at position 0.5 with {\arrow{Latex}}}, postaction={decorate}},
    dot/.style={fill, circle, inner sep=0, minimum size=4pt},
    mylabel/.style={draw, rounded corners=3mm, minimum height=6mm, outer sep=1mm}}

\begin{document}

\begin{tikzpicture}
\draw[->-](0,0)node[dot, label={[mylabel]left:Label 1}]{}--node[below]{50}(5,0)node[dot, label={[mylabel]right:Label 2}]{};
\end{tikzpicture}

\end{document}

答案3

@Sandy G 的回答(+1)有小幅变化,根据问题中显示的期望结果进行了调整:

\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, 
                decorations.markings, 
                shapes.misc}

\begin{document}

\begin{tikzpicture}[
    ->-/.style={decoration={markings, 
                            mark=at position 0.53 with {\arrow{Latex}}}, postaction={decorate}},
    dot/.style={fill, circle, inner sep=3pt, node contents={},
                label={[draw, rounded rectangle, outer sep=2pt,
                        font=\footnotesize]below:#1}},
                    ]
\draw[->-]  (0,0) node[dot=Label 1] 
                        -- node[below] {50} (5,0) 
                  node[dot=Label 2];
\end{tikzpicture}

enter image description here

相关内容