1

I am trying to use the subcaption package for horizontally aligning up some figures, and I did as the documentation instructs me to do, yet the result is these are vertically aligned. It really confuses me.

\documentclass{article}

\usepackage[font=small]{caption}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage{xltxtra}
\usepackage{fontspec}


\setmainfont[Ligatures=TeX]{Linux Libertine O}

\begin{document}
\begin{figure}[htp]

    \centering
    \begin{minipage}[t]{0.3\linewidth}
        \includegraphics[width=\linewidth]{fig/bed1.jpg}
        \subcaption{Bedroom 1}
    \end{minipage}

    \begin{minipage}[t]{0.3\linewidth}
        \includegraphics[width=\linewidth]{fig/bed2.jpg}
        \subcaption{Bedroom 2}
    \end{minipage}
    \caption{Two bedrooms}

\end{figure}
\end{document}

2 Answers 2

7

The second minipage is moved onto a new line because you have an extra line between the first \end{minipage} and the second \begin{minipage}. Check the documentation here http://mirror.ctan.org/macros/latex/contrib/caption/subcaption.pdf and you'll see the missing newline in the example. The following MWE will put them on the same line:

\documentclass{article}
\usepackage[font=small]{caption}
\usepackage{subcaption}
\usepackage{graphicx}
\begin{document}
\begin{figure}[htp]
    \centering
    \begin{minipage}[t]{0.3\linewidth}
        \includegraphics[width=\linewidth]{fig/bed1}
        \subcaption{Bedroom 1}
    \end{minipage}%
    \begin{minipage}[t]{0.3\linewidth}
        \includegraphics[width=\linewidth]{fig/bed2}
        \subcaption{Bedroom 2}
    \end{minipage}
    \caption{Two bedrooms}
\end{figure}
\end{document}
6
  • I have removed the newline but result is still the same.
    – qed
    Aug 6, 2013 at 13:13
  • @CravingSpirit Did you compile this code? When I do (with the demo option to graphicx as I don't have the pictures) both subfigures are placed next to each other as advertised!
    – cgnieder
    Aug 6, 2013 at 13:19
  • i see the problem now, I changed the width to 0.5, try it yourself and you will see it really doesn't work in that width, while if you remove the \centering line, everything will look as expected. Mysterious thing.
    – qed
    Aug 6, 2013 at 15:52
  • @CravingSpirit not really mysterious: with 0.5\linewidth both minipages don't fit in a line anymore: there's also a space between them. With \centering they're placed below each other then. Without you'll get the warning Overfull \hbox (2.22221pt too wide). If you add a % after the first \end{minipage} they'll fit again.
    – cgnieder
    Aug 6, 2013 at 16:05
  • Ok. What is the % for? Thanks.
    – qed
    Aug 6, 2013 at 16:40
0

darthbith's answer is partially right, here is how I solved it:

  1. remove the \centering line in the figure environment
  2. remove the superfluous newline as darthbith suggested.
4
  • The centering line should have no effect on whether or not there is a new line in the figure...
    – darthbith
    Aug 6, 2013 at 13:37
  • Indeed it has. But maybe it also has something to do with the command you use to compile the doc? I use xelatex.
    – qed
    Aug 6, 2013 at 14:16
  • I use xelatex as well, but as with cgnieder, the \centering has no effect in terms of line breaks when I compile. Perhaps it has something to do with your fonts? I eliminated those in my MWE because they are not installed on my system.
    – darthbith
    Aug 6, 2013 at 14:18
  • The \centering has nothing to do with the problem posted in the question, see my other comment
    – cgnieder
    Aug 6, 2013 at 16:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .