How to make text fit in a MetaFun box with specific width ?

For now I have

  % Useful values
  numeric w, h, o, b ;
  w := 4.5cm ; h := OverlayHeight ; o := BodyFontSize ;
  b := 1pt ; % border size

  drawoptions (withpen pencircle scaled b withcolor blue) ;
  draw (0,h)--(0,0)--(w,0)--(w,h)--(0,h) ;

  setbounds currentpicture to OverlayBox ;




    This is a looooooooong text that I need to fit in the box.


  • You are missing align=normal in the definition of \defineframedtext. Without a value of align, \framed acts as a \hbox, with a value of align it acts like a \vbox, which is what you want – Aditya Apr 20 at 5:20
  • Do you want the box to grow vertically so that all text is always visible, or are you okay if the text is cut off when too long? Also, are you sure that you need to use MetaFun in this case? Could this be an xyproblem? – Dave Jarvis Apr 21 at 21:18
  • Yeap, I want the box to grow vertically to get all the text. I know it's possible to do it with frames, however I would like to know how to do it with MetaFun just for the record and because I have some ideas I would like to make with as well. – dpi Apr 22 at 8:36
  • Have you considered using a frame that has a background drawn using MetaFun, instead? The frame will grow as you expect and then MetaFun can draw with respect to the overlay width and overlay height values. – Dave Jarvis Apr 30 at 21:44

You can limit the width of a string with the \limitatetext command which takes three arguments:

  1. The text you want to shorten.
  2. The maximum length for your text.
  3. A symbol which appears at the end of your text when it exceeds the maximum length.

enter image description here

In the following example the length of the text is limited to 9cm with a box which is 10cm wide.


\limitatetext{This is a looooooooong text that I need to fit in the box.}{9cm}{...}


This produces

enter image description here

| improve this answer | |

For the record, as @Dave Jarvis mentioned frames do the job.

  draw OverlayBox 
       withpen pencircle scaled OverlayLineWidth
       withcolor OverlayLineColor;

  setbounds currentpicture to OverlayBox ;



    \input knuth


| improve this answer | |
  • I hope that you don't mind that I have simplified the code (use OverlayBox instead of manually constructing the box; use OverlayLineWidth, and OverlayLineColor rather than passing these through MPvars and measures). – Aditya May 31 at 5:11
  • Great! However I would have left \measures for reusability but why not! – dpi May 31 at 8:22

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.