-
-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement a better method for generating svg images with TikZImage.pm. #557
Conversation
Just looking over the example .pg files for now. I guess you can put things like How do you put actual dollar, percent, and at signs in the tikz code? Like if they were part of the text labeling an axis or something? Is it possible without using Can authors leave comments in the tikz code? Like Let's add the |
Yeah, the To add the dollar symbol you can do Using I am not sure that the |
Yeah, so I looked into attempting to do the |
Okay. So it wasn't that hard after all. I have to update the documentation, but I will commit it soon. |
Note that using the |
Here is an example using the |
Overall, this looks good (with pun intended since svg is cleaner). I wonder if instead of calling:
inside a problem, that we have setting for this instead. It seems like this should be a global or course-level setting rather than a problem setting. We could have a
inside And then of course the conversion would need to handled somewhere instead of inside the problem itself. |
This is definitely not a case where it makes sense to be a course level or system level setting. This is a problem authoring decision. The point is that an author must decide if the images created by their tikz code work well with SVG or not. I am fine with setting the default to SVG, and then having the author override that to PNG if their images don't work well. Generally SVG is the right way to go if that works. I am not fond of having to set the override extension in the way that is done here, but there are limitations with the module that make this necessary. |
Attached are updated examples for the latest changes. |
Here is a screenshot from a modified version of the testing problem |
I am not entirely certain of what you are doing. I made a guess as follows, but it doesn't produce the output you show. Here is my example attempting to reproduce your image. DOCUMENT();
loadMacros("PGstandard.pl", "PGML.pl", "PGtikz.pl");
TEXT(beginproblem());
$a = random(-10, 9);
$b = random(-10, 9);
$graph_image = createTikZImage();
$graph_image->tikzLibraries("arrows.meta");
$graph_image->tikzOptions("x=0.6cm,y=0.6cm");
$graph_image->BEGIN_TIKZ
\tikzset{>={Stealth[scale=1.8]}}
\filldraw[draw=blue,fill=white,rounded corners=14pt,thick,use as bounding box] (-11,-11) rectangle (11,11);
\begin{scope}
\clip[rounded corners=14pt] (-11,-11) rectangle (11,11);
\draw[line width=0.2pt,color=gray,step=1] (-11,-11) grid (11,11);
\end{scope}
\huge
\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\)};
\filldraw[red] ($a,$b) circle[radius=5pt];
% this is a comment
\node at (3,-3){\(~~\$50,000\)};
END_TIKZ
BEGIN_PGML
[@ image(insertGraph($graph_image), width => 200, height => 200) @]*
[@ $graph_image->ext('png'); image(insertGraph($graph_image), width => 200, height => 200) @]*
[@ $graph_image->ext('svg'); image(insertGraph($graph_image), width => 100, height => 100) @]*
END_PGML
ENDDOCUMENT(); |
If I take That's using the 300 pixel width written into the problem, and apparently I am seeing some default height. The image of course is a zoomed in section of the intended graph. If I change the
|
If there is a discrepancy between what I get and what you get, it could be some issue about the version of |
Argh! I tested this on my server which has Ubuntu 18.04 and the versions of texlive/dvisvgm installed that you have. I get the same crap. So apparently newer versions of TeX and dvisvgm (and probably ghostscript which dvisvgm uses) are needed. Looking closely I see that the pgfsys-dvisvgm.def file in the older texlive distribution is quite different than the one in the newer tex distribution on Ubuntu 20.04. On Ubuntu 18.04 the dvisvgm binary is part of the texlive-binaries package, but on Ubuntu 20.04 it is in the separate dvisvgm package. So apparently things have changed quite a bit with dvisvgm. |
I added a system configuration option as @pstaabp had suggested earlier to set the default image extension used by the PGtikz.pl macro. This is not ideal, but unfortunately needed as the versions of latex and dvisvgm on Ubuntu 18.04 are not good enough to generate 'svg' images. See openwebwork/webwork2#1320 for the settings. |
I never really tried making tikz images before this change. Would it make sense for the default to still be svg, but the configuration option is between the I'm not sure if the same or similar issues happen with the
|
Something a bit crazy is going on. I'm looking at the If I add the line
then I do get it as a SVG file. I was looking if there is any config that I set, but it doesn't appear to. I'm guessing others are not seeing this. |
I just looked at openwebwork/webwork2#1320 and I have not pulled that in, so that's not causing this. |
@Alex-Jordan: The convert method always really generates png images, even for the svg extension. If you select the svg extension it just puts the png image into an svg container. @pstaabp: Even without openwebwork/webwork2#1320 the default is now 'png'. I haven't updated the example file, so you will get 'png' from both. You need to add |
@drgrice1 Thanks. I was walking through the code, trying to figure out what's going on. I got the impression that line 38 in |
OK, shooting through all of the options I know of, what about Using |
@pstaabp: Line 38 of TikZImage.pm sets the underlying default of TikZImage package. However, that is overridden by line 99 of PGtikz.pl. That sets the extension to the tikzImageExtension if that is defined, and falls back to 'png' if not. @Alex-Jordan: I would prefer to use dvisvgm as the current version does a better job in general. I guess I will switch this pull request to using pdf2svg for now though. |
I completely agree. I am wondering if the configuration option that you just introduced could control whether this process uses |
I was thinking the same thing, and will try to implement that. |
experiencing running on OSX. This is a bit of a stab in the dark, but may solve the problem. Please test.
@pstaabp: Could you test with the latest commit, and see if that resolves the permissions issue that you are experiencing? |
@drgrice1 This fixes that permissions problem now. |
@pstaabp: So the images are properly generated on your local test server then? |
@drgrice1 before this patch, it wouldn't generate the svg image using |
Yeah, that is what I meant. I forgot that it was working with dvisvgm. Good deal. |
that are inserted via insertGraph in PGbasicmacros.pl's image method.
Thanks. I got to thinking what other package option clashes might arise? Since the packages and their options are a hash reference, we cannot require the packages to load in the order that the problem author writes them. I thought to add an alternative where order would be enforced with an array reference. An extreme example:
but I think that is not easy to merge with the Probably it is incredibly rare that packages you might use with a tikz picture have the opportunity for a clash, so this observation is not all that important. |
You are right. This may not be that important, but could be an issue. How about just an array or arrays, where the inner arrays may have 1 or 2 elements. The first being the package name, and the second (if provided) the package options? So it would be used like $image->texPackages([
[package1, options1],
[package2]
]]) Here \usepackage[options1]{package1}
\usepackage{package2} Of course the xcolor package would be filtered out. I don't see the need to separate the options into yet another embedded hash. Furthermore, latex package options do not always fit into the format that are assumed with that. |
package. This ensures that packages are loaded in the order requested which may be neccessary for latex. An example of its usage is ```perl $image = createTikZImage(); $image->texPackages([ "pgfplots", ["hf-tikz", "customcolors"], ["xcolor", "cmyk,table"] ]); ``` Note that the `xcolor` package breaks the order rule. It is always loaded, and is always loaded first. Passing it is only needed if you want to change the options for the package.
The last commit changes from hash to an array for the texPackages option of the TikZImage package. $image = createTikZImage();
$image->texPackages([
"pgfplots",
["hf-tikz", "customcolors"],
["xcolor", "cmyk,table"]
]); Note that the |
Here is the updated example that uses the pgfplots package. It doesn't load any other packages though, so it is not a good example of how to use the texPackages option. |
I looked at the most recent commit that moves to an array reference for packages, and it looks good. Still approve for merge. |
Implement a better method for generating svg images with TikZImage.pm.
EDIT: Figured it out, my Now that this has been merged into PG-2.16, I have tested it out, using the .pg file in
|
@somiaj: Yeah, if you are testing things with the release candidate branch, make sure that your configuration files are all updated. Thanks for testing though. We need as many people testing things as we can get. |
@drgrice1 What is the preferred way to change convert options? I have been wanting to use Tikz to create some graphs, but the .png quality was always low. Adding I also assume that using Anyways, thanks for the support, and all my tests appear to be working fine, these are just things I noticed. |
There are two open pull requests to handle convert options. One for |
Wait for #566. That adds the capability you are looking for. @Alex-Jordan just beat me to this post. |
You can use |
@drgrice1 I was working with creating some images with this package and noticed the following behavior when working with .svg images (I am using
It appears that the old image is used if it is attached to an assignment (something is being cached?) thus making it so changes don't appear in the assignment. I tried deleting the image from Edit: In testing this I found that it only seemed to happen with .svg files. I also decided to test |
@somiaj: Check things carefully to see that you are using the files you think you are using. That is, make sure you are using the actual PG file for the homework problem that you think you are. There was a bug in the PG problem editor that actually changed the file the instructor was using in homework sets when the instructor edited a PG file. That was fixed in a recent pull request, but since you are mentioning restarting the server it is possible your server was on an earlier git commit before it was last restarted. |
@drgrice1: That wasn't the issue, other changes in the file would be visible, it was only the image that would not change. I tested this again this morning and got the same behavior again. I am able to see any changes to the problem except the image. But I think I was able to track down the issue, and it is due to my browser. Firefox was just caching the image, and since the random string that is generated is the same for the problem, the image name was the same and my browser was using the cached version. A hard reload got the correct image showing. Firefox doesn't seem to cache the image right away when I was actively testing it, but if I wait a bit and came back, it would use its cached version. Thanks again for your support. |
The current implementation generates a pdf using pdflatex, and then converts the pdf to svg using imagemagick's convert utility.
This changes it so that it uses latex to generate a dvi with the pgfsysdriver redefined to pgfsys-dvisvgm.def, and then converts the dvi to svg using dvisvgm. This is the best way that I have found of creating svg images from tikz/tex.
This does require another external program (dvisvgm). That is added in a corresponding pull request to webwork2.
I have attached a couple of examples to compare the png image generation to the svg image generation.
examples.zip
To test this, make sure to set the dvisvgm external command in site.conf. See openwebwork/webwork2#1308.