tcolorboxes 和 align

tcolorboxes 和 align

在使用 tcolorboxes 时,我发现了一些奇怪的事情。如果框的内容以 center-environment、minipage-environment 或简单的文本开头,则一切看起来都很好。但如果内容以 math align-environment 开头,则存在相当大的差距:

在此处输入图片描述

正常情况下它应该是这样的:

在此处输入图片描述

有人能帮我消除这个差距吗?我不知道为什么会有这个差距。这里有一个简单的代码示例,如果你想尝试一下:


\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

align*,作为垂直模式环境,需要在其自身和其之前的内容之间引入垂直空间。一个简单的解决方案是使用aligned,并将其放置在居中的内联数学中,如\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}

在此处输入图片描述

相关内容