1

I am trying to draw over a figure using tikz, using a method for adding scalebars from this answer. The figures that are drawn over this way have extra padding around them (i.e. between the subfigures) that I can't get rid of. Compare the top row of the image below with the bottom row.

I can kind of workaround it by adding an \hskip and manually finding the right number for a give figure (see commented out portion of MWE)

enter image description here

  \documentclass[12pt,a4paper]{article}
  \usepackage{graphicx}
  \usepackage{subcaption}
  \usepackage{tikz}

  % Add a scalebar to an image
  \newcommand{\scalebar}[5][white]{%
   \noindent\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=black, fill opacity=0.5] (0.02,0.1cm) rectangle (#4/#3+0.12,1cm);
     \fill [#1] (0.05,0.2cm) rectangle (#4/#3+0.05,0.4cm);
     \draw [#1] (0.00,0.4cm) node[anchor=south west, font=\footnotesize] {{#4}{#5} };
    \end{scope}
   \end{tikzpicture} %\hskip -1pt % This is my only workaround
  }

  \begin{document}

  \begin{figure}[h]
      \centering
      \begin{subfigure}{\textwidth}
          \caption{}
          \includegraphics[width=.3\textwidth]{example-image-golden}
          \includegraphics[width=.3\textwidth]{example-image-golden}
          \includegraphics[width=.3\textwidth]{example-image-golden}
      \end{subfigure}
      \begin{subfigure}{\textwidth}
          \caption{}
          \scalebar{\includegraphics[width=.3\textwidth]{example-image-golden}}{6}{1}{mm}
          \scalebar{\includegraphics[width=.3\textwidth]{example-image-golden}}{6}{1}{mm}
          \includegraphics[width=.3\textwidth]{example-image-golden}
      \end{subfigure}
  \end{figure}

  \end{document}

1 Answers1

1

Ok, I figured this a little after posting. I guess making the MWE simplified it enough I figured out what was wrong.

My defined command had an invisible space in it - that was all that was showing up, I think. See the two spots where I inserted a % at the end of the line:

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