[metapost] accessing letters in picture (for...within loop)

Dan Luecking luecking at uark.edu
Tue Nov 21 21:14:13 CET 2006

At 06:21 PM 11/20/2006, you wrote:
>the for...within loop provides access to text inside a picture
>variable together with properties such as the font name, coordinates,
>scale, color etc.  The loop doesn't always return text letter by letter,
>but very often returns arbitrary bunches of letters at once (whose
>length depends on the font in use).  This is very unfortunate, since I
>want to access the coordinates of single letters.  Is there any way to
>tell MetaPost to process pictures letter by letter?

You could build the text one letter at a time, measure the picture
each time an keep an array of lengths. Keep track seperately of the
width of each character. Then one can calculate the width and location
of each character: The width of
   btex Hello, wo etex
minus the width of
   btex o etex
gives the location of the start of the second o in
   btex Hello, world etex

If you tried to do this is TeX, it would require the same
calculations. Neither MetaPost nor TeX gives direct user
access to individual letters in an already formed box/picture.
MetaPost is even a little better than TeX in that one can tell
from the individual strings returned by for...within
where kerning has taken place. This could even be used to
help determine where a character is located: within each separate
string there should be no kerning, so one can tell from its length
(and from the length of each character) where the character


Daniel H. Luecking
Department of Mathematical Sciences
University of Arkansas
"I reject your reality, and substitute my own." -- Adam Savage

More information about the metapost mailing list