1

Using this code,

\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}

I get the following

1

What I would like however, is to have this style of graph but with the labels contained in rounded rectangles like this.

This code however does give the rounded rectangle labels, however the labels are applied at the vertices, instead of next to them, and those that are not rounded rectangles are just large circles.

\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}

As seen here

2

I tried using \node[draw, rounded rectangle]{Label 1} for the label, however that just gives the error:

Use of \tikz@scope@opt doesn't match its definition. \pgfutil@ifnextchar ...1\def \pgfutil@reserved@a { #2}\def \pgfutil@reserved@...

Harry
  • 13
  • 1
    rounded rectangle is a shape. Did you load the TikZ library shapes.misc? You can't use \node inside a label's text. That's just that, text. \Edge[label=\footnotesize 50, labelstyle={below, draw, rounded rectangle}](A)(B); works for me. – Qrrbrbirlbel Dec 07 '22 at 23:00
  • Yes, though only for the vertex lables, and not necessarily all at once. – Harry Dec 07 '22 at 23:01
  • You can do \SetUpEdge[labelstyle={below, draw, rounded rectangle, font=\footnotesize}]\Edge[label=50, style={->-,thick}](A)(B);. Doesn't look like the label for Vertices is customizable. – Qrrbrbirlbel Dec 07 '22 at 23:07
  • What would be the vertex equivalent of the edge label? Since the code for the vertex is different to that of an edge. – Harry Dec 07 '22 at 23:08
  • It was just the vertex labels I was trying to do. How would I resize and position a separate rounded rectangle node behind the label instead? – Harry Dec 07 '22 at 23:09

3 Answers3

2

In plain TikZ, you can use the option label={[<style>]<position>:<text>}] to make your labels have their own styles as nodes.

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->-node[dot, label={[mylabel]left:Label 1}]{}--node[below]{50}(5,0)node[dot, label={[mylabel]right:Label 2}]{}; \end{tikzpicture}

\end{document}

Sandy G
  • 42,558
1

I couldn't find a proper key/hook to change the style of a Vertex's label but since it apparently uses TikZ's own label key we can use the every label style for that.

Here, I'm changing the style only for the scope that contains the vertices so that other labels won't be affected by it.


I've taken the liberty to also draw your example with plain TikZ.

Code (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}] \Edgelabel=50, style={->-,thick}(B); \end{tikzpicture} \end{document}

Code (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}

Output (tkz)

enter image description here

Output (TikZ)

enter image description here

Qrrbrbirlbel
  • 119,821
1

A small variation of nice @Sandy G answer (+1), adjusted to desired result shown in the question:

\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

Zarko
  • 296,517