# How to add a scalebar to an image [duplicate]

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.

## marked as duplicate by Werner, user13907, Adam Liter, moewe, MicoAug 27 '14 at 17:56

• 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
• Great! a more robust way to ensure the text centering: \draw [#1] (0.05+#4/#3/2,0.4cm) node[anchor=south]{\SI{#4}{#5}}; – Dorian Sep 19 '14 at 7:49

\newcommand{\scalebarbackground}[6][white]{
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] (image) { #2 };
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\fill [fill=blue, fill opacity=0.5] (0.04,1.3em) rectangle (#5*#4/#3+0.04,0.1em);
\draw [#1, line width=0.2em] (0.04,1.2em) -- node[below,inner sep=0.1em, font=\footnotesize] {\SI{#5}{#6}} (#5*#4/#3+0.04,1.2em);
\end{scope}
\end{tikzpicture}
}


The resulting scalebar looks like this:

See my gist for more info and a scalebar without a background: https://gist.github.com/rbnvrw/00312251b756f6b48084#file-latexscalebars-md

• Your blog post is not available anymore :-( – the_nic Sep 3 '15 at 12:42
• I have updated the link. Thanks for reminding me! – Ruben Sep 4 '15 at 14:27
• SE answers should be self contained. You can put in an external link, but you still need to include enough in you answer to make use of it. There's no usage example given. Also, the external link didn't explain things very well either, and I couldn't figure out what your parameters 4 and 5 are supposed to be (the explanation in the comments in the external link seems to be a mistake?). Since they're just multiplied together I realized I just set one of them to 1, and the other to the length I want. – argentum2f Jun 27 '18 at 23:33

I found IPE to be a nice tool for this.

ipelets -> insert image.

Then add the text and save as eps or pdf.

• That is a bit inelegant in my opinion. Better to use the OP solution to ensure you have the same fonts across the board, rendered by the same method. – The V Nov 19 '13 at 19:14
• I agree with you that using TikZ is more elegant, but notice that IPE is rendering the fonts in LaTeX, so you can have the same font this way too. Some people like to draw their pictures and other like to program them, I added this answer for the former. – jens_bo Nov 19 '13 at 23:11