4

I'm having troubles with the subfigure package and proper alignment of figures. I'm inexperienced with Latex so please apologize obvious mistakes. The top-left figure is not properly aligned with the figures that follow below. It should be more to the left (please see attached screenshot). Shortened code:

\begin{figure}[h]
hfill
\subfigure[CEO fixed / total]{\includegraphics[width=8cm]{figures/scatter_cash_CapIQ_CEOfixed_to_total.png}}
\hfill
\subfigure[CEO bonus / total]{\includegraphics[width=8cm{figures/scatter_cash_CapIQ_CEObonus_to_total.png}}
\hfill
 \subfigure[CEO long-term / total]{\includegraphics[width=8cm]{figures/scatter_cash_CapIQ_CEOlongterm_to_total.png}}
\hfill
\caption{Compensation measures}
\end{figure}

enter image description here

Yimmer
  • 219
  • Welcome to TeX.sx! Please construct a compilable minimal working example (MWE) from \documentclass... until \end{document}. If you use the graphicx package I recommend using it in the MWE as follows \usepackage[demo]{graphicx} – masu Nov 13 '13 at 23:47
  • The space comes from that first \hfill command before the first subfigure. That command starts a paragraph and is placed at the start of it. Other lines are not similarly affected because horizontal space at linebreaks is discarded. The \hfill at the end of the paragraph (just before the caption) is also discarded, but not the space before it (end of previous line). This can cause a slight distortion of the alignment. Omit these spaces by placing % at the ends of all the subfigure lines. – Dan Nov 14 '13 at 20:44

4 Answers4

3

Try this solution.

\documentclass{article}

\usepackage{graphicx}

\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\begin{figure}[!h]
  \centering
  \begin{tabular}[c]{cc}
    \begin{subfigure}[c]{0.4\textwidth}
      \includegraphics[width=\textwidth]{figures/scatter_cash_CapIQ_CEOfixed_to_total.png}
      \caption{CEO fixed / total}
      \label{fig:ceoa}
    \end{subfigure}&
    \begin{subfigure}[c]{0.4\textwidth}
      \includegraphics[width=\textwidth]{figures/scatter_cash_CapIQ_CEOfixed_to_total.png}
      \caption{CEO fixed / total}
      \label{fig:ceob}
    \end{subfigure}\\
    \begin{subfigure}[c]{0.4\textwidth}
      \includegraphics[width=\textwidth]{figures/scatter_cash_CapIQ_CEOfixed_to_total.png}
      \caption{CEO fixed / total}
      \label{fig:ceoc}
    \end{subfigure}&
    \begin{subfigure}[c]{0.4\textwidth}
      \includegraphics[width=\textwidth]{figures/scatter_cash_CapIQ_CEOfixed_to_total.png}
      \caption{CEO fixed / total}
      \label{fig:ceod}
    \end{subfigure}\\
  \end{tabular}    
  \caption{Compensation measures}
  \label{fig:ceo}
\end{figure}

\end{document}

How it works?

  1. You use a tabular environment to control positioning of your subfigures. Each figure is in a cell in the table. The columns are centered. So the figures remain horizontally aligned even if you change the figure size.
  2. Each subfigure has [c] as position specifier. So, again, the figures remain vertically aligned in a row.
  3. Should you need to change the figure sizes, please change the multiplier with \textwidth (0.4 in this case. So, two side-by-side figures take 0.4 X 2 = 0.8 of your text width, leaving enough white space). Do not make them very large though, you will get overfull boxes.
Troy
  • 13,741
Masroor
  • 17,842
2

Try this: (just replace the image.png's with desired image files)


\documentclass{article}

\usepackage[tight]{subfigure}

% If you want to control the alignment of the pictures
% more accurately, uncomment these and try different values

% \subfigcapmargin = .5cm
% \subfigtopskip = 2cm
% \subfigcapskip = 2cm
% \subfigcaptopadj = 1cm
% \subfigbottomskip = 2cm
% \subfiglabelskip = 2cm

\usepackage{graphicx}

\begin{document}

\begin{figure}%
\centering
\subfigure[][]{%
\label{fig:ex3-a}%
\includegraphics[width=0.45\textwidth]{image.png}}%
\hspace{8pt}%
\subfigure[][]{%
\label{fig:ex3-b}%
\includegraphics[width=0.45\textwidth]{image.png}}\\
\subfigure[][]{%
\label{fig:ex3-c}%
\includegraphics[width=0.45\textwidth]{image.png}}%
\hspace{8pt}%
\subfigure[][]{%
\label{fig:ex3-d}%
\includegraphics[width=0.45\textwidth]{image.png}}%
\caption[A set of four subfigures.]{A set of four subfigure
s:
\subref{fig:ex3-a} describes the first subfigure;
\subref{fig:ex3-b} describes the second subfigure;
\subref{fig:ex3-c} describes the third subfigure; and,
\subref{fig:ex3-d} describes the last subfigure.}%
\label{fig:ex3}%
\end{figure}


\end{document}

Taken from the subfigure package documents

Troy
  • 13,741
Niko Fohr
  • 744
  • 1
  • 7
  • 15
  • Did you notice that subfigure is deprecated? – Werner Nov 14 '13 at 00:21
  • Thanks for your help! While it makes the situation better there is now a larger gab between the top row images compared to the bottom row. How can I get them to be spaced equally? – Yimmer Nov 14 '13 at 12:57
  • There was one extra whitespace letter causing this, but now the code is fine. Be sure that you dont add any extra whitespace after the image data, and that the end of the lines have '%' mark. I also added some options there to do fine tuning. If this answer helped you, please consider marking this as the answer for your question. – Niko Fohr Nov 14 '13 at 15:37
2

Using subcaption alone. If we can use \hspace{} between two adjacent subfigure environments horizontally, then we have to be allowed to use vspace{} between two adjacent subfigure environments vertically. I don't know whether or not this philosophy is correct. But it seems to be correct, doesn't it?

In the following example, I deliberately choose different size for each image just for the sake of generality. But you can change to suit your own preferences.

\documentclass[preview,border=12pt]{standalone}

\usepackage[demo]{graphicx}  

\usepackage{subcaption}

\begin{document}
Beauty without intelligence is a masterpiece painted on a toilet paper.
\begin{figure}[hbtp]
\centering
\begin{subfigure}[c]{.4\linewidth}
    \centering
    \includegraphics[width=4cm]{foo}
    \caption{foo}
    \label{fig:foo}
\end{subfigure}\hspace{12pt}
\begin{subfigure}[c]{.4\linewidth}
    \centering
    \includegraphics[width=3cm]{goo}
    \caption{goo}
    \label{fig:goo}
\end{subfigure}\vspace{12pt}
\begin{subfigure}[c]{.4\linewidth}
    \centering
    \includegraphics[height=4cm]{hoo}
    \caption{hoo}
    \label{fig:hoo}
\end{subfigure}\hspace{24pt}
\begin{subfigure}[c]{.4\linewidth}
    \centering
    \includegraphics[height=3cm]{ioo}
    \caption{ioo}
    \label{fig:ioo}
\end{subfigure}
\caption{The group of foo, goo, hoo, and ioo.}
\label{fig:group}
\end{figure}

\end{document}

enter image description here

Homogeneous Case

When all of your images have the same size, then the output looks much better as follows.

\documentclass[preview,border=12pt]{standalone}

\usepackage[demo]{graphicx}

\usepackage{array}
\usepackage{subcaption}

\begin{document}
Beauty without intelligence is a masterpiece painted on a toilet paper.
\begin{figure}[hbtp]
\centering
\begin{subfigure}[c]{.4\linewidth}
    \centering
    \includegraphics[width=4cm]{foo}
    \caption{foo}
    \label{fig:foo}
\end{subfigure}\hspace{12pt}
\begin{subfigure}[c]{.4\linewidth}
    \centering
    \includegraphics[width=4cm]{goo}
    \caption{goo}
    \label{fig:goo}
\end{subfigure}\vspace{12pt}
\begin{subfigure}[c]{.4\linewidth}
    \centering
    \includegraphics[width=4cm]{hoo}
    \caption{hoo}
    \label{fig:hoo}
\end{subfigure}\hspace{12pt}
\begin{subfigure}[c]{.4\linewidth}
    \centering
    \includegraphics[width=4cm]{ioo}
    \caption{ioo}
    \label{fig:ioo}
\end{subfigure}
\caption{The group of foo, goo, hoo, and ioo.}
\label{fig:group}
\end{figure}

\end{document}

enter image description here

1

The use of minipage-s might be better than manual spacing or the use of a tabular.

\documentclass{article}

\usepackage[demo]{graphicx}

\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\begin{figure}[!h]
  \centering
    \begin{subfigure}[c]{0.65\textwidth}
      \includegraphics[width=\textwidth]{figures/scatter_cash_CapIQ_CEOfixed_to_total.png}
      \caption{CEO fixed / total}
      \label{fig:ceoa}
    \end{subfigure}\hfill%
    \begin{subfigure}[c]{0.25\textwidth}
      \includegraphics[width=\textwidth]{figures/scatter_cash_CapIQ_CEOfixed_to_total.png}
      \caption{CEO fixed / total}
      \label{fig:ceob}
    \end{subfigure}

    \begin{subfigure}[c]{0.65\textwidth}
      \includegraphics[width=\textwidth]{figures/scatter_cash_CapIQ_CEOfixed_to_total.png}
      \caption{CEO fixed / total}
      \label{fig:ceoc}
    \end{subfigure}\hfill%
    \begin{subfigure}[c]{0.25\textwidth}
      \includegraphics[width=\textwidth]{figures/scatter_cash_CapIQ_CEOfixed_to_total.png}
      \caption{CEO fixed / total}
      \label{fig:ceod}
    \end{subfigure}
  \caption{Compensation measures}
  \label{fig:ceo}
\end{figure}

\end{document}
masu
  • 6,571
  • How do you ensure horizontal alignment of the subfigures should he decide to use different widths for different figures? Your scheme works good so long as the horizontal sizes are identical. Moreover, this may not be a good practice to be introduced to somebody who is stepping into LaTeX. – Masroor Nov 14 '13 at 01:15
  • @MMA I didn't think this was a general question but a one-time application. I've revised my code. – masu Nov 14 '13 at 07:29
  • Putting a single subfigure in a minipage seems to be trivial as the subfigure is actually designed to replace the combination of minipage and subcaption (see the documentation of subcaption on page 5). – kiss my armpit Nov 14 '13 at 17:17
  • 1
    @DonutE.Knot: I did not know that and have just fixed my answer. Thanks for the comment. (Your (new) username is still hilarious. :) ) – masu Nov 14 '13 at 17:25