I want to create a caption for an equation, made using align environment. Founded this post (9 years old): Caption for align environment, where someone's posted this solution:
\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~\thecaptionedequationset: #1
\end{center}}
{ \begin{flushleft}
Equation~\thecaptionedequationset: #1 %
\end{flushleft}}}
The problems that I have encountered are:
- Caption doesn't align with the equation. If a page breaker occurs (like \clearpage or normal page jump), it gets separated from the equation.
- Counter doesn't follow chapter number.
Here is a “minimal” MWE:
\documentclass[12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, lipsum}
\usepackage{calc}
\usepackage{ifthen}
% - - - - - C O M A N D O S - - - - -
% Para poder escribir Caption en ecuaciones matemáticas:
%-- 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~\thecaptionedequationset: #1
\end{center}}
{ \begin{flushleft}
Equation~\thecaptionedequationset: #1 %
\end{flushleft}}}
% - - - - - PORTADA - - - - -
\begin{document}
\chapter{}
\section{Section 1}
The Equation \ref{eq:1} shows an equation.
\begin{align}
\begin{split}
\text{Eq 1: }&\
& x = 0
\end{split}
\end{align}
\captionedequationset{Nice equation.\label{eq:1}}
The Equation \ref{eq:2} shows another equation.
\begin{align}
\begin{split}
\text{Eq 2: }&\
& x^2 = 0
\end{split}
\end{align}
\captionedequationset{Another nice equation.\label{eq:2}}
\section{Section 2}
The Equation \ref{eq:3} shows another equation.
\begin{align}
\begin{split}
\text{Eq 2: }&\
& x^2 + x = 0
\end{split}
\end{align}
\clearpage
\captionedequationset{Another nice equation.\label{eq:3}}
\end{document}
And the results are (ignores the 2 and CHAPTER 1 that appears):
Is there a better way to get this done? Or, there's any way to upgrade this code and solve the problems?
