4
$\begingroup$

I want to create a legend for my plots (a mixture of RegionPlots and ContourPlots). The legend should show the meanins of colors as boxes with text in them and the meaning of contours as framed number with text besides it. The following code creates an ideal legend in a notebook, but as soon as I export it to .eps (for inclusion in LaTeX), it screws up and starts breaking lines in the last part (the Grid inside the Grid).

How can I prevent Mathematica from changing the appearance of the image upon export?

BoxSize = {6, 2};
boxImSize = 170;
legend = Grid[{{Graphics[{Thick, Lighter[Green, 0.3], 
  Rectangle[{0, 0}, BoxSize], Black, 
  Text[Style["Allowed by all", 14, Bold], Center] }, 
  ImageSize -> boxImSize], 
  Graphics[{Thick, Lighter[Blue, 0.5], Rectangle[{0, 0}, BoxSize], 
    Black, Text[
    Style["Excluded by \!\(\*SubscriptBox[\(m\), \(H\)]\)", 14, 
    Bold], Center] }, ImageSize -> boxImSize], 
  Graphics[{Thick, Darker[Orange, 0.3], Rectangle[{0, 0}, BoxSize], 
    Black, Text[
    Style["Excluded by\n \[Mu] \[RightArrow] e \[Gamma]", 14, 
    Bold], Center] }, ImageSize -> boxImSize], 
Graphics[{Thick, Red, Rectangle[{0, 0}, BoxSize], Black, 
    Text[Style[
    "Excluded by\n \!\(\*SubscriptBox[\(m\), \(H\)]\) and \[Mu] \
\[Rule] e \[Gamma]", 14, Bold], Center] }, ImageSize -> boxImSize], 
Graphics[{Thick, GrayLevel[0.8], Rectangle[{0, 0}, BoxSize], 
  Black, Text[
   Style["Excluded by\n SUSY constraints  ", 14, Bold], 
   Center] }, ImageSize -> boxImSize],
Grid[{{Invisible[""], 
   Text[Style[Framed[5.7], 12, Bold, Blue], Background -> White], 
   Text[Style[
     "\!\(\*SuperscriptBox[\(10\), \(13\)]\)\[Times] B(\[Mu] \
   \[Rule] e \[Gamma])", 12, Bold, Blue]], 
   Invisible[""]}, {Invisible[""], 
   Text[Style[Framed[124], 12, Bold, Black], 
    Background -> White], 
   Text[Style["\!\(\*SubscriptBox[\(m\), \(H\)]\) in GeV", 12, 
     Bold, Black]], Invisible[""]}}, Spacings -> 0.5, 
 Alignment -> {Left, Center}]
}}, Background -> None, Spacings -> 0, Alignment -> {Left, Top}, 
Frame -> True]
$\endgroup$
4
  • $\begingroup$ This will mess up the purpose with .eps, vector based graphics, but still it can be mentioned that a quick fix is to put Rasterize@ in front of Grid. $\endgroup$
    – C. E.
    Aug 24, 2013 at 16:09
  • 1
    $\begingroup$ You can eliminate the line-breaks in the exported .eps by using GraphicsGrid instead of Grid, but you will then need to tweak the spacings. $\endgroup$
    – DavidC
    Aug 24, 2013 at 16:43
  • $\begingroup$ Export to .SVG seems to work well. Here is how to include .SVG in LaTeX. $\endgroup$ Aug 24, 2013 at 16:58
  • $\begingroup$ Thank you for your helpful comments. I now use GrahpicsGrid instead of Grid and have to live with the larger spacings this implies. $\endgroup$
    – Neuneck
    Aug 27, 2013 at 7:52

2 Answers 2

4
$\begingroup$

This isn't really an answer but rather a comment that is too long to fit in a comment text box .

Vitaly Kaurov suggested a very viable solution to your problem. If you follow his advice, there are still some aspects of your code you might want to tidy up.

If you are going to .svg, but not if you remain committed to exporting in .eps, you can

  1. Eliminate all uses of Graphics. Everything you want to print is text.

  2. Use BaseStyle instead of formatting each instance of text that appears.

  3. Use Items to be able to set the Background colors of grid cells.

  4. Use a thick white frame for the overall Grid with FrameStyle and Frame. Dividers-> All will ensure that the cells themselves are separated by white space. Simply use Framed[] to place a thin black line around the whole outer grid. (We already used the grid's frame to insert white space.)

  5. Remove instances of Invisible[""] throughout.

You'd probably want to tweak the spacings or allignments to suit your needs.


Here's a rewrite of your code.

Framed@Grid[{{
    Item["Allowed by all", Background -> Lighter[Green, 0.3]],
    Item["Excluded by \!\(\*SubscriptBox[\(m\), \(H\)]\)",Background -> Lighter[Blue, 0.5]],
    Item["Excluded by\n \[Mu] \[RightArrow] e \[Gamma]", Background -> Darker[Orange, 0.3]],
    Item["Excluded by\n \[Mu] \[RightArrow] e \[Gamma]", Background -> Red],
    Item["Excluded by\n SUSY constraints  ", Background -> GrayLevel[0.8]],
    Item[Grid[{{
        Style[Framed[5.7, FrameMargins -> 3], 12, Bold, Blue],
        Style[
         "\!\(\*SuperscriptBox[\(10\), \(13\)]\)\[Times] B(\[Mu]    \
\[Rule] e \[Gamma])", 12, Bold, Blue]},
       {Style[Framed[124, FrameMargins -> 3], 12, Bold],
        Style["\!\(\*SubscriptBox[\(m\), \(H\)]\) in GeV", 12, Bold]}}, 
        Spacings -> 0, Alignment -> {Left, Center}]]}},
  Dividers -> All,
  Spacings -> 7,
  FrameStyle -> {Thickness[8], White}, Frame -> True,
  BaseStyle -> {14, Bold, FontFamily -> Times},
  Alignment -> {Left, Center}]
$\endgroup$
1
  • $\begingroup$ Thank you for the proposal. Unfortunately, it did not eleminiate the linebreaks for me (Mathematica901 @ SUSE). Still +1 for the great suggestions! $\endgroup$
    – Neuneck
    Aug 27, 2013 at 7:54
4
$\begingroup$

Add ItemSize -> Full in the options of Grid. I tested it on your code and it works, meaning that the line is no longer broken in the exported pdf.

$\endgroup$
2
  • $\begingroup$ Even after exporting ? $\endgroup$
    – Sektor
    Nov 6, 2014 at 21:17
  • $\begingroup$ Even afte exporting. The cell ouput in mathematica has always been good. $\endgroup$
    – Chromatic
    Nov 7, 2014 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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