[Tex/LaTex] Subfloat caption width requirements and alignment problems

captionshorizontal alignmentsubfloatsvertical alignment

I'm having problems with conflicting width requirements for subcaptions on subfloats that are horizontal versus vertical. I've spent considerable time searching here and more broadly, poring over the subfig package documentation, etc, and I figured out that the problem is with the widths.

For the subfloats that are arranged horizontally, the subcaptions need to be 3 inches wide; for the subfloats that are arranged vertically, the subcaptions need to be 5 inches wide. If I specify a 3in subcaption width, the subcaptions for the 5in images look awful. If I specify anything over a 3in subcaption width, the subcaptions for the 3in images are offset to the right and run off the page. When I was not specifying caption widths at all, the subfloat subcaptions became very narrow and weird.

I'm not sure how to resolve this; both subfloat arrangements need to be included in the same document. I realize that a custom environment might be in order, but don't have the first idea of how to construct something for this purpose.

Here is the example MWE (to the best of my ability, it's a first try…)

\documentclass[11pt]{article}
\usepackage{tabulary}
\usepackage{ctable}
\usepackage{epsfig}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{multirow}
\usepackage{amssymb}
\usepackage{subfig}

\captionsetup[subfloat]{
nearskip=20pt,
width=3in
}

\captionsetup{
format=hang,
width=5.5in,
}

\begin{document}

\title{Title}
\author{Me}
\date{\today}
\maketitle

Vertically arranged images:

\begin{figure}[htbp]
\begin{center}
\subfloat[Calibration image on the front of the view guide.]{\label{fig:vizcardfront}}\includegraphics[width=5in]{figures/viewcardfront.jpg}
\subfloat[Data submission form on the back of the view guide.]{\label{fig:vizcardback}}\includegraphics[width=5in]{figures/viewcardback.jpg}
\caption{The visibility monitoring view guide.}
\label{fig:viewcard}
\end{center}
\end{figure}

Horizontally arranged images:

\begin{figure}[htpb]
\begin{center}
\subfloat[A plantcam at a monitoring plot.]{\label{fig:plantcam}}{\includegraphics[width=3in]{figures/plantcam.jpg}}
\hfill
\subfloat[A HOBO$\textsuperscript{\textregistered}$ data logger for air and soil data.]{\label{fig:logger}}{\includegraphics[width=3in]{figures/logger.jpg}}
\caption{Automatic data collection instruments.}
\label{fig:autodata}
\end{center}
\end{figure}

\end{document}

Any assistance will be very much appreciated!

Best Answer

Here are some suggestions to fix your current situation:

  • Note the subfloat notation:

    \subfloat[<LoF>][<subcaption>]{<body>}
    

    Since your <body> consisted only of a \label command, the <subcaption> (actually, the <LoF> entry) had to wrap around something that is "very thin", causing it to not typeset properly. See page 4 of the subfig documentation for an explanation of this.

  • Also, the \label should be included in the <body>.

  • If you put the entire \subfloat within a fixed-width environment, the caption will wrap according to that width. This can be achieved via a minipage environment, for example. In the following minimal example, I've defined two new commands:

    \vertfig[<subcaption>]{<body>}% For vertical figure placement
    \horizfig[<subcaption>]{<body>}% For horizontal figure placement
    

    that you can use to replace the traditional \subfloat. \vertfig typesets its contents in a 5in-width minipage, while \horizfig does so in a 3in-width minipage. Both dropped the second optional argument of \subfloat[..][..]{...}, since it didn't seem to matter to you.

    As you can see, no fixed-width \captionsetup is required, since the captions wrap to the box they're contained in.

Different subfloats with different widths

\documentclass[11pt]{article}
\usepackage[a3paper]{geometry}% http://ctan.org/pkg/geometry
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
%\usepackage{caption}% http://ctan.org/pkg/caption
\usepackage{subfig}% http://ctan.org/pkg/subfig
\newsubfloat{figure}
\pagestyle{empty}% No header/footer
\newcommand{\vertfig}[2][]{%
  \begin{minipage}{5in}\subfloat[#1]{#2}\end{minipage}}
\newcommand{\horizfig}[2][]{%
  \begin{minipage}{3in}\subfloat[#1]{#2}\end{minipage}}
\begin{document}

\begin{figure}[ht]
  \centering
  \vertfig% \subfloat
    [This is probably the biggest tiger you will ever see in your life.
     The caption associated with this subfloat should span more than one
     line even though the width of the contents is 5in.]
    {\includegraphics[width=5in]{tiger}\label{fig:vizcardfront}}

  \vertfig% \subfloat
    [Data submission form on the back of the view guide.]
    {\includegraphics[width=5in]{tiger}\label{fig:vizcardback}}
  \caption{The visibility monitoring view guide.}\label{fig:viewcard}
\end{figure}

\begin{figure}[ht]
  \centering
  \horizfig%\subfloat
    [Here is another picture of the tiger, this time a little smaller.
     It is still very fierce-looking, right?]
    {\includegraphics[width=3in]{tiger}\label{fig:plantcam}}
  \hfill
  \horizfig%\subfloat
    [A HOBO$\textsuperscript{\textregistered}$ data logger for air and soil data.]
    {\includegraphics[width=3in]{tiger}\label{fig:logger}}
  \caption{Automatic data collection instruments.}\label{fig:autodata}
\end{figure}

\end{document}

geometry was merely used to set the paper size to a3paper.