如何使用 tikz 删除图形上绘图的多余填充?

如何使用 tikz 删除图形上绘图的多余填充?

我正在尝试使用 tikz 绘制一个图形,使用从这个答案。以这种方式绘制的图形周围(即子图形之间)有额外的填充,我无法消除。将下图的顶行与底行进行比较。

我可以通过添加 \hskip 并手动找到给定数字的正确数字来解决这个问题(参见 MWE 的注释部分)

在此处输入图片描述

  \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

好的,我发帖后才弄清楚了这一点。我想,让 MWE 足够简化它,我弄清楚了哪里出了问题。

我定义的命令中有一个不可见的空格 - 我想这就是显示的全部内容。请看我在行尾插入 % 的两个位置:

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

相关内容