9

I would like to add a caption under a set of equations that I have in the align environment (i.e., \begin{align} ... \end{align}). I tried \caption{...} inside the align environment, but it didn't work.

Javad
  • 763
  • 2
  • 7
  • 8
  • Typically a \caption is associated with a float, which has an accompanying counter representation. For example Figure 1: or Table 2:. What would you want in this case for your align environment? Or do you not want any of that? – Werner Apr 20 '14 at 04:42
  • I would like to get something like Figure 1: for my align environment. – Javad Apr 20 '14 at 04:45
  • Package caption or capt-of provide a \captionof{figure}{...}. – Heiko Oberdiek Apr 20 '14 at 04:57

2 Answers2

7

You can force a caption outside a regular float using the caption package or the tiny capt-of package, each providing \captionof{<float>} which tricks LaTeX into setting a float-like caption:

enter image description here

\documentclass{article}
\usepackage{amsmath,capt-of,lipsum}
\begin{document}
\lipsum*[1]
\begin{align}
  f(x) &= ax^2 + bx + c \\
       &= g(x)
\end{align}

\begingroup\vspace*{-\baselineskip}
\captionof{figure}{A beautiful equation.}
\vspace*{\baselineskip}\endgroup

\lipsum*[2]
\end{document}

For a custom caption, I'd suggest creating a macro to reference the set using something like the following:

enter image description here

\documentclass{article}
\usepackage{amsmath,lipsum}
\newcounter{equationset}
\newcommand{\equationset}[1]{% \equationset{<caption>}
  \refstepcounter{equationset}% Step counter
  \noindent\makebox[\linewidth]{Equation set~\theequationset: #1}}% Print caption
\begin{document}
\lipsum*[1]
\begin{align}
  f(x) &= ax^2 + bx + c \\
       &= g(x)
\end{align}
\equationset{A beautiful equation.}

\medskip

\lipsum*[2]
\end{document}
Werner
  • 603,163
  • Thanks. How can I have a customized caption for my align environment. E.g., Equation set 1: instead of Figure 1:. – Javad Apr 20 '14 at 05:24
  • 2
    The use of \captionof without a proper container (such as center, minipage or similar) can produce unexpected results; I can't find now the question in this site illustrating this in a spectacular way (the problem appeared several pages after \captionof had been used without container!). – Gonzalo Medina Apr 20 '14 at 05:26
  • @GonzaloMedina: I know that \captionof without container allows you to use \caption as-is afterwards, since \@captype has been set - the only test used to see whether \caption is valid. – Werner Apr 20 '14 at 05:28
  • @Javad: Do you have a bunch of these "equation sets"? I've added something to manage this. Perhaps it's sufficient. – Werner Apr 20 '14 at 05:29
  • @Werner but that's not what I meant; using no container can produce odd results (not errors, but certainly problems). I wish I could find the relevant thread :( – Gonzalo Medina Apr 20 '14 at 05:34
  • @TorbjørnT.: \captionof sets \@captype. It means that you can use \captionof{table}{..} in one place, and then somewhere else \caption (outside of a float, but in the same group) and still yield a table caption. – Werner Jul 22 '15 at 21:49
  • @TorbjørnT. Those can be used as examples too, but no. The one I have in mind is more "spectacular". The problem manifested after several pages. – Gonzalo Medina Jul 22 '15 at 21:50
  • @Werner I know, you said that already, Gonzalo was looking for something else. – Torbjørn T. Jul 22 '15 at 21:51
  • @Werner - I used this method and got nice captions. Then I added corresponding \label{}'s. But where my caption might be Table 2., a reference to the label comes out as 2.6. How can I fix that? – CAB Mar 21 '17 at 20:14
  • 1
    @CAB: Are you using \equationset{Caption\label{abc}}? – Werner Mar 21 '17 at 21:07
  • @Werner, no, I had the \label on a separate line. It works fine now. Thanks! – CAB Mar 31 '17 at 19:33
2

A little tweak on Werner's answer. His answer has the problem of not allowing line break inside the caption. enter image description here

I have fixed the problem modifying his code.

\documentclass{article}
\usepackage{amsmath}
\usepackage{calc}
\usepackage{ifthen}

%-- Defines a CAPTION for a set of equations and numbers
%   the set of equations is numbered
% note: the caption adapts the text alignment to the length. 
\newcounter{captionedequationset} %numbering
\newdimen\captionlength
\newcommand{\captionedequationset}[1]{
    \refstepcounter{captionedequationset}% Step counter
    \setlength{\captionlength}{\widthof{#1}} %
    \addtolength{\captionlength}{\widthof{Equation set~\thecaptionedequationset: }}
    %If the caption is shorter than the line width then
    % the caption is centred, otherwise is flushed left.
    \ifthenelse{\lengthtest{\captionlength < \linewidth }} %
    {\begin{center}
            Equation set~\thecaptionedequationset: #1
        \end{center}} 
    { \begin{flushleft} 
        Equation set~\thecaptionedequationset: #1 %
        \end{flushleft}}}

\begin{document}

  \begin{gather}
      ax^2 +bx+c \\
      \sin(\omega x) + \cos(\omega x) 
  \end{gather}
  \captionedequationset{Lovely!} 

  \begin{gather}
      ax^2 +bx+c \\
      \sin(\omega x) + \cos(\omega x) 
  \end{gather}
  \captionedequationset{These are probably the cutest equations ever seen in \LaTeX---I really like them!---but somebody else may think differently. }

\end{document}

Enjoy your equations+captions. :) enter image description here