# tcolorbox equation and fleqn: not vertically centered

Using tcolorbox with ams equation, there is sometimes additional leading space before the equation comes:

\documentclass[fleqn]{article}
\usepackage[many]{tcolorbox}
\usepackage{amsmath}

\begin{document}
\begin{tcolorbox}[ams equation]
A = B
\end{tcolorbox}
\end{document}


Versus

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{amsmath}

\begin{document}
\begin{tcolorbox}[ams equation]
A = B
\end{tcolorbox}
\end{document}


How can I remove it or have the equation centered vertically? The option fleqn seems to be significant here.

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. I took the liberty to slightly improve the tags and the formatting of your question, I hope you don't mind! – yo' Oct 13 '14 at 15:41

 \documentclass[fleqn]{article}
\usepackage[many]{tcolorbox}
\usepackage{amsmath}

\begin{document}
\begin{tcolorbox}[ams align]
A = B
\end{tcolorbox}
\end{document}


• what a shame ... we go to so much effort to try to tell people that using align for single-line displays is not the right thing to do. i think i'll try to persuade the author of tcolorbox that this is a bug. – barbara beeton Oct 13 '14 at 16:24
• @barbarabeeton I've added an answer with a patch for the fleqn case. I'm not sure if the different space settings by the amsmath package are deliberately done ... (?) – Thomas F. Sturm Oct 13 '14 at 16:38

The amsmath package seems to add extra vertical space for an equation only with the fleqn option. I'm not sure if that's a bug or a feature, but currently tcolorbox is not aware of the different space settings with or without fleqn.

The following patch seems to do the trick:

\makeatletter
\if@fleqn
\tcbset{%
ams equation upper/.style={before upper=\tcb@hack@amsmath\equation,after upper=\endequation},%
ams equation lower/.style={before lower=\tcb@hack@amsmath\equation,after lower=\endequation},%
ams equation* upper/.style={before upper=\tcb@hack@amsmath\csname equation*\endcsname,after upper=\endequation},%
ams equation* lower/.style={before lower=\tcb@hack@amsmath\csname equation*\endcsname,after lower=\endequation}%
}
\fi
\makeatother


All together, this gives:

\documentclass[fleqn]{article}
%\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{amsmath}

\makeatletter
\if@fleqn
\tcbset{%
ams equation upper/.style={before upper=\tcb@hack@amsmath\equation,after upper=\endequation},%
ams equation lower/.style={before lower=\tcb@hack@amsmath\equation,after lower=\endequation},%
ams equation* upper/.style={before upper=\tcb@hack@amsmath\csname equation*\endcsname,after upper=\endequation},%
ams equation* lower/.style={before lower=\tcb@hack@amsmath\csname equation*\endcsname,after lower=\endequation}%
}
\fi
\makeatother

\begin{document}
abc
$$A = B$$
\begin{tcolorbox}[ams equation]$$A = B$$\end{tcolorbox}
\begin{tcolorbox}[ams align]$$A = B$$\end{tcolorbox}
\end{document}


This code works with and without fleqn.

• thanks very much. i will look into the anomaly you report; without investigation, i think it may well have to do with the assumption that for use with ams document classes, the equation number is assumed to be on the left, not the right. i'll get back to you, off-line. – barbara beeton Oct 13 '14 at 18:38