WeBWorK Problems

cannot label edge in tikz graph

cannot label edge in tikz graph

by Joseph Pedersen -
Number of replies: 4

The code below makes the edge red, but does not add the label "foo", as it does on the example posted https://tikz.dev/tikz-graphs

Is there something about PG that requires a different syntax to label the edge?

DOCUMENT();

loadMacros('PGstandard.pl', 'PGML.pl',"MathObjects.pl",

"parserFunction.pl", 'PGtikz.pl','PGcourse.pl');

# whether or not to graph this edge

$bd = random(0,1);

# Create the graph

$graph_image = createTikZImage();

$graph_image->tikzLibraries("graphs", "quotes");

$graph_image->BEGIN_TIKZ

  \node (a) at (0,0) {\(S_1\)};

  \node (b) at (0,4) {\(S_2\)};

  \node (c) at (3,0) {\(D_1\)};

  \node (d) at (3,4) {\(D_2\)};

  \graph { (a) -> (c);}

  \graph { (a) -> [red, "foo"] (d);}

  \graph { (b) -> (c);}

  \ifdim 1 pt = $bd pt\relax \graph {(b) -> (d);};\fi

END_TIKZ

BEGIN_PGML

Here's a graph

>> [@ image($graph_image, width => 300, tex_size => 450) @]* <<

END_PGML

ENDDOCUMENT();

In reply to Joseph Pedersen

Re: cannot label edge in tikz graph

by Glenn Rice -
I think that you are misinterpreting something in the PGF/TikZ manual. Your code also doesn't work directly in TeX to do what you want. I am not entirely certain where you want the "foo" label to appear, but to add it you would need to use a \node. In the syntax ->[red] the things in the brackets are options that apply to the -> preceeding it. If you add "foo" in the brackets, then TeX complains I do not know the key '/tikz/"{foo}"' and I am going to ignore it.
In reply to Glenn Rice

Re: cannot label edge in tikz graph

by Joseph Pedersen -
I have never used TeX directly, but in LaTeX, this works:
\documentclass[tikz]{standalone}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{graphs,quotes}
\tikz \graph {
a ->[red, "foo"] b;
};
\end{document}

This example was adapted from here:
https://tikz.dev/tikz-graphs#sec-19.2.3
In reply to Joseph Pedersen

Re: cannot label edge in tikz graph

by Joseph Pedersen -

From the documentation, it seems like putting a string in the options to be used as a label is part of the quotes library.

https://tikz.dev/tikz-graphs#sec-19.6.1

In reply to Joseph Pedersen

Re: cannot label edge in tikz graph

by Glenn Rice -

I was generically referring to TeX as the underlying system.  I used pdflatex to test your code.

You didn't refer to the quotes library before, and I didn't add that in my testing.  With that added, the "foo" label does appear.

The problem is how you are adding the "graphs" and "quotes" libraries.  See the documentation in PGtikz.pl.  The tikzLibraries method only takes one string argument.  The example in the documentation is

$image->tikzLibraries("arrows.meta,calc");
So change
$graph_image->tikzLibraries("graphs", "quotes");
to
$graph_image->tikzLibraries("graphs,quotes");