Difference between revisions of "TikZImages"

From WeBWorK_wiki
Jump to navigation Jump to search
(added historical tag and gave updated problem link)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
  +
{{historical}}
  +
  +
<p style="font-size: 120%;font-weight:bold">This problem has been replaced with [https://openwebwork.github.io/pg-docs/sample-problems/problem-techniques/TikZImages.html a newer version of this problem]</p>
 
<h2>Graphic Images, TikZImages</h2>
 
<h2>Graphic Images, TikZImages</h2>
   
Line 48: Line 51:
 
$graph_image = createTikZImage();
 
$graph_image = createTikZImage();
 
$graph_image->tikzLibraries("arrows.meta");
 
$graph_image->tikzLibraries("arrows.meta");
  +
  +
# Randomization
  +
$a = non_zero_random(-6,6); # horizonatal translation
  +
$b = random(-4,4); # vertical translation
   
 
$graph_image->BEGIN_TIKZ
 
$graph_image->BEGIN_TIKZ
Line 68: Line 75:
 
<li>In certain cases the 'svg' creation methods do not give the correct output, and so in those cases a 'png' image may be generated instead by adding <tt>$graph_image->ext('png');</tt>.</li>
 
<li>In certain cases the 'svg' creation methods do not give the correct output, and so in those cases a 'png' image may be generated instead by adding <tt>$graph_image->ext('png');</tt>.</li>
 
<li>The <tt>$graph_image->tikzLibraries("arrows.meta");</tt> will load the <tt>arrows.meta</tt> Tikz library.</li>
 
<li>The <tt>$graph_image->tikzLibraries("arrows.meta");</tt> will load the <tt>arrows.meta</tt> Tikz library.</li>
  +
<li>pg variables <tt>$a</tt> and <tt>$b</tt> are defined for use in the TikZ code that follows. If the TikZ code references non-existent pg variables the image creation fails silently.</li>
 
<li>The actual tikz image is built between <tt>$graph_image->BEGIN_TIKZ</tt> and <tt>END_TIKZ</tt> </li>
 
<li>The actual tikz image is built between <tt>$graph_image->BEGIN_TIKZ</tt> and <tt>END_TIKZ</tt> </li>
</p>
 
<p>
 
Notes: on using this and related Contexts.
 
 
</p>
 
</p>
   
Line 84: Line 89:
 
BEGIN_PGML
 
BEGIN_PGML
   
[@ image(insertGraph($graph_image), width => 300, tex_size => 1000) @]*
+
[@ image($graph_image, width => 300, tex_size => 1000) @]*
   
 
END_PGML
 
END_PGML
Line 91: Line 96:
 
<p>
 
<p>
 
<b>Main Text:</b>
 
<b>Main Text:</b>
This is how to insert the tikz image. Note the <tt>width</tt> and <tt>tex_size</tt> parameters can change the size of the image on the web and as hardcopy.
+
This is how to insert the tikz image. Note the <tt>width</tt> and <tt>tex_size</tt> parameters can change the size of the image on the web and as hardcopy.
 
</p>
 
</p>
  +
<p>Note that you may want to call <tt>$image = image($graph_image, width => 300, tex_size => 1000)</tt> above and use <tt>[@ $image @]*</tt> here while developing the problem. If the <tt>image</tt> call occurs inside the BEGIN_PGML/END_PGML section, then you will not get the warnings from failed image generation. You will instead get no image, and no reason as to why.
 
</td>
 
</td>
 
</tr>
 
</tr>

Latest revision as of 09:49, 29 June 2023

This article has been retained as a historical document. It is not up-to-date and the formatting may be lacking. Use the information herein with caution.

This problem has been replaced with a newer version of this problem

Graphic Images, TikZImages


This example shows how to create an image/plot using Tikz.

Problem Techniques Index

PG problem file Explanation
DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"PGtikz.pl"
);
TEXT(beginproblem());

Initialization: It is important to include the PGtikz.pl macro.

$graph_image = createTikZImage();
$graph_image->tikzLibraries("arrows.meta");

# Randomization
$a = non_zero_random(-6,6);  # horizonatal translation
$b = random(-4,4);           # vertical translation

$graph_image->BEGIN_TIKZ
\draw[<->,thick] (-11,0) -- (11,0) node[above left,outer sep=2pt]{\(x\)};
\draw[<->,thick] (0,-11) -- (0,11) node[below right,outer sep=2pt]{\(y\)};
\foreach \x in {-10,-8,...,-2,2,4,...,10} \draw[thin] (\x,5pt) -- (\x,-5pt) node[below]{\(\x\)};
\foreach \y in {-10,-8,...,-2,2,4,...,10} \draw[thin] (5pt,\y) -- (-5pt,\y) node[left]{\(\y\)};
\draw[<->,red] plot[domain={-3.2+$a}:{3.2+$a}] (\x,{pow(\x-$a,2)+$b});
END_TIKZ


Setup:

  • The createTikZImage() function creates an image to be built using tikz.
  • By default an 'svg' image will be generated, which will generally look better than a 'png image.
  • In certain cases the 'svg' creation methods do not give the correct output, and so in those cases a 'png' image may be generated instead by adding $graph_image->ext('png');.
  • The $graph_image->tikzLibraries("arrows.meta"); will load the arrows.meta Tikz library.
  • pg variables $a and $b are defined for use in the TikZ code that follows. If the TikZ code references non-existent pg variables the image creation fails silently.
  • The actual tikz image is built between $graph_image->BEGIN_TIKZ and END_TIKZ
BEGIN_PGML

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

END_PGML

Main Text: This is how to insert the tikz image. Note the width and tex_size parameters can change the size of the image on the web and as hardcopy.

Note that you may want to call $image = image($graph_image, width => 300, tex_size => 1000) above and use [@ $image @]* here while developing the problem. If the image call occurs inside the BEGIN_PGML/END_PGML section, then you will not get the warnings from failed image generation. You will instead get no image, and no reason as to why.

ENDDOCUMENT();

This doesn't have a question, so we aren't checking an answer.

Problem Techniques Index