我正在使用 tcolorbox 包来处理一些经常包含方程式的框。如果方程式或对齐环境之前有一些文本,并且这些文本足够长,可以强制将方程式稍微向下设置,则间距没有问题。两个框的高度相同(见第一张图片)。
但是如果上面的文本不够多,或者根本没有文本,则公式环境会稍微靠上一点,这样就恰到好处了。但是对齐环境不会这样,因为它上面的空间太多了。这就是为什么两个框的高度不同(见第二张图片)。
这是我的 MWE,它创建了图片中显示的框:
\documentclass[ngerman,a4paper,12pt]{scrbook}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\definecolor{maple-hblau}{RGB}{58,126,171}
\colorlet{colexample}{maple-hblau!75}
\newtcolorbox{examplebox}{%
empty,title={Example},attach boxed title to top left,
boxed title style={empty,size=minimal,toprule=2pt,top=4pt,overlay={\draw[colexample,line width=2pt] ([yshift=-1pt]frame.north west)--([yshift=-1pt]frame.north east);}},
coltitle=colexample,fonttitle=\footnotesize\bfseries,
before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=0pt,right=3mm,top=4pt,
overlay={\draw[colexample,line width=1pt] ([yshift=-1pt]title.north east)--([xshift=-0.5pt,yshift=-1pt]title.north-|frame.east)--([xshift=-0.5pt]frame.south east)--(frame.south west); },
}
\begin{document}
\twocolumn
% long text prior to the align-environment -> no problem
\begin{examplebox}
Align*-Environment:
\begin{align*}
\frac{5}{2} = \frac{15}{6}
\end{align*}
\end{examplebox}
\newpage
% long text prior to the equation-environment, for comparison
\begin{examplebox}
Equation*-Environment:
\begin{equation*}
\frac{21}{9} = \frac{7}{3}
\end{equation*}
\end{examplebox}
\newpage
% not enough text prior to the align-environment -> too much white space above align
\begin{examplebox}
Align*:
\begin{align*}
\frac{5}{2} = \frac{15}{6}
\end{align*}
\end{examplebox}
\newpage
% the same amount of text prior to the equation-environment, for comparison
\begin{examplebox}
Equation*:
\begin{equation*}
\frac{21}{9} = \frac{7}{3}
\end{equation*}
\end{examplebox}
\end{document}
对齐环境表现出这样的行为有什么原因吗?或者这是一个错误?
更重要的问题是:我该如何改变对齐的行为,以便对齐环境和其上方仅有少量文本的框与旁边带有方程式环境的框一样大,因此对齐环境上方的空间较小?最好是全局定义此行为,这样我就不必每次出现对齐环境和其上方很少的文本时都插入一些命令。
答案1
这是一个已知问题:align
不使用捷径。
解决方案是加载nccmath
包(部分ncctools
\useshortskip
),并在之前使用命令align
:
\documentclass[ngerman,a4paper,12pt]{scrbook}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb}
\usepackage{nccmath}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\definecolor{maple-hblau}{RGB}{58,126,171}
\colorlet{colexample}{maple-hblau!75}
\newtcolorbox{examplebox}{%
empty,title={Example},attach boxed title to top left,
boxed title style={empty,size=minimal,toprule=2pt,top=4pt,overlay={\draw[colexample,line width=2pt] ([yshift=-1pt]frame.north west)--([yshift=-1pt]frame.north east);}},
coltitle=colexample,fonttitle=\footnotesize\bfseries,
before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=0pt,right=3mm,top=4pt,
overlay={\draw[colexample,line width=1pt] ([yshift=-1pt]title.north east)--([xshift=-0.5pt,yshift=-1pt]title.north-|frame.east)--([xshift=-0.5pt]frame.south east)--(frame.south west); },
}
\begin{document}
\twocolumn
% long text prior to the align-environment -> no problem
\begin{examplebox}
Align*-Environment:%\useshortskip
\begin{align*}
\frac{5}{2} = \frac{15}{6}
\end{align*}
\end{examplebox}
\newpage
% long text prior to the equation-environment, for comparison
\begin{examplebox}
Equation*-Environment:
\begin{equation*}
\frac{21}{9} = \frac{7}{3}
\end{equation*}
\end{examplebox}
\newpage
% not enough text prior to the align-environment -> too much white space above align
\begin{examplebox}
Align*:\useshortskip
\begin{align*}
\frac{5}{2} = \frac{15}{6}
\end{align*}
\end{examplebox}
\newpage
% the same amount of text prior to the equation-environment, for comparison
\begin{examplebox}
Equation*:
\begin{equation*}
\frac{21}{9} = \frac{7}{3}
\end{equation*}
\end{examplebox}
\end{document}