1

I'm drawing graphs in which I want to label vertices (e.g. x, y, z, w, etc), but also attach the value of a function calculated at each vertex (e.g. 4, -3, 2, 7 corresponding to vertices x, y, z, w etc). I'd like to achieve this by having x inside the vertex and the value of the function on the outside, though if there are other ways of achieving this easily I'd be open to that.

Is it possible to use the tkz-graph package to achieve this? I have read the documentation and (notwithstanding that my French is very rusty), I haven't been able to figure this out. Also open to alternative (simple & easy) packages that would allow me to achieve this.

(My level of LaTeX is $< \epsilon$)

\documentclass[10pt,border=3mm,tikz]{standalone}
\usepackage{tkz-graph}

\begin{document}
 \begin{tikzpicture}
    \GraphInit[vstyle=Normal]
    \SetGraphUnit{2}
    \SetVertexMath
    \Vertex{x}
    \Vertex[LabelOut, L=$4$, Lpos=90]{x} % this just replaces the internal label doesn't add an outside label
    \EA(x){y}
    \NOEA(y){z}
    \SOEA(z){w}
    \Edges(x,y,z,w,y,z) 
 \end{tikzpicture}
\end{document}

result

6
  • Welcome. // Unfortunately, compiling your code fragment results in Errors starting with "! LaTeX Error: Environment tikzpicture undefined.". Please make your code ready to compile. Thank you
    – MS-SPO
    Nov 25, 2023 at 8:19
  • Thanks. I think you need to load the package, so I added that \usepackage line
    – Sharanjit
    Nov 25, 2023 at 9:38
  • Thank you. You are on a good track: did you try to compile it? It still can‘t …
    – MS-SPO
    Nov 25, 2023 at 9:44
  • "(My level of LaTeX is $< \epsilon$)" // For Latex I suggest getting one of the books, or use this online book: en.wikibooks.org/wiki/LaTeX, or Overleaf overleaf.com/learn/latex/Learn_LaTeX_in_30_minutes . // For Tikz I suggest e.g. the tutorials in the pgf-manual tikz.dev , Overleaf overleaf.com/learn/latex/TikZ_package#Introduction etc. // With this background you can at least walk through the examples in the tkz-manual, which basically follows the Tikz-path.
    – MS-SPO
    Nov 25, 2023 at 10:59
  • I've been trying to avoid delving too deep into tikz, and instead relying on the packages built on top, like tkz-graph and tikz-cd, as my focus is studying maths rather than LaTeX, but it looks like I'm going to have to study tikz.
    – Sharanjit
    Nov 25, 2023 at 13:57

1 Answer 1

2

Here is one way to do it: mixing tkz-graph (built on Tikz) with plain Tikz.

Some hints

Tikz needs a semicolon ; at each end of a line to know "this path shall finish here". Think of a path being a series of commands to be executed, which we perceive as "drawing something". tkz-graph already provides this ; internally, so you can write them without this indicator. (And you can also mix both approaches.)

So in the first part just draw your structure with tkz-graph:

    \Vertex{x}      % default position: at (0,0)
    \EA     (x){y}  % EAst of x
    \NOEA   (y){z}  % NOrthEAst of y
    \SOEA   (z){w}  % SOuthEAst of z
    
    \Edges(x,y,z,w,y,z) 

To place your labels just use ordinary \nodes from Tikz, like so:

  • place a \node
  • at the coordinate from (x), previously defined
  • and put as text { }
  • some Latex-compliant text, here in math mode $4$
  • and finish this path, i.e. this single command, by ;
    % ~~~ and now some ordinary Tikz to place text in math mode $ $ ~~~
    %     kindly watch the semicolon ; needed here
    \node[abv]  at (x) {$4$};
    \node[bel]  at (y) {$-3$};
...

The styles abv and bel are defined in the beginning, and just shift the label a bit up and down, i.e. into y-direction. (Perhaps it's also available as a shortcut in tkz-graph.)

 \begin{tikzpicture}[   % defining some own styles
    abv/.style={yshift=6mm},
    bel/.style={yshift=-6mm},
 ]
...

Final results

result

\documentclass[10pt,border=3mm,tikz]{standalone}
\usepackage{tkz-graph}

\begin{document}
 \begin{tikzpicture}[   % defining some own styles
    abv/.style={yshift=6mm},
    bel/.style={yshift=-6mm},
 ]
    \GraphInit[vstyle=Normal]
    \SetGraphUnit{2}
    \SetVertexMath
    
    \Vertex{x}      % default position: at (0,0)
    \EA     (x){y}  % EAst of x
    \NOEA   (y){z}  % NOrthEAst of y
    \SOEA   (z){w}  % SOuthEAst of z
    
    \Edges(x,y,z,w,y,z) 
    
    % ~~~ and now some ordinary Tikz to place text in math mode $ $ ~~~
    %     kindly watch the semicolon ; needed here
    \node[abv]  at (x) {$4$};
    \node[bel]  at (y) {$-3$};
    \node[abv]  at (z) {$2$};
    \node[bel]  at (w) {$7$};
 \end{tikzpicture}
\end{document}
2
  • Thanks. It sounds like there's no obvious trick within tkz-graph to add multiple vertex labels that I'm not using. Thank you also for the explanation of each of the steps - e.g. the importance of ; . I'll try this out.
    – Sharanjit
    Nov 25, 2023 at 13:55
  • 1
    Right, tkz-graph allows some style changes on the vertices, but no double-label so to say, as far as I see it.
    – MS-SPO
    Nov 25, 2023 at 16:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .