[Tex/LaTex] Error using subcaption

captionssubfloats

I've used the subcaption package in the past without any problem. But now, i can't use it, which makes it impossible to compile some important documents.

If i try to compile

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage[skip=0cm,list=true,labelfont=it]{subcaption}
\begin{document}
\begin{figure}[hptb]
\centering
\begin{tabular}{l l}
\subfloat[\label{fig:g1} Figura 1]
   {\resizebox{0.45\textwidth}{!}{\includegraphics{img_a}}} &
\subfloat[\label{fig:g2} Figura 2]
   {\resizebox{0.45\textwidth}{!}{\includegraphics{img_a}}} \\
\end{tabular}
\caption{Figure caption.}
\end{figure}
\end{document}

i get the error:

! Undefined control sequence.
l.14 \subfloat
[\label{fig:g1} Figura 1]

From the file log:

This is pdfTeX, Version 3.1415926-2.4-1.40.13 (TeX Live 2012/Debian) (format=latex 2012.9.18)
Package: caption 2012/02/19 v3.2f Customizing captions (AR)
Package: caption3 2012/01/12 v1.4b caption3 kernel (AR)
Package: subcaption 2012/01/12 v1.1d Sub-captions (AR)

Any idea on what the problem might be?

Best Answer

I think there's a bit of confusion here. The subcaption defines the subfigure environment, but the \subfloat command comes from the subfig package.

Personally, I'd recommend sticking with the subcaption package, and using a set up like the following for your MWE

screenshot

MWE

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage[skip=0cm,list=true,labelfont=it]{subcaption}
\begin{document}
\begin{figure}[hptb]
\begin{subfigure}{.45\textwidth}
   \includegraphics[width=\textwidth]{img_a}
   \caption{First subfigure}
\label{fig:g1}
\end{subfigure}%
\hfill
\begin{subfigure}{.45\textwidth}
   \includegraphics[width=\textwidth]{img_a}
   \caption{Second subfigure}
\label{fig:g2}
\end{subfigure}
\caption{Figure caption.}
\end{figure}
\end{document}

Note that the subfigure environment takes a mandatory <width> argument, which I have specified as .45\textwidth (what you used in your MNWE). The subfigure environment can also take the same optional arguments that the minipage argument can take.

This now means that the resizebox command for your images should be used with \textwidth which will match the width of the subfigure environment.

Using this approach also means that you can remove your tabular approach.