1
$\begingroup$

It does not seem to be using Rasterize[Text[...], "RasterSize"] nor ImageDimensions[ImageCrop[Graphics[Text[...]]]] nor Rasterize[Text[...], "BoundingBox"] (and indeed seems to do the computation differently than png export, which leads me to suspect this might be a bug).

Consider

Rasterize[p1 = With[{nmax = 10},
   Graphics[
    Table[With[{v = StringJoin @@ Table["a", Mod[n, 2, 1]*10]}, 
      Inset[Text[v], 
       Offset[ImageDimensions[ImageCrop[Graphics[Text[v]]]]/2 + {3, 
          0}, {0, n/nmax}], {0, 0}]], {n, 1, nmax}]
    ]]]
Rasterize[p2 = With[{nmax = 10},
   Graphics[
    Table[With[{v = StringJoin @@ Table["a", Mod[n, 2, 1]*10]}, 
      Inset[Text[v], 
       Offset[Rasterize[Text[v], "RasterSize"]/2 + {3, 0}, {0, 
         n/nmax}], {0, 0}]], {n, 1, nmax}]
    ]]]
Rasterize[p3 = With[{nmax = 10},
   Graphics[
    Table[With[{v = StringJoin @@ Table["a", Mod[n, 2, 1]*10]}, 
      Inset[Text[v], 
       Offset[Rasterize[Text[v], "BoundingBox"][[1 ;; 2]]/2 + {3, 
          0}, {0, n/nmax}], {0, 0}]], {n, 1, nmax}]
    ]]]
Export[FileNameJoin[{NotebookDirectory[], "foop1.pdf"}], p1]
Export[FileNameJoin[{NotebookDirectory[], "foop2.pdf"}], p2]
Export[FileNameJoin[{NotebookDirectory[], "foop3.pdf"}], p3]

In Mathematica, only the first one is correctly aligned:

p1

p2 looks like this:

p2

And p3 is similar but not identical. However, when I export to pdf, foop1.pdf looks like this (screenshot):

foop1.pdf

and foop2.pdf and foop3.pdf look similar. What's going on?

$\endgroup$
2
$\begingroup$

I don't think you're using Inset properly. Look at the documentation:

?Inset

(*
Inset[obj] represents an object obj inset in a graphic. 
Inset[obj,pos] specifies that the inset should be placed at position pos in the graphic. 
Inset[obj,pos,opos] aligns the inset so that position opos in the object lies at position pos in the enclosing graphic. 
Inset[obj,pos,opos,size] specifies the size of the inset in the coordinate system of the enclosing graphic. 
Inset[obj,pos,opos,size,dirs] specifies that the axes of the inset should be oriented in directions dirs.  >>
*)

Notice that the second usage statement says that the inset is placed at position pos in the graphic. Yet you are using different values for pos for even and odd lines. Secondly, when using a Text object in an inset, using {0, 0} for the opos value means that the center of the Text object is placed at pos. You should instead use {-1, 0} for the opos value so that the left end of the Text object starts at pos. So, using the same pos value for each row, and {-1, 0} for the opos value would produce left-aligned output. For example:

nmax=10;
Graphics[
    Table[
        With[{v=StringJoin@@Table["a",Mod[n,2,1]*10]},
            Inset[Text[v], Offset[{0, 0}, {0, n/nmax}], {-1,0}]
        ],
        {n,1,nmax}
    ]
]

enter image description here

Addendum

If you must use {0, 0} for $\operatorname{opos}$, then to get the raster size right you should include an explicit FontSize setting:

Graphics[
    Table[
        With[{v = StringJoin @@ Table["a",Mod[n,2,1]*10]},
            Inset[
                Text[Style[v,16]],
                Offset[
                    Rasterize[Text[Style[v,16]],"RasterSize"]/2+{3,0},
                    {0,n/nmax}
                ],
                {0,0}
            ]
        ],
        {n,1,nmax}
    ]
]

enter image description here

$\endgroup$
  • $\begingroup$ I am mimicking how Callout and PlotLabels insert text in Graphics (though they seem to use Text rather than Inset, but the doc says that they behave the same when given three arguments). The problem is fixed if I set the x coordinates the same and align the text to the left edge. But this is not what PlotLabels -> Automatic does! What I am doing here is saying "place the center of the text at a location which is width-of-text/2 from where I want the left edge to be. The figure p1 displays correctly in Mathematica. And in png export. But not pdf export. Why? $\endgroup$ – Jason Gross Jan 11 '18 at 13:07
  • $\begingroup$ Said another way, the differences between even and odd lines are supposed to cancel out with aligning based on the center of the object. And they do, in p1, in Mathematica and in png export. My question here is what pdf export is doing that makes these not cancel out. $\endgroup$ – Jason Gross Jan 11 '18 at 13:09
  • $\begingroup$ Ah, thank you! Specifying FontSize fixes things. I guess my next question shall be, how does pdf export compute FontSize... $\endgroup$ – Jason Gross Jan 12 '18 at 9:31
  • $\begingroup$ How does rasterize pick the default fontsize, and why is it different from what gets rendered, though? $\endgroup$ – Jason Gross Jan 12 '18 at 9:38
  • $\begingroup$ Uh, wait, this works perfectly fine at FontSizes 9 and 16, but does not work at FontSize 10 $\endgroup$ – Jason Gross Jan 12 '18 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.