WeBWorK Main Forum

TikZ graph subroutine in PGcourse.pl produces errors

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

 $graph->BEGIN_TIKZ

END_TIKZ

block

       

1. ERROR caught by Translator while processing problem file:tmpEdit/local/Library/Rochester/setAlgebra19FunTransforms/p1.pg.swansoac.tmp
****************
ERRORS from evaluating PG file: 
'require' trapped by operation mask at /usr/lib/i386-linux-gnu/perl/5.26/Encode.pm line 5 Died within Encode::Alias::find_alias called at line 114 of /usr/lib/i386-linux-gnu/perl/5.26/Encode.pm from within Encode::getEncoding called at line 132 of /usr/lib/i386-linux-gnu/perl/5.26/Encode.pm from within Encode::find_encoding called at line 166 of /usr/lib/i386-linux-gnu/perl/5.26/Encode.pm from within Encode::encode called at line 93 of /opt/webwork/webwork2/lib/Apache/WeBWorK.pm Compilation failed in require at /usr/lib/i386-linux-gnu/perl/5.26/Encode/Alias.pm line 22




In reply to Andrew Swanson

Re: TikZ graph subroutine in PGcourse.pl produces errors

by Andrew Swanson -
To narrow things down I took it out of PGcourse.pl - now I'm getting a different error message - which I think is the same as the first one I got.

I'm attaching the edited problem and the macro file I created.  Hmmm, seems I can only attach one file. I'll attach the edited problem in the reply and the macro file here.

The error message now says:

In reply to Andrew Swanson

Re: TikZ graph subroutine in PGcourse.pl produces errors

by Glenn Rice -
I would not recommend putting this in PGcourse.pl. PGcourse.pl is loaded by all problems, and is not intended for this use. It would be better to create another macro for this purpose, and only load it in those files that use it.

You will still see the same problem in another macro file though. Macros are not parsed in the same way that problems are. They are not directly passed into the translator as problems are, and are instead loaded via loadMacros. This means that the substitutions made by the translator such as the BEGIN_TIKZ/END_TIKZ will not work. The same is true of BEGIN_TEXT/END_TEXT, BEGIN_PGML/END_PGML, etc. So you will need to call the method for that directly. So you will need to change
$graph->BEGIN_TIKZ
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.


In reply to Andrew Swanson

Re: TikZ graph subroutine in PGcourse.pl produces errors

by Glenn Rice -

Any macro file should define an init method.  It needs to be named _filename_init.  The PGcourse.pl macro does this too.  It is used by webwork to prevent the macro from being loaded more than once.

The line

$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.