WeBWorK Main Forum

Embed image in PGML solution

Embed image in PGML solution

by Yuncong Chen -
Number of replies: 3
Hi, 

How do I embed images in PGML solution? I know how to embed images in PGML text by wrapping the PG command in BEGIN_TEXT and END_TEXT, but in solution if I do this the images will be visible to the students.

Thanks.
In reply to Yuncong Chen

Re: Embed image in PGML solution

by Alex Jordan -
In PGML or PGML_SOLUTION,

[@ @]

is used to surround commands (the kind of thing that you would put inside \{ \} if you were in a TEXT environment). Sometimes, you need to use an asterisk or two like

[@ @]*
or
[@ @]**

to get the desired effect. My weak understanding is that the [@ @] produces the code of the command inside, and then the asterisks somehow indicate that it should be executed. I don't remember exactly how it goes with the image command from PGgraphmacros, but one of these three enclosures should work. Remember to not use the \{ \} that you would use in TEXT mode.

Sometimes you may have trouble with this if your perl command itself uses square brackets, since PGML may be parsing them as PGML block delimiters. For instance if you were trying to show an image called $gr[0]. When that happens, I rename all such images as $gr0, etc., right before opening the PGML environment. It's an ugly solution, and I wouldn't discourage you from finding a way to use $gr[0] directly if you can.

For a full list of the PGML delimiters, go to github and look for %BlockDefs at about line 440.
In reply to Yuncong Chen

Re: Embed image in PGML solution

by Davide Cervone -
Alex is right, the [@ ... @] command can be used to do this. For example:
    [@ image(insertGraph($graph),
             width=>200,height=>200,tex_size=>480) @]*
where $graph is a graph object created from the macros in PGgraphicmacros.pl would do it.

Here is the meaning of the stars that follow the command. The command between the [@ ... @] is passed to Perl for execution, and the result is treated as a string that will be inserted into the PGML output. The stars control how that string is processed prior to insertion.

  • With no stars, the string is inserted so that it will appear verbatim; that is, any special characters are quoted so that they show up as themselves (works correctly for both HTML and TeX output). So if the result of the command was "Hi<br>there", you would get exactly that in the output (in particular, the <br> would not operate as an HTML line break, and you would see the actual less-than, br, and greater than in the output.

WIth one star, no escaping is performed, and the result is used as it is. So if the result of the command is already HTML formatted, you would want to use the star. In the example above, since the image() command returns formatted results already, you use the star to prevent PGML from escaping the formatting that is already part of the result.

With two stars, the result is processed for additional PGML commands. So if the result returned PGML that needed to be parsed and processed, you would use two stars.

Hope that clears things up a bit.