Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

When using the built-in Debugger, the breakpoints and the current step will be framed looks like this:

enter image description here

My favorite part is, those frames are adaptive to the text when window width changing makes it auto-wrapped to a different shape:

enter image description here

So my question is:

How can I programmly get a frame with this kind of adaptive behavior, as good performance (executing fast) as possible?

share|improve this question

marked as duplicate by Silvia, ciao, m_goldberg, Kuba, bobthechemist Apr 10 '14 at 13:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Define "efficient." Do you mean shortest code, fastest code, or something else? – rcollyer Dec 1 '12 at 13:35
@rcollyer Sorry for the ambiguousness. I would like a code executing as fast as possible. I have edited my descriptions. Thanks for pointing out. – Silvia Dec 1 '12 at 13:41
Maybe relevant: – ziyuang Dec 2 '12 at 2:30
@ziyuang Thanks for your information. But what I concern is not how to wrap text but how to draw the irregular frame around given text. – Silvia Dec 2 '12 at 11:43
@Silvia I think you can draw the frame after you wrap the text by surrounding your wrapped text, if I understand your question correctly. Or are you asking whether there is a built-in function for this purpose since this effect appears in Mathematica? – ziyuang Dec 2 '12 at 12:51

This is hacky, but it works:

front = Pane[
 Style["this is a test and I hope it works so I need a very long string",    
 Background -> Yellow], ImageSize -> 150]

enter image description here

back = Pane[
  Style["this is a test and I hope it works so I need a very long string",
  Background -> Red], ImageSize -> 150, ImageMargins -> 2]

enter image description here

Overlay[{MinFilter[Rasterize@back, 2], front}, Alignment -> Center, 
 ImageMargins -> 1]

enter image description here

There are a few artefacts where the text hits the edge of the background in the back copy, and therefore the black color is picked up in the MinFilter. As shown below you can fix this by setting the text color and the background color in the back copy the same.

You could build this up into a function like this:

borderedText[text_String, width_Integer?Positive, {color1_, color2_}] :=
 With[{f = Pane[Style[text, Background -> color1], ImageSize -> width], 
   b = Pane[Style[text, color2, Background -> color2], ImageSize -> width, ImageMargins -> 2]},
  Overlay[{MinFilter[Rasterize@b, 2], f}, Alignment -> Center, ImageMargins -> 1]]

borderedText["This is a test that might or might not work", 150, 
{Lighter@Yellow, Red}]

enter image description here

share|improve this answer
Hi Verbeia. Thank you for your answer! It's really clever! Though I don't have access to a MMA, but it looks promising. Do you think it could be made to work in an "Input" style cell, so we can block-mark the code without affecting its executability? – Silvia Sep 23 '13 at 3:53

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