# How to add a scalebar to an image

I'm trying to figure out how best to add scale bars to my images, such as in these wondeful snowflake images (the full size image is a bit large, but it's the best example of what I'm trying to do):

One possibility would be to insert the scale bars using a different program. However, I want to ensure the text is nice and crisp, rather than having it saved as part of a raster image. Also, if I insert the scale bars beforehand, then the text will get resized when LaTeX scales the images.

So far I haven't been able to find any existing commands or packages that make this easy to do. I would like to be able to specify the real world size of the image, and have the size of the scale bar be calculated.

If I have to write my own macro, I think I'll start with the overpic package

Am I headed in the right direction?

EDIT 2:

After familiarizing myself with TikZ, here is the macro I've come up with:

\usepackage{tikz}
\usepackage{siunitx}

% Inserts a scale bar into an image
% Optional argument 1: the colour of the bar and text
% Argument 2: an \includegraphics command
% Argument 3: the real world width of the image
% Argument 4: the length of the scale bar
% Argument 5: the units in which the scale bar is measured
\newcommand{\scalebar}[5][white]{
\begin{tikzpicture}
\draw (0,0) node[anchor=south west,inner sep=0] (image) { #2 };
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\fill [#1] (0.05,0.2cm) rectangle (#4/#3+0.05,0.4cm);
\draw [#1] (0.05,0.4cm) node[anchor=south west] { \SI{#4}{#5} };
\end{scope}
\end{tikzpicture}
}


I'm using a combination of units here (cm and fractions of the image size). I feel like this aspect could be a bit more elegant, but what I have now is sufficent for my purposes.

-
One of the most popular TikZ questions of this site: tex.stackexchange.com/questions/9559/… – percusse Aug 19 '12 at 15:41
Looks like a good place to start - I've heard of TikZ but never knew exactly what it was for... I'll give it a shot. – Anthony Aug 19 '12 at 15:48