Sign up ×
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.

This question already has an answer here:

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?


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


% 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
  \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} };

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.

share|improve this question

marked as duplicate by Werner, Papiro, Adam Liter, moewe, Mico Aug 27 '14 at 17:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

One of the most popular TikZ questions of this site:… –  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

2 Answers 2

I've adjusted your macro a bit to add a transparent background for when the text becomes hard to read:

  \draw (0,0) 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);

The resulting scalebar looks like this: Scalebar with background

See my gist for more info and a scalebar without a background:

share|improve this answer
Your blog post is not available anymore :-( –  the_nic Sep 3 at 12:42
I have updated the link. Thanks for reminding me! –  Ruben Sep 4 at 14:27

I found IPE to be a nice tool for this.

ipelets -> insert image.

Then add the text and save as eps or pdf.

share|improve this answer
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. –  Mr. 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

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