Top Banner
sign up log in tour help Take the 2minute tour × TeX LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required. How to superimpose LaTeX on a picture? I know how to add a picture to my LaTeX file, e.g. . What I'd now like to do is place some latex symbols onto the picture. Like a to name a curve. I do not want to do that in the graphics editing program e.g. because I might want to rename that curve later and don't want to repeat the editing process. \includegraphics[width=7cm]{curve.pdf} \gamma I know I can do a in a environment. But this environment forces me to specify its size at the beginning. That would be cumbersome since I'd have to go open the graphics file and do some calculations. Is there a way to get the dimensions (or aspect ratio) of a graphics file within LaTeX, so I might use it there? But even if I could do these calculations automatically, I still wouldn't like the redundancy of specifying the width for again, but I could live with that. \put picture \includegraphics Also, is it possible to specify the size in to the environment? I just tried and got some errors. cm picture So basically what I am looking for is a solution as simple as possible which: a) lets me specify a width or a height and a file, and displays the image scaled while keeping its aspect ratio and then b) lets me put LaTeX stuff inside the image in relative coordinates (percentages or 0...1), so I won't have to redo anything should I decide that the image needs to be a bit larger or smaller. (b is kind of like setting to the whole width=height for the 'picture' environment. But of course most images are not square and I also don't appreciate the redundancy (but could live with it).) \unitlength {graphics} {symbols} {scaling} edited Jun 15 '11 at 22:50 lockstep 138k 29 398 569 asked Jun 15 '11 at 8:10 peter 819 1 11 16 5 See . Drawing on an image with TikZ Martin Scharrer Jun 15 '11 at 8:14 1 The package allows the use of explicit length such as in the environment. picture 2cm picture egreg Jun 15 '11 at 8:24 2 Answers use the package. overpic \documentclass{article} \usepackage[percent]{overpic} \begin{document} \begin{overpic}[width=0.5\textwidth,grid,tics=10]{pictures/baum} \put (20,85){\huge$\displaystyle\gamma$} \end{overpic} \end{document} will give you
5

Graphics - How to Superimpose LaTeX on a Picture

Jan 27, 2016

Download

Documents

This discussion shows how to use the environment overpic on LATEX to add any kind of element on top of a figure.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Graphics - How to Superimpose LaTeX on a Picture

      

sign up log in tour help 

 Take the 2­minute tour ×TeX ­ LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and relatedtypesetting systems. It's 100% free, no registration required.

How to superimpose LaTeX on a picture?

I know how to add a picture to my LaTeX file, e.g. . What I'd now like to do is placesome latex symbols onto the picture. Like a to name a curve. I do not want to do that in the graphics editingprogram e.g. because I might want to rename that curve later and don't want to repeat the editing process.

\includegraphics[width=7cm]{curve.pdf}

\gamma

I know I can do a in a environment. But this environment forces me to specify its size at the beginning. Thatwould be cumbersome since I'd have to go open the graphics file and do some calculations. Is there a way to get thedimensions (or aspect ratio) of a graphics file within LaTeX, so I might use it there? But even if I could do these calculationsautomatically, I still wouldn't like the redundancy of specifying the width for again, but I could live withthat.

\put picture

\includegraphics

Also, is it possible to specify the size in to the environment? I just tried and got some errors.cm picture

So basically what I am looking for is a solution as simple as possible which:

a) lets me specify a width or a height and a file, and displays the image scaled while keeping its aspect ratio and then

b) lets me put LaTeX stuff inside the image in relative coordinates (percentages or 0...1), so I won't have to redo anythingshould I decide that the image needs to be a bit larger or smaller.

(b is kind of like setting to the whole width=height for the 'picture' environment. But of course most imagesare not square and I also don't appreciate the redundancy (but could live with it).)

\unitlength

{graphics} {symbols} {scaling}

edited Jun 15 '11 at 22:50lockstep138k 29 398 569

asked Jun 15 '11 at 8:10peter819 1 11 16

5   –  See .Drawing on an image with TikZ Martin Scharrer♦ Jun 15 '11 at 8:14

1   –  The package allows the use of explicit length such as in the environment.picture 2cm pictureegreg Jun 15 '11 at 8:24

2 Answers

use the package.overpic

\documentclass{article}\usepackage[percent]{overpic}

\begin{document}\begin{overpic}[width=0.5\textwidth,grid,tics=10]{pictures/baum} \put (20,85) {\huge$\displaystyle\gamma$}\end{overpic}\end{document}

will give you

Page 2: Graphics - How to Superimpose LaTeX on a Picture

Remove the 'grid' option after you are done putting symbols in the picture. Overpiccan basically be used as a replacement for \includegraphics. Also, recently a userfound some minor bug where the figure turned out to be slightly shifted (probablydue to a number rounding error). I never had (or noticed) this issue myself.

edited Jun 15 '11 at 13:33 answered Jun 15 '11 at 9:56Martin H9,289 1 22 42

    –  works like a charm. thanks. peter Jun 15 '11 at 10:48

    –  is there a way to scale the picture before you apply the symbol? Yiannis Lazarides Jun 15 '11 at11:15

1   – 

@Yiannis Yes of course you can add the usual parameters for includegraphics to overpic.Something like \begin{overpic}[width=0.5\linewidth,grid,tics=10]{pictures/baum} should workMartin H Jun 15 '11 at 12:26

    – 

Thanks, if you don't mind won't you make a short edit in your reply to make it easy for otherpeople to see as well? Yiannis Lazarides Jun 15 '11 at 12:40

   

– 

man it's great to work with this. it's more than i expected with this grid. i love it! unfortunately ican't vote it up more than once ;-). just 2 things: why that '\displaystyle'? it seems to do the samewithout it. and: is there a way to specify where the put coords are within the stuff to be put? rightnow it seems to be lower left corner, this way positions won't stay exactly correct when rescaling(by a very small factor). is there a way to center it (like in the pi answer below)? peter Jun 15'11 at 18:16

Requirement 1:

a) lets me specify a width or a height and a file, and displays the image scaledwhile keeping its aspect ratio and then

There are 3 "mutual exclusive" options that can be chosen to keep the aspect ratioeasily.

(dimensionless)scale

(dimension)width

(dimension)height

For example:

makes the size of the rendered graphics 3 timesas big as its original size.\includegraphics[scale=3]{filename}

makes the size of the rendered graphics 3/4times as small as its original size.\includegraphics[scale=0.75]{filename}

makes the width of the rendered graphicsequal to 3cm.\includegraphics[width=3cm]{filename}

makes the width of the rendered\includegraphics[width=0.5\linewidth]{filename}

Page 3: Graphics - How to Superimpose LaTeX on a Picture

graphics equal to half the width of available space. makes the height of the rendered graphics

equal to 3cm.\includegraphics[height=3cm]{filename}

makes the height of the renderedgraphics equal to half the width of available space.\includegraphics[height=0.5\linewidth]{filename}

It is common to use as the reference as follows\linewidth

% Ratio of the rendered graphics width to the line width.\def\RatioToLineWidth{0.75}\savebox\IBox

{%    \includegraphics[width=\RatioToLineWidth\linewidth]{Images/pie}%}

One unit in PSTricks equal to 1 cm by default. Therefore the most right and top gridlabels represent the width and the height of the rendered graphics, respectively. Theybehave as the horizontal and vertical rulers. You can make use them to specify theposition at which the math symbol will be placed. For example, you want to put thepie symbol at (4.5cm, 3.5cm) as follows.

\rput(4.5,3.5){\psscalebox{20}{\color{white}$\pi$}}

Unfortunately, this coordinate is not the centroid of the rendered graphics. The exactcoordinate of the centroid can be easily obtained as follows.

\rput(0.5\width,0.5\height){\psscalebox{20}{\color{white}$\pi$}}

where and are the width and height of the rendered graphics,respectively. They are defined as follows in the preamble.

\width \height

\width=\wd\IBox\height=\ht\IBox

The complete code and the resulting PDF output are given below. I know you are nothappy with this way. Please see the remaining answer how to accomplish your secondrequirement.

 \documentclass{article}\usepackage{graphicx,pstricks}

\newpsstyle{gridstyle}{    gridwidth=0.1pt,    subgridwidth=0.05pt,     gridlabels=5pt,    gridcolor=green,     subgridcolor=white,     subgriddiv=2}

Page 4: Graphics - How to Superimpose LaTeX on a Picture

\psset{style=gridstyle}

\newsavebox\IBox

% Ratio of the imported graphics width to the line width.\def\RatioToLineWidth{0.75}\savebox\IBox

{%    \includegraphics[width=\RatioToLineWidth\linewidth]{Images/pie}%}

\newdimen\width

\newdimen\height

\width=\wd\IBox\height=\ht\IBox

\pagestyle{empty}

\begin{document}\begin{pspicture}(\width,\height)           \rput[lb](0,0){\usebox\IBox}    \rput(0.5\width,0.5\height){\psscalebox{20}{\color{white}$\pi$}}    \psgrid% comment this row if  you want to hide the grid.\end{pspicture}\end{document}

Requirement 2:

b) lets me put LaTeX stuff inside the image in relative coordinates (percentages or0...1), so I won't have to redo anything should I decide that the image needs to bea bit larger or smaller.

(b is kind of like setting \unitlength to the whole width=height for the 'picture'environment. But of course most images are not square and I also don't appreciatethe redundancy (but could live with it).)

Dividing the horizontal/vertical axis into 100 or 1 part(s) is practically uncomfortable.The former makes the grid labels crowded and the latter makes low precision. In mypersonal preference, dividing the horizontal/vertical axis into 10 parts is goodenough. To do this, just set and as follows.xunit yunit

\psset

{    xunit=0.1\width,    yunit=0.1\height}

Here we use and as references. For instance, (x,y) in the code belowmust be regarded in our mind as (x/10 of , y/10 of ).

\width \height

\width \height

Page 5: Graphics - How to Superimpose LaTeX on a Picture

\documentclass{article}\usepackage{graphicx,pstricks}

\newpsstyle{gridstyle}{    gridwidth=0.1pt,    subgridwidth=0.05pt,     gridlabels=5pt,    gridcolor=green,     subgridcolor=white,     subgriddiv=2}

\psset{style=gridstyle}

\newsavebox\IBox

% Ratio of the imported graphics width to the line width.\def\RatioToLineWidth{0.75}\savebox\IBox

{%    \includegraphics[width=\RatioToLineWidth\linewidth]{Images/pie}%}

\newdimen\width

\newdimen\height

\width=\wd\IBox\height=\ht\IBox

\pagestyle{empty}

\begin{document}

\psset

{    xunit=0.1\width,    yunit=0.1\height}

\begin{pspicture}(\width,\height)           \rput[lb](0,0){\usebox\IBox}    \rput(0.5\width,0.5\height){\psscalebox{20}{\color{white}$\pi$}}    \psgrid% comment this row if  you want to hide the grid.\end{pspicture}\end{document}

I got an info from PSTricks manual, rather than creating a new box , we can usethe existing one .

\IBox

\pst@boxg

edited Aug 5 '11 at 5:46 community wiki8 revsxport

    –  how will the grid change if you change the 'width=' of the '\includegraphics'? peter Jun 15 '11at 18:08

1   – 

so it will remain a 9 by 12 grid even if the picture is half the size? if this is true those numbersseem a bit random. no pdflatex makes it a no-go for me, but it is interesting anyway. peterJun 15 '11 at 18:19

    – 

No. The grid labels are not random numbers. Depending on how you specify the , they areautomatically adjusted instead.

unitxport Jun 16 '11 at 7:01