Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to place a curly brace within a plot such that the top/bottom of the curly brace line up with two horizontal lines in the plot:

enter image description here

I have not been able to find a way to make the curly brace have the right size without setting the size explicitly by hand. Insetting a Text object doesn't work as the font size is used to scale text, and guessing the right font size is a complete guessing game. I settled upon using "Pane" with the "ResizeToFit" ImageSizeAction set, however I am not sure how to then specify the Inset size or the ImageSize for pane to get the desired results.

Here is the code I am using to generate this plot. ImageSize being set to "5" is just a guess to get approximately the right size, but ideally I want the top/bottom of the curly brace to exactly line up with the two horizontal dashed lines:

f = 1/x;
\[Epsilon]pl = 
  Plot[{f}, {x, 0, 1}, PlotStyle -> Black, 
   PlotStyle -> AbsoluteThickness[1]];

\[Epsilon]1pl = 
  Plot[{f}, {x, 0.2, 0.4}, 
   PlotStyle -> Directive[Red, AbsoluteThickness[1.5]]];
\[Epsilon]1dashed = 
  ListPlot[{{{0.1, f /. x -> 0.27}, {0.27, f /. x -> 0.27}}, {{0.1, 
      f /. x -> 0.3}, {0.3, f /. x -> 0.3}}}, 
   PlotStyle -> Directive[Red, Dashed, AbsoluteThickness[1]], 
   Joined -> True];

Show[\[Epsilon]pl, \[Epsilon]1pl, \[Epsilon]1dashed, 
 PlotRange -> {{0, 1}, {0, 6}}, Axes -> False, Frame -> True, 
 ImageSize -> 600, 
 FrameStyle -> Directive[AbsoluteThickness[1], FontSize -> 24], 
 FrameTicks -> False, FrameLabel -> {"\!\(\*
StyleBox[\"r\",\nFontSlant->\"Italic\"]\)", "\!\(\*
StyleBox[\"\[Epsilon]\",\nFontSlant->\"Italic\"]\)"}, 
 Epilog -> 
  Inset[Pane["{", ImageSize -> 5, 
    ImageSizeAction -> 
     "ResizeToFit"], {0.095, ((f /. x -> 0.27) + (f /. x -> 0.3))/
    2}, {0, 0}, {Automatic, (f /. x -> 0.27) - (f /. x -> 0.3)}]]

Additionally, I would like to be able to scale the curly brace such that the original aspect ratio isn't maintained (i.e. the width of the curly brace will be a fixed value, but the height can vary). What is the best way to go about doing this?

share|improve this question
add comment

4 Answers

up vote 15 down vote accepted

"Vectorizing" the font:

curve = First[First[ ImportString[ExportString[
        Style["{",FontFamily ->"Times",FontSize -> 72],  "PDF"], "TextMode" -> "Outlines"]]];
cg = Graphics[curve]

and then your code, replacing the Inset[] clause by

Inset[Pane[cg, ImageSizeAction ->  "ResizeToFit"], ....

Result (scales OK when resizing the Plot):

Mathematica graphics

Edit

Full code follows, just in case you decide to edit your code:

curve = First[First[
    ImportString[
     ExportString[Style["{", FontFamily -> "Times", FontSize -> 72], 
      "PDF"], "TextMode" -> "Outlines"]]];
cg = Graphics[curve];
f = 1/x;
\[Epsilon]pl = 
  Plot[{f}, {x, 0, 1}, PlotStyle -> Black, 
   PlotStyle -> AbsoluteThickness[1]];

\[Epsilon]1pl = 
  Plot[{f}, {x, 0.2, 0.4}, 
   PlotStyle -> Directive[Red, AbsoluteThickness[1.5]]];
\[Epsilon]1dashed = 
  ListPlot[{{{0.1, f /. x -> 0.27}, {0.27, f /. x -> 0.27}}, {{0.1, 
      f /. x -> 0.3}, {0.3, f /. x -> 0.3}}}, 
   PlotStyle -> Directive[Red, Dashed, AbsoluteThickness[1]], 
   Joined -> True];

Show[\[Epsilon]pl, \[Epsilon]1pl, \[Epsilon]1dashed, 
 PlotRange -> {{0, 1}, {0, 6}}, Axes -> False, Frame -> True, 
 ImageSize -> 600, 
 FrameStyle -> Directive[AbsoluteThickness[1], FontSize -> 24], 
 FrameTicks -> False, FrameLabel -> {"\!\(\*
   StyleBox[\"r\",\nFontSlant->\"Italic\"]\)", "\!\(\*
   StyleBox[\"\[Epsilon]\",\nFontSlant->\"Italic\"]\)"}, 
 Epilog -> 
  Inset[Pane[cg, 
    ImageSizeAction -> 
     "ResizeToFit"], {0.095, ((f /. x -> 0.27) + (f /. x -> 0.3))/
     2}, {0, 0}, {Automatic, (f /. x -> 0.27) - (f /. x -> 0.3)}]]
share|improve this answer
    
Excellent, exactly what I was looking for. –  Guillochon Apr 17 '12 at 21:47
    
Very nice. I figured there might be an easy way to do this. +1 –  Andy Ross Apr 17 '12 at 22:03
    
@Mr.Wizard FilledCurve[] and JoinedCurve[] are new in 8 ... and the ImportString thing is returning a FilledCurve[ blah blah] –  belisarius Apr 17 '12 at 22:37
1  
as they say: "duh" -- I've really gotten lazy; sorry. –  Mr.Wizard Apr 17 '12 at 22:52
2  
+1 for "Vectorizing the font", just the thing I was looking for. –  István Zachar Apr 18 '12 at 0:17
add comment

To make such labeling more user-friendly, I wrote this function (edited to add rotation):

stretchText[char_, pos_, scale_, angle_: 0] := 
 Module[{g, coords, xMin, xMax, yMin, yMax}, 
  g = First@First@ImportString[ExportString[char, "PDF"],
      "TextOutlines" -> True];
  coords = 
   Apply[Join, 
    Cases[g, FilledCurve[___, p_] :> Flatten[p, 1], Infinity]];
  {{xMin, xMax}, {yMin, yMax}} = 
   Map[{Min[#], Max[#]} &[#] &, Transpose[coords]];
  Rotate[
   Inset[
    Graphics[g,
     PlotRange -> {{xMin, xMax}, {yMin, yMax}},
     If[ListQ[scale],
      AspectRatio -> Full,
      AspectRatio -> Automatic
      ]
     ],
    pos,
    {xMin, yMin},
    scale],
   angle]
  ]

Here are some features:

First, you don't have to use a Pane or even an Epilog to use it (the output of stretchText is already an Inset and can be put into a Graphics). Secondly, you can position whatever string you like very precisely by specifying its lower left corner as the second argument pos, and then its scale in the horizontal and vertical direction as the third argument scale. The scale is in the coordinates of the surrounding graphic by default. So if you want the text to be exactly 0.2 wide and 0.7 high in the coordinates of your actual plot, that's exactly what you enter as scale. No need for trial-and-error adjustments.

The scale can be entered as a single number (in which case it sets the text width and scales the height proportionally), or as a pair of numbers for independent scaling of width and height. The latter is really what makes stretchText different from normal text. The last (optional) argument in stretchText is the rotation angle away from the horizontal.

Here is an example that just combines a hyperbola plot with the dashed lines and the stretched { by using a simple Show statement. To make the stretching clearer, I made a movie of it. What really counts is inside the Table:

t = With[{xMax = 3, wX = .2},
   Table[
    Show[Plot[1/x, {x, 0, 5}],
     Graphics[
      stretchText["{", {xMin - wX, 1/xMax}, {wX, 1/xMin - 1/xMax}]],
     Graphics[{Red, Dashed, 
       Map[Line[{{0, 1/#}, {#, 1/#}}] &, {xMin, xMax}]}],
     PlotRange -> {0, 1.5}, Frame -> True, Axes -> False],
    {xMin, .7, 2.5, .2}]
   ];

Stretching character

As this example shows, the positioning is pretty straightforward because stretchText takes care of rescaling the outlined characters (it could be a whole string, too) to the given parameters.

The essential ingredient in stretchText is the PDF export/import trick to produce FilledCurves, so it requires Mathematica 8. The ability to combine this annotation with Show means that it's easy to manipulate the output with Mathematica's graphics editor too, if needed.

Edit: note on outlining text

The reason why I convert text to outlines here is that text glyphs in a string can't be scaled by independent factors in the horizontal and vertical directions. You can only scale text proportionally, and that would make it impossible to get a stretched brace.

I decided to use "TextOutlines" -> True instead of "TextMode"->"Outlines" to get a FilledCurve representation of the glyphs. That's simply because the documentation gives it a more official status ("TextMode"->"Outlines" is used only in the examples for FilledCurve and JoinedCurve). But both of these options are in principle unnecessary because the PDF options for Import already list "TextOutlines" -> True as the default. Leaving the explicit option in is just a way to be safer in this application.

The point lists in the FilledCurve for the outlined glyphs are also used here to determine a reasonably close-cropped bounding box for the text. This is only approximate because I make no distinction between control and other points, but it is very simple and seems to work well in practice.

share|improve this answer
    
Very nice improvement to belisarius' solution. –  Guillochon Apr 18 '12 at 2:29
    
I've actually been doing it this way for a long time, see my web page. Too bad that "TextMode"->"Outline" only got introduced (fully) in version 8 - before that, one had to resort to more complicated work-arounds, e.g., using ghostscript; see here. That would still be a way to crearte similar functionality in older Mathematica versions... –  Jens Apr 18 '12 at 2:40
add comment

You can use my brace function.

p2c[p_]:=p[[1]]+I p[[2]];
c2p[p_]:={Re[p],Im[p]};
brace[pa_,pb_,a_:1/10,t_:1/3]:=Module[{\[Theta],\[Rho]},
\[Theta]=Arg[p2c[pb-pa]];
\[Rho]=Norm[p2c[pb-pa]];
{Circle[pa+a \[Rho]{Cos[\[Theta]],Sin[\[Theta]]},a \[Rho],{\[Pi]+\[Theta]+t \[Pi]/2,(3\[Pi])/2+\[Theta]}],
  Line[{pa+a \[Rho]{Cos[\[Theta]],Sin[\[Theta]]}+a \[Rho]{Sin[\[Theta]],-Cos[\[Theta]]},(pa+pb)/2-a \[Rho]{Cos[\[Theta]],Sin[\[Theta]]}+a \[Rho]{Sin[\[Theta]],-Cos[\[Theta]]}}],
  Circle[(pa+pb)/2-a \[Rho]{Cos[\[Theta]],Sin[\[Theta]]}+2a \[Rho]{Sin[\[Theta]],-Cos[\[Theta]]},a \[Rho],{\[Theta],\[Pi]/2+\[Theta]}],
  Circle[(pa+pb)/2+a \[Rho]{Cos[\[Theta]],Sin[\[Theta]]}+2a \[Rho]{Sin[\[Theta]],-Cos[\[Theta]]},a \[Rho],{\[Pi]/2+\[Theta],\[Pi]+\[Theta]}],
  Line[{(pa+pb)/2+a \[Rho]{Cos[\[Theta]],Sin[\[Theta]]}+a \[Rho]{Sin[\[Theta]],-Cos[\[Theta]]},pb-a \[Rho]{Cos[\[Theta]],Sin[\[Theta]]}+a \[Rho]{Sin[\[Theta]],-Cos[\[Theta]]}}],
  Circle[pb-a \[Rho]{Cos[\[Theta]],Sin[\[Theta]]},a \[Rho],{(3\[Pi])/2+\[Theta],2\[Pi]+\[Theta]-t \[Pi]/2}]}
]

Here, a is in [0,1/4] and t is in [0,1], which you can easily figure out how they work. Below, you can find and example.

With[{pa={3,1},pb={1,5},a=1/20,t=1/3}, 
 Graphics[{Point[{pa,pb}],brace[pa,pb,a,t], 
   Text[EuclideanDistance[pa,pb],(pa+pb)/2+1/2Reverse[(pa-pb)/2]*{-1,1}]}, 
   ImageSize->200,Frame->True,PlotRange->{{0,6},{0,6}}
 ]
]

Showing distance with a curly brace

share|improve this answer
    
For the special case of a brace, I also posted an implementation here. It differs from this in that the label is passed as an argument to the function I call braceLabel. –  Jens Aug 7 '13 at 21:55
add comment

One possibility is to create your own "{" character using BSplineCurve.

Graphics[{Thick, BSplineCurve[{{0.3, 2.9}, {-0.6, 2.45}, {-0.95, 1.8}, 
      {-0.2, 1.05}, {0.25,0.45}, {0., 0.}, {-0.8, -0.15}, {0., -0.35}, 
      {0.25, -0.75}, {-0.15, -1.25}, {-0.85,-1.9}, {-0.35, -2.55}, 
      {0.35, -3.}}]}
 ]

You can place the graphic directly in place of the "{" character and it will scale nicely. The only issue is getting the character to look as high quality as the built in ones. I made my poor attempt using the "Bezier" setting from Heike's answer to this question.

Note: You will want to remove the ImageSize option from your Inset.

Here is an example using a larger brace than the one in your example as proof of concept that it scales up nicely.

enter image description here

Edit:

One can also use Rasterize on the character and with a little cropping achieve what you want as well.

ImageCrop@Image[Rasterize["{", ImageSize -> 500]]
share|improve this answer
    
I'd like to avoid rasterization, if possible. Perhaps if there's a way to vectorize font characters, that would be the ideal solution, as then the graphics can be scaled in a straight-forward way. There might be font issues when the graphic is exported if a font character is used. –  Guillochon Apr 17 '12 at 21:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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