## WeBWorK Main Forum

### TikZ graph subroutine in PGcourse.pl produces errors

by Andrew Swanson -
Number of replies: 6

I have a problem I've been converting to TikZ graphics. Since it includes several graphs, I made a subroutine to do that.

It worked great, and I thought with some modification, this subroutine would be really useful in lots of problems.

Then I tried moving the subroutine into my PGcourse.pl.

The problem (and all other problems) produces an error message (I've seen several different ones as I've tried to narrow down what in the subroutine is doing it.

As far as I can tell, the problem occurs in the

to
$graph->tex(<< "END_TIKZ"); You will also need to make sure the the later END_TIKZ is at the beginning of the line, and not indented as you have it. Another change you will need to make due to the different way that problems and macros are parsed is that you will need to escape your backslashes in the strings. So everywhere that you use a backslash in a string, it will need to be a double backslash. For example, instead of$functiongraph.="\addplot [samples=100,color=blue]{$exp};"; you will need$functiongraph.="\\addplot [samples=100,color=blue]{$exp};"; (Also note that you have to many semicolons. You have a semicolon inside the string here, and another where it is used in the BEGIN_TIKZ/END_TIKZ construction.) On a TikZ side note, you should not load both the decorations and snakes TikZ libraries. The snakes library is deprecated, and superseded by the decorations library. On a PG problem coding side note, you should not be using BEGIN_TEXT/END_TEXT. You should be using BEGIN_PGML/END_PGML. In reply to Glenn Rice ### Re: TikZ graph subroutine in PGcourse.pl produces errors by Glenn Rice - I have attached a version of your macro that works. In reply to Glenn Rice ### Re: TikZ graph subroutine in PGcourse.pl produces errors by Andrew Swanson - Thanks! That helps a lot! I had wondered how the fact that a macro was .pl rather than .pg would make a difference. A few questions: I never would have known to use "sub _TikzTransform_init { }". What does it do? I don't remember seeing anything about "addToPreamble" in the TikZ documentation, what does this line do?$graph->addToPreamble("\\pgfplotsset{compat=1.16}");

I had wondered about those backslashes, since I'd already determined with a later version of this subroutine that I would need to change ~~$in problem files into \$.

As I said, I'm converting the problem from an older version - changing to PGML has been done since posting.

Thanks again for the quick response.

$graph->addToPreamble("\\pgfplotsset{compat=1.16}"); adds "\pgfplotset{compat=1.16}" to the preamble of the generated tex file used to create the image. Any time you use the pgfplots package you should set the compat level. See the pgfplots documentation about that. The "$graph->addToPreamble" part is not a TikZ thing.  That is a PGtikz.pl macro thing.  It is documented in that file and in the PGlateximage.pl macro (both of which use the same underlying module -- LaTeXImage.pm).  See https://webwork.maa.org/pod/pg/macros/PGtikz.html.