Take the 2-minute 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.

For my figures thus far I've been using LaTeX's straightforward figure code, i.e.:

\begin{figure}
    \centering
    \includegraphics[width=6.5in]{figure.png}
    \caption[Stuff here]{(A) More stuff here and (B) here.}
    \label{background-operating-mode}
\end{figure} 

All well and good until I've got a multi-part figure, e.g., part (A) and part (B) that need to be labeled within the same PNG file. What I'd like to do is this: insert the PNG file as the 'bottom layer'; on top of that, in a location of my choosing, put in a black rectangle, as the 'middle layer'; on top of that, in roughly the same location, put in a white character of text saying, for example, "A".

I've searched and saw that TikZ may be a way to do this. However I'm deeply confused by it (first attempt below), so I ask, is there a simpler way to achieve this very basic task? I'm not looking to draw a figure from scratch... just want to overlay a rectangle and a character on a PNG. What's the simplest way to do this?

\noindent \begin{tikzpicture}
    {\includegraphics[width=6.5in]{group-figures/background/fig1.png}};
    \fill (0,0) rectangle (20pt, 20pt);
\end{tikzpicture}
share|improve this question
    
See also Drawing on an image with TikZ –  Torbjørn T. Oct 4 '11 at 12:06

1 Answer 1

up vote 30 down vote accepted

The simplest way is to use the onimage package from the From Answers to Packages project, which implements the answer given in Drawing on an image with TikZ.

To use the package: Download the file onimage.dtx from bazaar.launchpad.net/~tex-sx/tex-sx/development/files, run pdflatex on it, and move the resulting .sty file to your working directory.

Then you can load the onimage package and use the tikzonimage environment, which has the form

\begin{tikzonimage}[<options for includegraphics>]{<image name>}[<options for TikZ>]
...
\end{tikzonimage}`

The coordinate system will be scaled in such a way that (0,0) is the bottom left corner of the image, and (1,1) is the top right. You can disable this scaling, if you wish, by setting the key tsx/scale cs=none in the optional TikZ arguments.

Here's a simple example:

\documentclass{article}
\usepackage{onimage}

\begin{document}
\begin{tikzonimage}{bottle.jpg}
    \draw [orange, line width=5pt] (0.5,0.92) circle [radius=0.75cm];
\end{tikzonimage}
\end{document}

You can switch on a grid to help you position your TikZ objects by setting the key tsx/show help lines.

\documentclass{article}
\usepackage{onimage}

\begin{document}
\begin{tikzonimage}{bottle.jpg}[tsx/show help lines]
    \draw [orange, line width=5pt] (0.5,0.92) circle [radius=0.75cm];
\end{tikzonimage}
\end{document}


In order to get the labels to look like you described them, you can use the font=<font commands> option to set the font options, fill=<color> for the background and text=<color> for the text color. To keep things neat, you can define a new style for this using

\tikzset{
     <new style name>/.style={
        fill=black,
        text=white,
        font=\fontfamily{phv}\selectfont\Large\bfseries
    }
}

Here's an example in which I've also added the label position (0.2 cm from the bottom right edge of the images) as part of the style definition, and made the style alter the every node/.style, which is applied to all nodes in a TikZ picture. This allows us to apply this newly defined style to the whole TikZ picture in the tikzonimage optional argument, instead of applying it to the node itself using \node [<style>] {};:

\begin{document}

\tikzset{
    image label/.style={
        every node/.style={
            fill=black,
            text=white,
            font=\fontfamily{phv}\selectfont\Large\bfseries,
            anchor=south east,
            xshift=-0.2cm,
            yshift=0.2cm,
            at={(1,0)}
        }
    }
}

\begin{tikzonimage}[width=0.5\textwidth]{bottle}[image label]
\node{A};
\end{tikzonimage}
\begin{tikzonimage}[width=0.25\textwidth]{bottle}[image label]
\node{B};
\end{tikzonimage}

\end{document}
share|improve this answer
    
Love it - thank you very much! –  Max Oct 4 '11 at 12:49
    
And a follow-up question, how do I change the color, font type and size in the following command: \node at (0.015,0.98) {A}; I can change color with \node at (0.015,0.98) {{\color{white}A}}; is this the best way? –  Max Oct 4 '11 at 12:58
    
Hmmm, I've figured that I can do this, but it seems hilariously ugly: \node at (0.015,0.98) {{\color{white}\fontfamily{phv}\selectfont{\LARGE{\bfseries{A}}}}}; –  Max Oct 4 '11 at 13:07
1  
I've added an example that shows how to use the fill, text, and font keys. –  Jake Oct 4 '11 at 23:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.