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 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}
share|improve this question
1  
Don't believe everything a Wiki says ;-) subfig is not deprecated (subfigure is indeed obsolete, though). –  Gonzalo Medina Jul 22 '13 at 17:50

1 Answer 1

up vote 4 down vote accepted

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}
share|improve this answer
    
I'm confused. I couldn't find any spaces at the end of my lines, and still adding the % helped me fix my problem; my output looks like I wanted... I don't understand which spaces I am omitting, since there were none after the curly braces. –  MHaaZ Jul 22 '13 at 17:45
    
@MHaaZ: If you leave a line-break, that is similar to a space (commonly referred to as a spurious space). Adding % acts to concatenate the two lines, almost like writing \end{subfigure}\begin{subfigure}. Be sure to leave no spaces though. For example, the following will still insert a space \end{subfigure} %, since it translates to \end{subfigure} \begin{subfigure}. Try it... –  Werner Jul 22 '13 at 17:50
    
Thanks a lot for clarifying. So every time I insert a linebreak into my source file, an extra space gets added into the output? That seems weird... what am I missing? (feel free to tell me to RTFM, as long as you reference said manual :)) –  MHaaZ Jul 22 '13 at 17:52
    
Just realized you included a reference at the bottom of your answer. I just saw your code and didn't keep on reading... might wanna push that link before the code so other impatient people don't miss it :) –  MHaaZ Jul 22 '13 at 17:53
    
@MHaaZ: Done. And yes, but multiple spaces are gobbled into a single one. In a similar sense, you can leave 1 line or 100 between paragraphs. –  Werner Jul 22 '13 at 17:55

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.