Bug introduced in 8.0 or earlier and persisting through 13.2.0 or later
This has irked me for a while as I really feel its a significant shortfall in Mathematica's abilities but maybe I am missing something.
I would like to export simple graphics with shading in a vector graphics format, preferably PDF. This should certainly be possible with the pdf format as I quote from wikipedia (en.wikipedia.org/wiki/Portable_Document_Format#Vector_graphics):
Beginning with PDF 1.3 there is also a shading pattern, which draws continuously varying colors.
Take for example the following simple graphic:
Created with the code
Graphics[{CapForm["Round"], Thickness[0.2],
Line[{{0, 0}, {1, 0}}, VertexColors -> {Red, Green}]}
]
When exported as a pdf it becomes:
I think I am correct in stating that Mathematica supports a more recent version of pdf than 1.3 so where is the problem? Is there a workaround? My graphics are still simple 2d lines but such that vector formats are far preferable to rasters.
May thanks for any help offered, I am surprised that I have been unable to find a solution.
EDIT: Two other exporting options which have occurred to me are:
- To right click and save as PDF, this produces the same green worm as exporting to pdf.
- To right click and print, then save as pdf on the system dialog, this manages to maintain the shading (hurray!) but removes the round capform and leaves the graphic on an entire A4 background...
Thus neither provide satisfactory solutions.
EDIT 2: Not sure if this should be an edit or posted as a reply but it's a bit long for a comment.
Taking on board the comments of @Szabolcs and @Jens I've tried a few more things out. Specifically I have compared exporting the line as shown above, and a line created using the Polygon function instead of Line (similar therefore to @Szabolcs triangle). I have exported both as PDF, EPS (then converted to PDF via terminal) and SVG, in all instances the option "AllowRasterization" -> False is used. What I have found is:
- Export the Line graphic results in an olive worm, regardless of format. (A Mathematica bug?)
- Exporting a Polygon graphic as SVG or EPS (then converting to PDF) both appear to work.
- Exporting a Polygon graphic as PDF produces incorrect results which vary depending on the exact polygon, for example the red-green line (without the round ends as one cannot do this with Polygon) becomes:
whilst @Szabolcs' triangle becomes:
Current Conclusions: My current conclusions therefore are that exporting to PDF is incompletely implemented (and should be better).
And that exporting Line objects is flawed, there is no way to export a Line object with VertexColors as a vector graphic.
So a very limited workaround for the most basic shapes is to recreate them as polygons, export to EPS, then convert to PDF. This however does not allow the use of any curved elements, still unsatisfactory for my purposes. I will consider reporting this as a bug (unless anyone advises me otherwise!).
Thanks for your help so far guys.
Graphics[Polygon[{{0, 0}, {1, 0}, {1, 1}}, VertexColors -> {Red, Green, Blue}]]
. $\endgroup$Export["~/Desktop/test.pdf", Graphics[Polygon[{{0, 0}, {1, 0}, {1, 1}}, VertexColors -> {Red, Green, Blue}]]]
. It worked fine. Did you use the same? The OP's example indeed does not work, even though it's quite similar to thePolygon
example above. That's why I thought that this might be an actual bug, and not just a missing feature. $\endgroup$