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.

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.

share|improve this question
2  
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
add comment

2 Answers 2

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

\newcommand{\scalebarbackground}[6][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 [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: Scalebar with background

See my blog post for more info: http://universitysoundbites.blogspot.nl/2014/07/add-scalebars-to-images-in-latex.html

share|improve this answer
add comment

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. –  jenson_bo Nov 19 '13 at 23:11
add comment

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.