# How to overlay text with a graphics and specify the relative position?

I have some random graphics with specified ImageSize and AspectRatio, e.g., this line

g1 = Graphics[Line[{{1, 0}, {2, 1}, {3, 0}, {4, 1}}], ImageSize -> 230, AspectRatio -> 1/2]

and some text

g2 = Text[Style["1+1", Red, Large, Bold], Background -> LightRed]

as shown below:

How do I "overlay" the text g2 ontop of g1 and specify the relative position? For example, I want the center of g2 to be located at {1/3,7/8} of g1, which means 1/3 the width and 7/8 of the height of g1, both measured from the lower left corner of g1. By doing this, the text g2 will be just below the first peak of g1. How can this be done?

You can put text directly into Graphicsthus

   Graphics[{Line[{{1, 0}, {2, 1}, {3, 0}, {4, 1}}], ImageSize -> 230,
AspectRatio -> 1/2,
Text[Style["1+1", Red, Large, Bold], {1/3 4, 7/8 1},
Background -> LightRed]}, Frame -> True]


I have added Frame-> True so I could see the coordinates of where to put the text. Look up Text there is also an option for an offset.

Does that help?

• Thank you @Hugh for the answer. Is it possible to fill the space above the line with a solid color up to the upper frame? – nanjun May 13 '19 at 21:08
• All depends what you are trying to do. If you use Plot and express your line as a function then there are fill commands. However, if you are looking for geometric shapes then Polygon may be what you need.I can't be sure what your general case is until you show more details. This may be suitable for another question. – Hugh May 14 '19 at 11:02