15

Note: this problem is no longer present in version 10.


Exporting the following as a PDF file

Graphics[{
  {Red, Rectangle[{0, 0}, {1, 1}]},
  {Green, Rectangle[{1, 0}, {2, 1}]}
}]

does not produce perfectly tiled rectangles, and some of the corners are cut too, as shown here:

rectangle with dog-ear, first example

and here:

rectangle with dog-ear, second example

Is this a bug?

I solve the problem by giving them some tiny edges:

Graphics[{
  EdgeForm[{Red, Thickness[Tiny]}],
  {Red, Rectangle[{0, 0}, {1, 1}]},
  EdgeForm[{Green, Thickness[Tiny]}],
  {Green, Rectangle[{1, 0}, {2, 1}]}
}]

The rectangles are slightly larger, and their sizes are slightly different; practically this isn't a problem. But is there a more elegant solution?

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
Taiki
  • 5,259
  • 26
  • 34

2 Answers2

8

Please let me compile answers from the comments so that this topic can be marked as answered.

  1. Yes, this is a bug.

  2. A better solution than having rectangles of slightly different sizes and slightly larger than desired is to export the figure as EPS first and then convert it to PDF later. The cut corners are still there though.

Taiki
  • 5,259
  • 26
  • 34
  • As an alternative answer, you could try the function rasterTrick in my answer here. The output of Export["file.pdf",graphics//rasterTrick] looks fine to me using version 9.0.1 on OS X (where graphics is the original example in your question) – Jens Jul 02 '14 at 22:39
5

Unfortunately some effects, such as transparency, are not supported in EPS, so that workaround is not always usable. I found a different workaround with different tradeoffs:

  • instead of Rectangle use Polygon
  • specify the colours using the VertexColors option of Polygon

The polygons will be correct size and will not overlap when exported to PDF. However, they will be broken up into triangles, and depending on the PDF viewer the seams may be subtly visible, especially if the polygons are transparent.

Examples:

t1 = Normal@
  Graphics[GraphicsComplex[
    Tuples[Range[5], 2], {Opacity[0.5], 
     Polygon[#, 
        VertexColors -> ConstantArray[RandomReal[1, 3], 4]] & /@ Join @@ Table[{1, 2, 5 + 2, 5 + 1} + i + j, {i, 0, 3}, {j, 0, 3*5, 5}]}]]


t2 = Normal@
  Graphics[GraphicsComplex[
    Tuples[Range[5], 
     2], {Opacity[0.5], {RGBColor @@ RandomReal[1, 3], Polygon[#]} & /@
       Join @@ Table[{1, 2, 5 + 2, 5 + 1} + i + j, {i, 0, 3}, {j, 0, 3*5, 5}]}]]

t1 will export as precisely sized rectangles while t2 will export as overlapping rectangles.

Zoomed in:

enter image description here

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263