问题
我想在mdframed
分割(分页符)后的环境中添加空间,并且分割(分页符)后的第一个对象是另一个mdframed
环境。可以吗?
图片与代码
\documentclass[a4paper]{article}
\usepackage{mdframed}
\begin{document}
\begin{mdframed}[
innerrightmargin = 0.5cm,
innertopmargin = 0.5cm,
skipbelow = 135mm,
backgroundcolor = red,
skipabove = 35mm,
linewidth = 0pt,
userdefinedwidth = 11cm,
innerbottommargin = 0.5cm,
splitbottomskip = 10mm,
splittopskip = 10mm,
roundcorner = 5pt,
innerleftmargin = 0.5cm,
align = center]
Vestibulum lectus metus, tincidunt at fermentum non, pellentesque at lorem. Vivamus nisl sem, tempor ac mi et, elementum feugiat justo. Pellentesque tristique consequat molestie. Donec non porttitor risus. Praesent adipiscing elementum tortor, commodo ullamcorper justo consequat eget. Aenean eget nulla enim. Nam purus massa, hendrerit quis faucibus eget, consectetur ac dui. Curabitur ornare non ipsum elementum suscipit. Sed a dignissim ligula, nec vestibulum dui. Fusce sed varius tellus.
Vestibulum lectus metus, tincidunt at fermentum non, pellentesque at lorem. Vivamus nisl sem, tempor ac mi et, elementum feugiat justo. Pellentesque tristique consequat molestie. Donec non porttitor risus. Praesent adipiscing elementum tortor, commodo ullamcorper justo consequat eget. Aenean eget nulla enim. Nam purus massa, hendrerit quis faucibus eget, consectetur ac dui. Curabitur ornare non ipsum elementum suscipit. Sed a dignissim ligula, nec vestibulum dui. Fusce sed varius tellus.
Vestibulum lectus metus, tincidunt at fermentum non, pellentesque at lorem. Vivamus nisl sem, tempor ac mi et, elementum feugiat justo. Pellentesque tristique consequat molestie. Donec non porttitor risus. Praesent adipiscing elementum tortor, commodo ullamcorper justo consequat eget. Aenean eget nulla enim. Nam purus massa, hendrerit quis faucibus eget, consectetur ac dui. Curabitur ornare non ipsum elementum suscipit. Sed a dignissim ligula, nec vestibulum dui. Fusce sed varius tellus.
Vestibulum lectus metus, tincidunt at fermentum non, pellentesque at lorem. Vivamus nisl sem, tempor ac mi et, elementum feugiat justo. Pellentesque tristique consequat molestie. Donec non porttitor risus. Praesent adipiscing elementum tortor, commodo ullamcorper justo consequat eget. Aenean eget nulla enim. Nam purus massa, hendrerit quis faucibus eget, consectetur ac dui. Curabitur ornare non ipsum elementum suscipit. Sed a dignissim ligula, nec vestibulum dui. Fusce sed varius tellus.
\begin{mdframed}[
innerrightmargin = 0.5cm,
innertopmargin = 0.5cm,
skipbelow = 135mm,
backgroundcolor = blue,
skipabove = 35mm,
linewidth = 0pt,
userdefinedwidth = 9cm,
innerbottommargin = 0.5cm,
splitbottomskip = 10mm,
splittopskip = 10mm,
roundcorner = 5pt,
innerleftmargin = 0.5cm,
align = center]
Vestibulum lectus metus, tincidunt at fermentum non, pellentesque at lorem. Vivamus nisl sem, tempor ac mi et, elementum feugiat justo. Pellentesque tristique consequat molestie. Donec non porttitor risus. Praesent adipiscing elementum tortor, commodo ullamcorper justo consequat eget. Aenean eget nulla enim. Nam purus massa, hendrerit quis faucibus eget, consectetur ac dui. Curabitur ornare non ipsum elementum suscipit. Sed a dignissim ligula, nec vestibulum dui. Fusce sed varius tellus.
\end{mdframed}
\end{mdframed}
\end{document}
答案1
这为该问题提供了半自动和全自动解决方案... 在所有情况下,它都是“自动”的,因为mdframe
创建颜色填充所需的所有参数都直接从环境中获取,无需用户干预。如果用户选择“半自动”解决方案形式,它允许他们手动调用宏\padframe[padheight]
以在需要的时间和地点包含填充。
在全自动解决方案中,环境Mdframed
会为每个嵌套框架粘贴填充,无论框架是否拆分(但不包括外框)。在半自动和全自动版本中,默认填充高度是外框splittopskip
的高度。
它从外部环境自动抓取的参数mdframe
有backgroundcolor
、userdefinedwidth
、innerleftmargin
、innerrightmargin
和splittopskip
(默认的 padheight);从内部mdframe
环境抓取innertopmargin
、userdefinedwidth
、innerleftmargin
、 和innerrightmargin
。
它使用这些参数来构建一组\rule
s,使外部的颜色呈现“填充”的外观mdframe
。
修订说明:
V1:初步答案
V2:通过使用长度,消除了在\padvslop
嵌套/填充时出现的水平重影线mdframe
中间页面。添加 vslop 后,我必须修改构建顶部规则的方式\laps
。此外,将splittopskip
外框设为默认 pad 高度,而不是splitbottomskip
外框。
mdframe
V3:构建垫圈时,考虑了外部左右边缘不对称的问题。
\padframe
V4:从外部调用 无关紧要mdframe
(它们被忽略)。这意味着剩下要做的就是尝试让 总是在进入时mdframed
调用\padframe
。到目前为止,我的努力还没有成功。
V5:引入了Mdframed
自动插入填充。我通过将参数设置\mdfManualPaddding
为{T}
或 ,将 MWE 设置为“手动插入填充”或“自动插入填充” {F}
。使用自动填充时,将忽略手动调用\padframe
。
\documentclass[a4paper]{article}
\usepackage{mdframed}
\usepackage{xcolor}
% DECIDE WHETHER YOU WANT TO MANUALLY INSERT PADDING AND SET ACCORDINGLY
\def\mdfManualPadding{F}% {T} or {F}
%%%%%
\newlength\padvslop% TO ELIMINATE HORIZONTAL GHOST LINES
\padvslop=0.3pt% KEEP AS SMALL AS POSSIBILE,
% SINCE THIS WILL BE A TOP MARGIN OVERRUN ON PADDED SPLIT FRAMES
\makeatletter
\let\svmdframed\mdframed
\let\svendmdframed\endmdframed
\def\mdfl@splittopskip{U}% SERVES AS INITIALIZATION
\def\mdf@backgroundcolor{white}% SERVES AS INITIALIZATION
\def\mdfl@userdefinedwidth{U}% SERVES AS INITIALIZATION
\def\mdfl@innerleftmargin{U}% SERVES AS INITIALIZATION
\def\mdfl@innerrightmargin{U}% SERVES AS INITIALIZATION
\def\outermdparams{\edef\oSTS{\mdfl@splittopskip}\edef\oBGC{\mdf@backgroundcolor}%
\edef\oILM{\mdfl@innerleftmargin}\edef\oIRM{\mdfl@innerrightmargin}%
\edef\oUDW{\mdfl@userdefinedwidth}}
\newlength\SidebarWidth
\newlength\LRMarginDiscrepancy
\newcounter{mdfnestlevel}
\newcommand\@padframe[1][\oSTS]{%
\ifnum\themdfnestlevel=1\else{% IF CALLED FROM OUTER FRAME, IGNORE!
\def\ITM{\mdfl@innertopmargin}%
\def\ILM{\mdfl@innerleftmargin}%
\def\IRM{\mdfl@innerrightmargin}%
\def\UDW{\mdfl@userdefinedwidth}%
\def\PBS{#1}%
\SidebarWidth=\dimexpr\oUDW-\UDW%
\SidebarWidth=.5\SidebarWidth%
\LRMarginDiscrepancy=\dimexpr\oILM-\oIRM%
\LRMarginDiscrepancy=.5\LRMarginDiscrepancy%
\fboxsep=0pt\fboxrule=0pt%
\rule[-\PBS]{0pt}{0pt}%
\smash{%
\raisebox{\dimexpr\ITM-\PBS+\padvslop}{\llap{\colorbox{\oBGC}{%
\rule{\dimexpr\ILM+\SidebarWidth+\LRMarginDiscrepancy+1pt}{0pt}\rule{0pt}{\PBS}}\kern-1pt}}%
\raisebox{\dimexpr\ITM-\PBS+\padvslop}{%
\colorbox{\oBGC}{\rule{\textwidth}{0pt}\rule{0pt}{\PBS}}}%
\raisebox{\dimexpr\ITM-\PBS+\padvslop}{\rlap{\kern-1pt\colorbox{\oBGC}{%
\rule{\dimexpr\IRM+\SidebarWidth-\LRMarginDiscrepancy+1pt}{0pt}\rule{0pt}{\PBS}}}}%
}}%
\par\noindent\ignorespaces\fi%
}
\if T\mdfManualPadding
%%%%% DEFINE THIS TO USE mdframed OR Mdframed WITH MANUAL INVOCATIONS OF \padframe
\newcommand\padframe[1][\oSTS]{\@padframe[#1]}
\renewcommand\mdframed{\stepcounter{mdfnestlevel}\outermdparams\svmdframed}
\renewcommand\endmdframed{\svendmdframed\addtocounter{mdfnestlevel}{-1}}
\newenvironment{Mdframed}[1][]{%
\begin{mdframed}[#1]%
}{%
\end{mdframed}%
}
\else
%%%%% OR ELSE DEFINE THIS TO USE NEWLY CREATED Mdframed,
%%%%% WHICH AUTO-INSERTS PADDING WITH NESTED FRAMES
\newcommand\padframe[1][]{\relax}% IGNORE MANUALLY INSERTED PADS
\newenvironment{Mdframed}[1][]{%
\stepcounter{mdfnestlevel}%
\outermdparams%
\begin{mdframed}[#1]%
\@padframe\@ifnextchar\par\@gobble\relax%
}{%
\end{mdframed}%
\addtocounter{mdfnestlevel}{-1}%
}
%%%%%%
\fi
\makeatother
\begin{document}
\def\mytext{%
Vestibulum lectus metus, tincidunt at fermentum non, pellentesque at lorem. Vivamus
nisl sem, tempor ac mi et, elementum feugiat justo. Pellentesque tristique consequat
molestie. Donec non porttitor risus. Praesent adipiscing elementum tortor, commodo
ullamcorper justo consequat eget. Aenean eget nulla enim. Nam purus massa, hendrerit
quis faucibus eget, consectetur ac dui. Curabitur ornare non ipsum elementum
suscipit. Sed a dignissim ligula, nec vestibulum dui. Fusce sed varius tellus.}
\begin{Mdframed}[
innerrightmargin = 0.5cm,
innertopmargin = 0.5cm,
skipbelow = 5mm,
backgroundcolor = red,
skipabove = 5mm,
linewidth = 0pt,
userdefinedwidth = 11cm,
innerbottommargin = 0.5cm,
splitbottomskip = 10mm,
splittopskip = 5mm, % splittopskip IS THE DEFAULT TO APPLY AS HEIGHT OF A NESTED \padframe
roundcorner = 5pt,
innerleftmargin = 1cm,
align = center]
\padframe% USE THIS FORM WITH MANUAL PADDING
THIS CASE SHOWS A NESTED mdframe WITH PADDING ON SAME PAGE. THE MARGINS ARE
ASYMMETRICAL. THE LENGTH padvslop WAS USED TO ELIMINATE HORIZONTAL GHOST LINE
\mytext\par
\mytext
\begin{Mdframed}[
innerrightmargin = 0.5cm,
innertopmargin = 0.5cm,
skipbelow = 5mm,
backgroundcolor = blue,
skipabove = 5mm,
linewidth = 0pt,
userdefinedwidth = 9cm,
innerbottommargin = 0.5cm,
splitbottomskip = 10mm,
splittopskip = 10mm,
roundcorner = 5pt,
innerleftmargin = 0.5cm,
align = center]
\padframe% USE THIS FORM WITH mdframed, NOT Mdframed
\mytext
\end{Mdframed}
\end{Mdframed}
\clearpage
\begin{Mdframed}[
innerrightmargin = 0.5cm,
innertopmargin = 0.5cm,
skipbelow = 135mm,
backgroundcolor = red,
skipabove = 35mm,
linewidth = 0pt,
userdefinedwidth = 11cm,
innerbottommargin = 0.5cm,
splitbottomskip = 10mm,
splittopskip = 5mm, % splittopskip IS THE DEFAULT TO APPLY AS HEIGHT OF A NESTED \padframe
roundcorner = 5pt,
innerleftmargin = 0.5cm,
align = center]
THIS CASE SHOWS A NESTED mdframe WITH PADDING ON THE FOLLOWING PAGE.
\padframe% USE THIS FORM WITH mdframed, NOT Mdframed
\mytext\par
\mytext\par
\mytext\par
\mytext
\begin{Mdframed}[
innerrightmargin = 0.5cm,
innertopmargin = 0.5cm,
skipbelow = 135mm,
backgroundcolor = blue,
skipabove = 35mm,
linewidth = 0pt,
userdefinedwidth = 9cm,
innerbottommargin = 0.5cm,
splitbottomskip = 10mm,
splittopskip = 10mm,
roundcorner = 5pt,
innerleftmargin = 0.5cm,
align = center]
\padframe[3cm]% USE THIS FORM WITH mdframed, NOT Mdframed
\mytext
\end{Mdframed}
\end{Mdframed}
\end{document}
mdframe
例如,当内部定义为
\begin{mdframed}[
innerrightmargin = 1.1cm,
innertopmargin = 1.1cm,
skipbelow = 135mm,
backgroundcolor = blue,
skipabove = 35mm,
linewidth = 0pt,
userdefinedwidth = 9cm,
innerbottommargin = 0.5cm,
splitbottomskip = 10mm,
splittopskip = 10mm,
roundcorner = 5pt,
innerleftmargin = 1.1cm,
align = center]
\padframe[1cm] % SPECIFY PAD HEIGHT EXPLICITLY
\mytext
\end{mdframed}
其中 padframe 高度指定为 1cm,innerleft/rightmargin
s 和innertopmargin
都设置为 1.1cm,结果如下: