1

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}

1 Answer 1

2

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

3
  • Thank you very much! I'm just curious: Is there a way to redefine the align-Environment, so that something like that does not happen and the normal behaviour is not affected?
    – Titanlord
    Sep 29, 2021 at 10:22
  • 1
    @Titanlord I would have to know a lot more on how tcolorbox operates to answer that question. However, my suspicion is that the problem is not with align*, but with the way tcolorbox operates. On a normal page (outside of tcolorbox), \vspace at the top of a page does not manifest itself. My guess is the tcolorbox does not absorb initial \vspace in the same manner. Sep 29, 2021 at 11:05
  • That makes sense! Thank you very much!
    – Titanlord
    Sep 29, 2021 at 12:13

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.