# ConTeXt: How to force text to fit in a MetaFun box?

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

For now I have

\startuseMPgraphic{MPBoxFrame}
% 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 ;
\stopuseMPgraphic

\defineoverlay[MPBoxFrame][\useMPgraphic{MPBoxFrame}]

\defineframedtext[BoxFramedText][frame=off,
background=MPBoxFrame,
offset=\bodyfontsize,
width=\textwidth]

\definestartstop[SpecificWidthBox][alternative=empty,
before=\startBoxFramedText,
after=\stopBoxFramedText]

\starttext
\startSpecificWidthBox
This is a looooooooong text that I need to fit in the box.
\stopSpecificWidthBox
\stoptext


producing:

• 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.

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

\starttext

\startframed[width=10cm]
\limitatetext{This is a looooooooong text that I need to fit in the box.}{9cm}{...}
\stopframed

\stoptext


This produces

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

\startuseMPgraphic{MPBox}
draw OverlayBox
withpen pencircle scaled OverlayLineWidth
withcolor OverlayLineColor;

setbounds currentpicture to OverlayBox ;
\stopuseMPgraphic

\defineoverlay[MPBoxOverlay][\useMPgraphic{MPBox}]

\defineframedtext[MPBoxFramed]
[
frame=off,
background=MPBoxOverlay,
offset=\bodyfontsize,
framecolor=red,
rulethickness=0.5pt,
width=100mm,
]

\starttext
\startMPBoxFramed
\input knuth
\stopMPBoxFramed
\stoptext


• 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