当mdframed
环境以 结尾时align
,底部的垂直间距过大。我想调整环境MyBoxedEnvironment
,使底部的垂直空间保持均匀,与以文本或align
环境结尾的框的内容无关。
所有病例(#1和#3在 MWE 中,我不喜欢过多的间距,这些间距用红色表示。其他间距(未显示在包含的图像中)用黄色表示,我觉得还可以(参见输出的第 2 页)。
笔记:
为了避免多个嵌套框,
MyBoxedEnvironment
应用了以下解决方案调整嵌套时环境的行为。为了确保盒子始终是满的,
\linwidth
内容被包裹在里面minipage
。这实际上只需要在第一个测试用例中修复这个问题。请注意,在所包含图像的第一部分(其中环境未嵌套)中,底部的垂直间距是正常的。在确定
minipage
解决方案之前,我尝试使用,\hfill
但结果导致第一个框中的垂直空间过多。
问题:
- 如何在环境内进行调整,而无需手动对来自外部文件
mdframed
的框内容进行垂直调整?\input
- 为什么案例五如果删除“案例 5”文本,情况会不会有轻微变化?这并不困扰我,只是对原因感到好奇。
更新:2012-03-12:
MWE 代码已更新,以简化与原始和 @DavidCarlisle 当前提出的解决方案的比较。此解决方案非常适合案例 1 和案例 3(我们以显示数学环境结束)。但对于案例 2、4 至 7 存在问题,其中环境没有以显示数学结束并吞噬了最后一行。但是,在采纳了评论中提到的建议后,最后一行不再被吞噬。所以,现在唯一剩下的问题是顶部的额外垂直空间
这是案例 3 的图像,用于显示问题。MyBoxedEnvironmentOld
左侧显示原始输出 (via),MyBoxedEnvironmentNew
右侧显示新输出 (via)。请注意,显示数学环境的顶部似乎有额外的垂直空间。
因此,我尝试应用 页面/小页面顶部的对齐环境上方存在虚假空间通过取消注释所包含的代码,我们得到:
代码:(更新于 2012-02-13)
\documentclass{article}
\usepackage[paperheight=25.0in]{geometry}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{mdframed}
%%%% This adjustment from the URL below has no effect here.
%%%% https://tex.stackexchange.com/questions/36954/spurious-space-above-align-environment-at-top-of-page-minipage
%%
%\usepackage{etoolbox}
%\makeatletter
%\pretocmd\start@align{%
% \if@minipage\kern-\topskip\kern-\abovedisplayskip\fi
%}{}{}
%\makeatother
\colorlet{MyBackgroundColor}{yellow!10}
\newmdenv[
backgroundcolor=MyBackgroundColor,
linewidth=1.5pt,
skipabove=0pt,
skipbelow=0pt,
innerleftmargin=1pt,
nobreak=true,
]{MyBoxedEnvironmentMdframed}
% Should be no harm with trailing % here.
\newenvironment{MyBoxedEnvironmentOld}{%
\def\MyBoxedEnvironmentOld{%
\let\endMyBoxedEnvironmentOld\empty%
\par\noindent\ignorespaces%
}%
\begin{MyBoxedEnvironmentMdframed}%
\begin{minipage}{\linewidth}% to fix very first test case
\par\noindent\ignorespaces%
}{%
%\hfill% ensure full line width of first test case {minipage version is better}.
\end{minipage}%
\end{MyBoxedEnvironmentMdframed}%
\ignorespacesafterend%
}%
\makeatletter%
\newenvironment{MyBoxedEnvironmentNew}{%
\def\MyBoxedEnvironmentNew{%
\let\endMyBoxedEnvironmentNew\par%
\par%\noindent\ignorespaces%
}%
\begin{MyBoxedEnvironmentMdframed}%
\begin{minipage}{\linewidth}% to fix very first test case
\par%\noindent\ignorespaces%
}{%
\par%
\setbox0=\lastbox%
\ifnum\ht\z@=\z@%
\ifnum\dp\z@=\z@%
\unskip\unskip\unpenalty%
% \else%
% \box\z@%
% \else%
% \box\z@%
% \fi%
%\fi%
\fi\fi\box\z@% Apply fix mentioned in comments
\end{minipage}%
\end{MyBoxedEnvironmentMdframed}%
\ignorespacesafterend%
}%
\makeatother%
% Simplify repeated testing and comparrison: Run MyBoxedEnvironmentOld and MyBoxedEnvironmentNew side by side
\newcommand{\TestNestedEnvironments}[2][yellow!10]{%
\colorlet{MyBackgroundColor}{#1}%
\noindent%
\begin{minipage}[t]{0.49\linewidth}
\begin{MyBoxedEnvironmentOld}% non-nested test
#2%
\end{MyBoxedEnvironmentOld}%
%
\begin{MyBoxedEnvironmentOld}% nested test
#2%
\begin{MyBoxedEnvironmentOld}%
\textcolor{red}{#2}%
\end{MyBoxedEnvironmentOld}%
\end{MyBoxedEnvironmentOld}%
\end{minipage}%
\hfill%
\begin{minipage}[t]{0.49\linewidth}
\begin{MyBoxedEnvironmentNew}% non-nested test
#2%
\end{MyBoxedEnvironmentNew}%
%
\begin{MyBoxedEnvironmentNew}% nested test
#2%
\begin{MyBoxedEnvironmentNew}%
\textcolor{red}{#2}%
\end{MyBoxedEnvironmentNew}%
\end{MyBoxedEnvironmentNew}%
\end{minipage}%
\bigskip%
}%
\begin{document}
\noindent%
Problem only with \textbf{MyBoxedEnvironment} with display math and no text afterwards:
\TestNestedEnvironments[red!20]{
\begin{align*}
e = m c^2 \quad (Case~1)
\end{align*}
}
%----------------------------------------------------------
\TestNestedEnvironments{
\begin{align*}
e = m c^2 \quad (Case~2)
\end{align*}
Ok with text below (short or long).
}
%----------------------------------------------------------
\TestNestedEnvironments[red!20]{
Text only above:
\begin{align*}
e = m c^2 \quad (Case~3)
\end{align*}
}
%----------------------------------------------------------
%\newpage% Don't need since paper height is now extended.
\noindent
Ok with display math and text above and below:
\TestNestedEnvironments{
Text above (and below):
\begin{align*}
e = m c^2 \quad (Case~4)
\end{align*}
Text below (and above).
}
%----------------------------------------------------------
\bigskip\noindent
Ok with inline math (but slight change in spacing if the "(Case~5)" label is removed):
\TestNestedEnvironments{$e = m c^2 \quad (Case~5)$}
%----------------------------------------------------------
\bigskip\noindent
Ok with short text:
\TestNestedEnvironments{Lorem ipsum dolor sit amet.\quad (Case~6)}
%----------------------------------------------------------
\bigskip\noindent
Ok with long text:
\TestNestedEnvironments{
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed accumsan nulla ac ipsum elementum interdum.
Praesent ultricies faucibus turpis, non scelerisque nisi bibendum et.
\quad (Case~7)
}
\end{document}
答案1
我认为这是一个“color[u]r 有时会弄乱间距的问题。很久以前,我在原始 grfguide 中写道:
所有驾驶员都会遇到不同类型的问题。由于某些技术困难,在颜色变化的地方,间距可能会受到影响。
额外的空白是
........\penalty 10000
........\glue 0.0
........\penalty 0
........\glue(\belowdisplayskip) 10.0 plus 2.0 minus 5.0
........\glue(\baselineskip) 8.39996
........\hbox(0.0+0.0)x331.0, glue set 331.0fil
.........\pdfcolorstack 0 pop
.........\penalty 10000
.........\glue(\parfillskip) 0.0 plus 1.0fil
.........\glue(\rightskip) 0.0
也就是说,它是一个带有空白行的段落,其中仅包含彩色内容和一些填充胶。
您不必太担心哪个包裹在做这样的事情,您只需检测一个零高度的盒子并移除东西,即可产生:
我还\noindent
从环境的开始处删除了,因为如在另一个线程中所述,它们很可能在开始时导致类似的空白行(\noindent
开始一个段落,因此如果它后面是垂直材料,它会形成一个带有空段落的空白行)。
\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{mdframed}
\showboxdepth30
\showboxbreadth10000
%%% This adjustment from the URL below has no effect here.
%%% http://tex.stackexchange.com/questions/36954/spurious-space-above-align-environment-at-top-of-page-minipage
%
%\usepackage{etoolbox}
%\makeatletter
%\pretocmd\start@align{%
% \if@minipage\kern-\topskip\kern-\abovedisplayskip\fi
%}{}{}
%\makeatother
\colorlet{MyBackgroundColor}{yellow!10}
\newmdenv[
backgroundcolor=MyBackgroundColor,
linewidth=1.5pt,
skipabove=0pt,
skipbelow=0pt,
innerleftmargin=1pt,
nobreak=true,
]{MyBoxedEnvironmentMdframed}
% Should be no harm with trailing % here.
\makeatletter
\newenvironment{MyBoxedEnvironment}{%
\def\MyBoxedEnvironment{%
\let\endMyBoxedEnvironment\par%
\par%\noindent\ignorespaces%
}%
\begin{MyBoxedEnvironmentMdframed}%
\begin{minipage}{\linewidth}% to fix very first test case
%\setlength{\belowdisplayskip}{0pt}% to see effects of manual adjustments
%\setlength{\belowdisplayshortskip}{0pt}%
\par%\noindent\ignorespaces%
}{%
%\hfill% ensure full line width of first test case {minipage version is better}.
\par
\setbox0=\lastbox
\ifnum\ht\z@=\z@
\ifnum\dp\z@=\z@
\unskip\unskip\unpenalty
\else
\box\z@
\else
\box\z@
\fi
\fi
\end{minipage}%
\end{MyBoxedEnvironmentMdframed}%
\ignorespacesafterend%
}%
% Simplify repeated testing
\newcommand{\TestNestedEnvironments}[2][yellow!10]{%
\colorlet{MyBackgroundColor}{#1}%
\begin{MyBoxedEnvironment}% non-nested test
#2%
\end{MyBoxedEnvironment}%
%
\begin{MyBoxedEnvironment}% nested test
#2%
\begin{MyBoxedEnvironment}%
\textcolor{red}{#2}%
\end{MyBoxedEnvironment}%
\end{MyBoxedEnvironment}%
}%
\begin{document}
\noindent
Problem only with \textbf{MyBoxedEnvironment} with display math and no text afterwards:
\TestNestedEnvironments[red!20]{
\begin{align*}
e = m c^2 \quad (Case~1)
\end{align*}
}
%----------------------------------------------------------
\TestNestedEnvironments{
\begin{align*}
e = m c^2 \quad (Case~2)
\end{align*}
Ok with text below (short or long).
}
%----------------------------------------------------------
\TestNestedEnvironments[red!20]{
Text only above:
\begin{align*}
e = m c^2 \quad (Case~3)
\end{align*}
}
%----------------------------------------------------------
\newpage\noindent
Ok with display math and text above and below:
\TestNestedEnvironments{
Text above (and below):
\begin{align*}
e = m c^2 \quad (Case~4)
\end{align*}
Text below (and above).
}
%----------------------------------------------------------
\bigskip\noindent
Ok with inline math (but slight change in spacing if the "(Case~5)" label is removed):
\TestNestedEnvironments{$e = m c^2 \quad (Case~5)$}
%----------------------------------------------------------
\bigskip\noindent
Ok with short text:
\TestNestedEnvironments{Lorem ipsum dolor sit amet.\quad (Case~6)}
%----------------------------------------------------------
\bigskip\noindent
Ok with long text:
\TestNestedEnvironments{
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed accumsan nulla ac ipsum elementum interdum.
Praesent ultricies faucibus turpis, non scelerisque nisi bibendum et.
\quad (Case~7)
}
\end{document}
答案2
将此问题归咎于 是不公平的align
。同样的事情也会发生在equation
和 没有 的情况下amsmath
。在提供的代码中,注释掉\usepackage{amsmath}
的所有实例并用 替换align*
(equation
抱歉,没有*
可用,因此您将获得显示数字),您将得到以下结果:
\[ ... \]
仅使用(如预期的那样)而不使用数字就可以获得相同的结果。
这仍然是一个问题,但却是 (la)tex 更深层次的问题。