[Tex/LaTex] Leaving no space between subfigures using package subcaption

spacingsubcaptionsubfloats

I want to draw a figure consisting of multiple subfigures. For this, I am using the subcaption package, since the LaTeX Wikibook suggests here that both subfigure and subfig are deprecated.

The subfigures have rectangular borders, and I want no space at all in between subfigures. This should, in theory, be possible by leaving no space in between subfigure environments and inserting graphics which are exactly as wide as the subfigure environments' width. However, there remain this little horizontal gaps in between the subfigures that make everything look awful. I could play around using negative \vspace{}, but I'm looking for a "clean" solution. Is there any way to arrange subfigures to be really beside one another, with no gap in between?

Here is an example of the kind of code I am using in my document:

\documentclass[a4paper]{article}

\usepackage{tikz}
\usepackage{subcaption}
\usepackage[utf8]{inputenc}

\begin{document}
\begin{figure}
  \centering
  \begin{subfigure}{72pt}
     \begin{tikzpicture}[x=72pt]
       \draw[blue] (0,0) rectangle (1,1); 
     \end{tikzpicture}
  \end{subfigure}
  \begin{subfigure}{72pt}
     \begin{tikzpicture}[x=72pt]
       \draw[blue] (0,0) rectangle (1,1); 
     \end{tikzpicture}
  \end{subfigure}
  \begin{subfigure}{72pt}
     \begin{tikzpicture}[x=72pt]
       \draw[blue] (0,0) rectangle (1,1); 
     \end{tikzpicture}
  \end{subfigure}
  \begin{subfigure}{72pt}
     \begin{tikzpicture}[x=72pt]
       \draw[blue] (0,0) rectangle (1,1); 
     \end{tikzpicture}
  \end{subfigure}
\end{figure}
\end{document}

Best Answer

You have spurious spaces at the end of every subfigure. Insert a % and the spaces are removed. As reference, see What is the use of percent signs (%) at the end of lines? Here's a complete MWE:

enter image description here

\documentclass{article}

\usepackage{tikz,subcaption}

\begin{document}
\begin{figure}
  \centering
  \begin{subfigure}{72pt}
     \begin{tikzpicture}[x=72pt]
       \draw[blue] (0,0) rectangle (1,1); 
     \end{tikzpicture}
  \end{subfigure}% <-------------- Added %
  \begin{subfigure}{72pt}
     \begin{tikzpicture}[x=72pt]
       \draw[blue] (0,0) rectangle (1,1); 
     \end{tikzpicture}
  \end{subfigure}% <-------------- Added %
  \begin{subfigure}{72pt}
     \begin{tikzpicture}[x=72pt]
       \draw[blue] (0,0) rectangle (1,1); 
     \end{tikzpicture}
  \end{subfigure}% <-------------- Added %
  \begin{subfigure}{72pt}
     \begin{tikzpicture}[x=72pt]
       \draw[blue] (0,0) rectangle (1,1); 
     \end{tikzpicture}
  \end{subfigure}
\end{figure}
\end{document}