Skip to content

Instantly share code, notes, and snippets.

@i3v
Forked from rbnvrw/LaTeXScaleBars.md
Last active August 22, 2018 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save i3v/2a98c56f8108f23b96920d17f2341479 to your computer and use it in GitHub Desktop.
Save i3v/2a98c56f8108f23b96920d17f2341479 to your computer and use it in GitHub Desktop.
Adding scalebars to images in LaTeX

Scalebar with background - a demonstration of a little issue with two pics next to each other

Here's an MWE:

\documentclass{article}

\usepackage{graphicx}
\usepackage{subcaption}

\usepackage{tikz}
\usepackage{color}
\usepackage{siunitx}
\definecolor{scalebgcolor}{rgb}{0.08,0.52,0.80}

\newcommand{\scalebarbackground}[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 [fill=scalebgcolor, 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}{\micro \meter}} (#5*#4/#3+0.04,1.2em);
  \end{scope}
 \end{tikzpicture}
}


\begin{document}
\begin{figure}

	\subcaptionbox{}[.3\linewidth]{%		
			\scalebarbackground{%
				\includegraphics[width=\linewidth]{example-image-a}% 
				}{10}{1}{2}%
	}%
	\subcaptionbox{}[.3\linewidth]{%
		\includegraphics[width=\linewidth]{example-image-b}%
	}% 
	\hfill\null%
	\caption{ 2nd pic is slighly lower than the 1st. }%

\end{figure}

\end{document}

The result looks like this (the second image is a tiny bit lower): The second pic is a bit lower

And that's not a viewer issue, it's really a bit lower: The second pic is a bit lower - zoom

To fix this, replace \draw (0,0) node[anchor=south west,inner sep=0] (image) { #2 }; with just \node[anchor=south west,inner sep=0] (image) { #2 };. Now everything looks fine:

The second pic is at the same height

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment