2
$\begingroup$

This just drives me crazy: Suppose I have a label label that I want to put on top of a Graphics (or any other Mathematica object like a Grid).

label= Style[ "This is just some text for a label that is not too wide.", "Label", 18];
size = {600, 200};
plot = Labeled[Graphics[{}, ImageSize -> size, Background -> LightBlue], label, {{Top, Center}}]

This is what Mathematica 12.0 on macos shows to me in the notebook:

enter image description here

This is what I get after export to PDF:

First@ImportString[ExportString[plot, "PDF"], "PDF"]

enter image description here

Not only doesn't this keep the size of the Labeled region, what really annoys me is the linebreak in the label.

Does anybody know how to prevent that?

PS.: I know that I could use the PlotLabel option of Graphics. The Graphics object here is really only a place holder.

$\endgroup$
  • $\begingroup$ in V 12.1 I do not see this. Here is screen shot !Mathematica graphics $\endgroup$ – Nasser Jun 4 at 12:40
  • $\begingroup$ Thank you, @Nasser, that gives me hope for the future! =) $\endgroup$ – Henrik Schumacher Jun 4 at 12:42
  • $\begingroup$ I'm not seeing the problem on V 12.0 on Win 10. $\endgroup$ – Chris Degnen Jun 4 at 12:44
  • $\begingroup$ Thank you Chris. That underlines my suspicion that it is related to the way macos handles PDF. That changed frequently and caused many problems in the past. $\endgroup$ – Henrik Schumacher Jun 4 at 12:45
1
$\begingroup$

Okay, this appears to be a version and os-specific issue. So in my desperate attempts I recalled the old $\LaTeX$ rule to make a box around everything. What comes closest to a box is an invisible frame. And indeed Framed allows for an ImageSize option, leading to this workaround:

plot = Labeled[
   Graphics[{}, ImageSize -> size, Background -> LightBlue],
   Framed[label, ImageSize -> {size[[1]], Automatic}, 
    Alignment -> Center, FrameStyle -> None],
   {{Top, Center}}
   ];
First@ImportString[ExportString[plot, "PDF"], "PDF"]

enter image description here

| improve this answer | |
$\endgroup$

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.