Tcolorboxes and align

aligngaptcolorbox

While using tcolorboxes I found something strange. If the content of the box begins with the center-environment, minipage-environment or simply text, everything looks fine. But if the content begins with the math align-environment, there is a pretty big gap:

enter image description here

Normally it should look like this:

enter image description here

Can someone help me get rid of this gap? I have no idea, why this gap even exists. Here is a simple code example, if you want to play around with it:


\documentclass[a4paper, 11pt]{book}

\usepackage[most]{tcolorbox}

\tcbset{framedboxfilled/.style={  enhanced jigsaw,
        sharp corners,
        colback=white!80!gray,
        colbacktitle=white!80!gray,
        fonttitle={\bfseries},
        coltitle=black,
        toprule=1pt, titlerule=1pt, bottomrule=1pt,
        leftrule=0pt, rightrule=0pt, right=0pt,
        left=-2pt,
    }
}
\tcbset{framedboxoutline/.style={%  
        framedboxfilled,
        colback=white,
        colbacktitle=white,
    }
}
\def\myboxstyle{framedboxoutline}

\newtcolorbox[auto counter,number within=section]{beispiel}[2][]{%
    \myboxstyle,
    title={Beispiel \thetcbcounter~#2},
}
\begin{document}
\section{Test}
\begin{beispiel}{XOR-Operator}
    \begin{align*}
        &b1 && = && 1000\ 1000\ 1101\ 1001_2\\
        &b2 && = && 1111\ 0000\ 1010\ 1101_2\\
        &\text{result}\ b_1 \oplus b_2 && = && 0111\ 1000\ 0111\ 0100_2
    \end{align*}
\end{beispiel}

\begin{beispiel}{Schlüssel einer Substitutions Cipher}
    \begin{center}
    \begin{tabular}{p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}}
        a & b & c & d & e & f & g & h & i & j & k & l & m & n & o & p & q & r & s & t & u & v & w & x & y & z\\
        $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ &  $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ &  $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ &  $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ &  $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$  \\
        X &E &U &A &D &N &B &K &V &M &R &O &C &Q &F &S &Y &H &W &G &L &Z &I &J &P &T
    \end{tabular}   
    \end{center}    
\end{beispiel}

\end{document}

Best Answer

align*, being a vertical-mode environment, wants to introduce that vertical space between itself and what comes before it. A simple solution is to use aligned instead, and place it inside centered, inline math, as in \centering$\begin{aligned}...\end{aligned}$.

\documentclass[a4paper, 11pt]{book}

\usepackage[most]{tcolorbox}

\tcbset{framedboxfilled/.style={  enhanced jigsaw,
        sharp corners,
        colback=white!80!gray,
        colbacktitle=white!80!gray,
        fonttitle={\bfseries},
        coltitle=black,
        toprule=1pt, titlerule=1pt, bottomrule=1pt,
        leftrule=0pt, rightrule=0pt, right=0pt,
        left=-2pt,
    }
}
\tcbset{framedboxoutline/.style={%  
        framedboxfilled,
        colback=white,
        colbacktitle=white,
    }
}
\def\myboxstyle{framedboxoutline}

\newtcolorbox[auto counter,number within=section]{beispiel}[2][]{%
    \myboxstyle,
    title={Beispiel \thetcbcounter~#2},
}
\begin{document}
\section{Test}
\begin{beispiel}{XOR-Operator}
  \centering$
    \begin{aligned}
        &b1 && = && 1000\ 1000\ 1101\ 1001_2\\
        &b2 && = && 1111\ 0000\ 1010\ 1101_2\\
        &\text{result}\ b_1 \oplus b_2 && = && 0111\ 1000\ 0111\ 0100_2
    \end{aligned}$
\end{beispiel}

\begin{beispiel}{Schlüssel einer Substitutions Cipher}
    \begin{center}
    \begin{tabular}{p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}p{0pt}}
        a & b & c & d & e & f & g & h & i & j & k & l & m & n & o & p & q & r & s & t & u & v & w & x & y & z\\
        $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ &  $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ &  $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ &  $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ &  $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$ & $\downarrow$  \\
        X &E &U &A &D &N &B &K &V &M &R &O &C &Q &F &S &Y &H &W &G &L &Z &I &J &P &T
    \end{tabular}   
    \end{center}    
\end{beispiel}

\end{document}

enter image description here

Related Question