# Equation numbering using floating tcolorbox

There is an issue in the numbering of equation when using a floating tcolorbox. I would like the equations to be numbered in the order of appearance.

Here is a MWE

\documentclass{article}
\usepackage{tikz}
\usepackage{tcolorbox}
\newtcolorbox{myframe}[1][]{#1}
\begin{document}
$$1+2=3$$
used with theorem \ref{theorem}
\begin{myframe}[float,floatplacement=t]
This is the theorem:
$$1+1=2. \label{theorem}$$
\end{myframe}
gives
$$2+2=4.$$
\end{document}


Is there a way to number the equations correctly ? Thanks.

EDIT To be more precise, I want the numbering to be in the order of appearance, wherever the float is positioned.

• The problem is not tcolorbox but floating. You'll have same problem with a figure. The equation number is based in which order equations are declared in .tex file not based in where it is definitively placed in your document. Jan 18 '16 at 10:58
• I doubt that floating theorem boxes are useful. As Ignasi said: The number is fixed at the very moment the tcolorbox command is used, but LaTeX shifts this to another place.
– user31729
Jan 18 '16 at 11:29
• @ChristianHupfer. The MWE does not reflect my project. I actually am writing a lecture in physics and want to use boxes for technical mathematical details that would make the reading of the main text jerky. This is the reason I opted for floats. Jan 18 '16 at 12:27
• The order in which \newlabel (or any \protected@write) appears in the aux file is the same as the order they appear on the page. So it is possible, but not easy. Jan 18 '16 at 17:51

This uses the \tag command from amsmath. For this to work, you will need to add \mytag to every numbered equation. It will take two runs to relabel the equations, and 3 runs to get \ref right.

If you want the chapter number prefixed to the equation number, you will have to reformat the \tag inside \mytag.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{tcolorbox}
\newtcolorbox{myframe}[1][]{#1}

\newcounter{myequation}

\newcommand{\neweqmap}[1]% #1 = original euqation number
{\stepcounter{myequation}%
\expandafter\xdef\csname eqmap#1\endcsname{\themyequation}}

\makeatletter
\newcommand{\mytag}{\@ifundefined{eqmap\theequation}{}%
{\stepcounter{equation}%
\tag{\csname eqmap\theequation\endcsname}}%
\protected@write\@auxout{}{\string\neweqmap{\theequation}}}
\makeatother

\begin{document}
$$1+2=3 \mytag$$
used with theorem \ref{theorem}
\begin{myframe}[float,floatplacement=t]
This is the theorem:
$$1+1=2. \mytag\label{theorem}$$
\end{myframe}
gives
$$2+2=4. \mytag$$
\end{document}


• Very clever ! Thanks for this great answer @JohnKormylo ! Jan 18 '16 at 21:06

Just change the order of appearance:

\documentclass{article}
\usepackage{tikz}
\usepackage{tcolorbox}
\newtcolorbox{myframe}[1][]{#1}
\begin{document}
\begin{myframe}[float,floatplacement=t]
This is the theorem:
$$1+1=2. \label{theorem}$$
\end{myframe}
$$1+2=3$$
used with theorem \ref{theorem}
gives
$$2+2=4.$$
\end{document}


• In a document with several pages and floats, the positionning of the float is not known, and the colorbox could be put to the next page. In this case this would still give the number (1) to the theorem but it would appear on the next page... Jan 18 '16 at 10:38
• So you want the order of numbering to be changed by the placements of the floats. Sorry, I don't know how to solve that and dout if it is even possible... Jan 18 '16 at 10:57