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 am trying to use subcaption with beamer, but I get a series of error messages concerning subcaption.sty when I attempt to do so. This doesn't happen in the article document class. Here is a MWE

\documentclass{beamer}

\usepackage{caption}
\usepackage{subcaption}

\begin{document}
\begin{frame}
\begin{figure}
  \begin{subfigure}[b]{.45\linewidth}
    \centering
    \caption{First subfigure}
    \label{fig:a}
    \textcolor{blue}{\rule{3cm}{3cm}}
  \end{subfigure}\hfill
  \begin{subfigure}[b]{.45\linewidth}
    \centering
    \caption{Second subfigure}
    \label{fig:b}
    \textcolor{blue}{\rule{3cm}{3cm}}
  \end{subfigure}\    \caption{A figure}\label{fig:1}
\end{figure}
\end{frame}
\end{document}
Package caption Warning: \caption will not be redefined since it's already
(caption)                redefined by a document class or package which is
(caption)                unknown to the caption package.
See the caption package documentation for explanation.


! Package caption Error: The `subcaption' package does not work correctly
(caption)                in compatibility mode.

See the caption package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.7 \begin{document}

? 
! Emergency stop.
 ...                                              

l.7 \begin{document}

Any tips?

share|improve this question
    
In general don't use floats with figures because they can't shift to other slides (would be awkward anyway) and you also don't need captions as nobody needs to know the number of the figure. Mention somewhere in the frame and that would be much more sensible. –  percusse Jul 26 '13 at 16:11
    
@percusse No; beamer internally deactivates the floating mechanism, so figure and table don't produce floating objects; one can still use those environment in case one wants to use \caption. –  Gonzalo Medina Jul 26 '13 at 18:34
    
@GonzaloMedina Oh I didn't know that. –  percusse Jul 26 '13 at 19:12
    
Your example document works fine here (TeXlive 2013). What error messages are you getting? Is updating your TeX distribution an option? –  Axel Sommerfeldt Jul 27 '13 at 5:37

2 Answers 2

up vote 3 down vote accepted

Edit: I tried to remove this answer since it obviously does not solve the question at hand. However, since this was accepted once, it is not possible. Please refer to the other answer. (I'll leave the answer for consistency.)

Just remove the unnecessary floats and use \captionof{(sub)figure}{<capt>} instead.

\documentclass{beamer}

\usepackage{caption}
\usepackage{subcaption}

\begin{document}
\begin{frame}
  \begin{minipage}[b]{.45\linewidth}
    \centering
    \captionof{subfigure}{First subfigure}
    \label{fig:a}
    \textcolor{blue}{\rule{3cm}{3cm}}
  \end{minipage}\hfill
  \begin{minipage}[b]{.45\linewidth}
    \centering
    \captionof{subfigure}{Second subfigure}
    \label{fig:b}
    \textcolor{blue}{\rule{3cm}{3cm}}
  \end{minipage}\    \captionof{figure}{A figure}\label{fig:1}
\end{frame}
\end{document}

I can understand someone would like to do that, for a List of... or handout. But generally it is much simpler coding it with style commands.

share|improve this answer
    
But this code produces errors (at least on TeX Live 2013 and caption.sty 2013/05/02 v3.3-89 Customizing captions (AR) caption3.sty 2013/05/02 v1.6-88 caption3 kernel (AR) subcaption.sty 2013/02/03 v1.1-62 Sub-captions (AR)). –  Gonzalo Medina Apr 3 at 2:47
    
In fact, this code produces the exact same error mentioned in the original question. –  Gonzalo Medina Apr 3 at 3:17
    
Additionally, figure and table are not floating environments in beamer; please refer to Section 12.6 Figures and Tables of the beamer manual. –  Gonzalo Medina Apr 3 at 3:24
    
I just noticed that myself. When I answered the question, the error description was not there. However, the MWE worked fine on my system. –  Martin Apr 3 at 4:55

The error message is produced since when subcaption gets loaded the compatibility boolean option for caption is found to be true, and subcaption.sty contains the lines:

\caption@AtBeginDocument{\caption@ifcompatibility{%
  \caption@Error{%
    The `subcaption' package does not work correctly\MessageBreak
    in compatibility mode}}{}}

which trigger the error. You can prevent this error by using the compatibility=false option for caption (and keep your fingers crossed (see below)), as in the following exmaple:

\documentclass{beamer}
\usepackage[compatibility=false]{caption}
\usepackage{subcaption}

\begin{document}
\begin{frame}
\begin{figure}
  \begin{subfigure}[b]{.45\linewidth}
    \centering
    \caption{First subfigure}
    \label{fig:a}
    \textcolor{blue}{\rule{3cm}{3cm}}
  \end{subfigure}\hfill
  \begin{subfigure}[b]{.45\linewidth}
    \centering
    \caption{Second subfigure}
    \label{fig:b}
    \textcolor{blue}{\rule{3cm}{3cm}}
  \end{subfigure}\    \caption{A figure}\label{fig:1}
\end{figure}
\end{frame}
\end{document}

However, this might produce undesired results in beamer's captions; in fact, with the above document you get the warning

Package caption Warning: Forced redefinition of \caption since the (caption) unsupported(!) package option `compatibility=false' (caption) was given. See the caption package documentation for explanation.

The caption package documentation also warns against using this option:

But please note that using this option is neither recommended nor supported since unwanted side-effects or even errors could occur afterwards. (For that reason you will get a warning about this.)

If captions and subcaptions are really required for a presentation, I think a better option with beamer is to use subfig with its caption=false option instead of caption/subcaption:

\documentclass{beamer}
\usepackage[caption=false]{subfig}

\begin{document}

\begin{frame}
\begin{figure}
  \centering
  \subfloat[First subfigure\label{fig:a}]{\textcolor{blue}{\rule{3cm}{3cm}}}\quad
  \subfloat[Second subfigure\label{fig:b}]{\textcolor{blue}{\rule{3cm}{3cm}}}
  \caption{A figure}
  \label{fig:1}
\end{figure}
\end{frame}

\end{document}

enter image description here

Remark

It's a very common misconception to believe that the figure and table environments shouldn't be used in beamer (the alleged reason is something like "they might produce objects that will float away"). This is not true; beamer internally deactivates flotation, so it is perfectly safe to use figure and table if you really need to provide captions in a presentation.

Section 12.6 Figures and Tables of the beamer manual clearly explains this:

You can use the standard LaTeX environments figure and table much the same way you would normally use them. However, any placement specification will be ignored. Figures and tables are immediately inserted where the environments start. If there are too many of them to fit on the frame, you must manually split them among additional frames or use the allowframebreaks option.

share|improve this answer
    
Isn't a floating environment within a beamer frame a Latex sin? –  pluton Apr 3 at 3:14
1  
@pluton No; beamer internally deactivates flotation. It keeps the figure and table environments (but, as I said, suppressing the flotation) just to give the possibility to use \caption. Another thing is if you really need captions in your presentations. It is a common mistake to believe that figure (or table) inside beamer might produce flotation to a different slide. –  Gonzalo Medina Apr 3 at 3:16
1  
@pluton Please read Section 12.6 Figures and Tables of the beamer manual. –  Gonzalo Medina Apr 3 at 3:20

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.