1

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}
\begin{equation} 1+2=3 \end{equation}
used with theorem \ref{theorem}
\begin{myframe}[float,floatplacement=t]
This is the theorem:
\begin{equation} 1+1=2. \label{theorem} \end{equation}
\end{myframe}
gives
\begin{equation} 2+2=4. \end{equation}
\end{document}

MWE output

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.

  • 3
    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. – Ignasi 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. – Tom-Tom 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. – John Kormylo Jan 18 '16 at 17:51
3

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}
\begin{equation} 1+2=3 \mytag \end{equation}
used with theorem \ref{theorem}
\begin{myframe}[float,floatplacement=t]
This is the theorem:
\begin{equation} 1+1=2. \mytag\label{theorem} \end{equation}
\end{myframe}
gives
\begin{equation} 2+2=4. \mytag \end{equation}
\end{document}

renumbered equation

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

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:
\begin{equation} 1+1=2. \label{theorem} \end{equation}
\end{myframe}
\begin{equation} 1+2=3 \end{equation}
used with theorem \ref{theorem}
gives
\begin{equation} 2+2=4. \end{equation}
\end{document}

number

  • 1
    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... – Tom-Tom 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... – Ktree Jan 18 '16 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.