mdframed
我正在使用包(以及TikZ
)根据中给出的题外话示例创建新的框架环境mdframed-example-texsx.tex
。我的代码如下:
\documentclass{scrartcl}
\usepackage{times}
\usepackage{lipsum}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc,arrows,shadows}
\usepackage[framemethod=tikz]{mdframed}
\tikzset{
title/.style={
fill=white,
font=\normalfont,
text=black,
anchor=base west,
},
contour/.style = {
line width = 0.6pt,
draw = black,
rounded corners = 2ex,
},
fakeshadow/.style = {
line width = 4.5pt,
draw = white,
},
}
\newcommand{\definitiontitle}{
{\scshape \bfseries \Large Definition}
}
\mdfdefinestyle{definition}{%
singleextra={%
%% Store (O) in \p1, store (P) in \p2. Now \p1=(\x1,\y1) and \p2=(\x2,\y2). From that, define (Q) = (\x1,\y2).
\path let \p1=(O), \p2=(P) in (\x1,\y2) coordinate (Q);
\path let \p1=(O), \p2=(P) in (\x2,\y1) coordinate (R);
\path let \p1=(O), \p2=(Q) in (\x1,{(\y1+\y2)/2}) coordinate (M);
\path[contour] (M) |- (P) |- (O) -- (M);
\node[title, anchor=west, xshift=18pt - 5pt] at (Q) {\definitiontitle};
},
firstextra={%
\path let \p1=(O), \p2=(P) in (\x1,\y2) coordinate (Q);
\path let \p1=(O), \p2=(P) in (\x2,\y1) coordinate (R);
\path[contour] (O) -- (Q) -- (P) -- (R);
\node[title, anchor=west, xshift=18pt - 5pt] at (Q) {\definitiontitle};
\path[fakeshadow] ($(O)+(1pt,-1.5pt)$) -- ($(R)+(-1pt,-1.5pt)$); %% Hide the bottom shadow
},
secondextra={%
\path let \p1=(O), \p2=(P) in (\x1,\y2) coordinate (Q);
\path let \p1=(O), \p2=(P) in (\x2,\y1) coordinate (R);
\path[contour] (Q) -- (O) -- (R) -- (P);
},
middleextra={%
\path let \p1=(O), \p2=(P) in (\x1,\y2) coordinate (Q);
\path let \p1=(O), \p2=(P) in (\x2,\y1) coordinate (R);
\path[contour] (O) -- (Q);
\path[contour] (P) -- (R);
\path[fakeshadow] ($(O)+(1pt,-1.5pt)$) -- ($(R)+(-1pt,-1.5pt)$); %% Hide the bottom shadow
},
align=center,
backgroundcolor=yellow,
userdefinedwidth=.9\textwidth,
middlelinewidth=1.7em,middlelinecolor=white,
hidealllines=true,topline=true,
innertopmargin=6pt,
innerbottommargin=18pt,
innerleftmargin=18pt,
innerrightmargin=18pt,
splitbottomskip=8pt,
splittopskip=16pt,
roundcorner=2ex,
shadow=true,
shadowsize=5,
shadowcolor=black!40,
%% Experimental
needspace=3em,
ignorelastdescenders=true,
}
\begin{document}
\lipsum[3]
\vspace{1\baselineskip}
\begin{mdframed}[style=definition]
\lipsum[1]
\end{mdframed}
\vspace{1\baselineskip}
\lipsum[3]
\vspace{1\baselineskip}
\begin{mdframed}[style=definition]
\lipsum[1-2]
\end{mdframed}
\vspace{1\baselineskip}
\lipsum[3]
\vspace{1\baselineskip}
\begin{mdframed}[style=definition]
\lipsum[1-8]
\end{mdframed}
\end{document}
我目前正在使用 TikZ 放置自定义标题并绘制框架。这是必要的,以便使用适当的middlelinewidth
高度来补偿标题高度,这样 mdframed 就会知道在哪里分割。但是,我也使用了圆角,这就是整体问题发生的地方。
正如 Marco Daniel 在这个帖子,使用代码很重要
hidealllines=true,topline=true,
然而,在 中使用hidealllines=true
会产生意想不到的效果\mdf@singleextra
,即圆角消失,取而代之的是呈现完整的框。当框架被分割时(或当hidealllines=true
未使用时,但无法正确计算高度时),不会发生这种情况,如输出所示:
知道为什么这种情况只会发生在哪里\mdf@singleextra
以及如何解决它吗?
答案1
如果你设置了该选项,就会出现问题,即背景绘制时没有任何圆角topline=true
。但是你可以破解测试:
\makeatletter
\let\mdf@putbox@single@orig\mdf@putbox@single
\mdfapptodefinestyle{definition}{%
settings={%
\def\mdf@putbox@single{%
\let\mdf@test@t\@gobbletwo
\let\mdf@test@noline\@firstoftwo
\mdf@putbox@single@orig
}%
}%
}
\makeatother
破解后,你必须singlextra
通过以下行扩展选项:
\path[draw=white,line width=1.7em,overlay] (O|-P) -- (P);
绘制标题的白色背景。
第二页的奇怪角落无法重现。
这里是输出:
完整代码如下:
\documentclass{scrartcl}
\usepackage{times}
\usepackage{lipsum}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc,arrows,shadows}
\usepackage[framemethod=tikz]{mdframed}
\tikzset{
title/.style={
fill=white,
font=\normalfont,
text=black,
anchor=base west,
},
contour/.style = {
line width = 0.6pt,
draw = black,
rounded corners = 2ex,
},
fakeshadow/.style = {
line width = 4.5pt,
draw = white,
},
}
\newcommand{\definitiontitle}{
{\scshape \bfseries \Large Definition}
}
\mdfdefinestyle{definition}{%
singleextra={%
%% Store (O) in \p1, store (P) in \p2. Now \p1=(\x1,\y1) and \p2=(\x2,\y2). From that, define (Q) = (\x1,\y2).
\path let \p1=(O), \p2=(P) in (\x1,\y2) coordinate (Q);
\path let \p1=(O), \p2=(P) in (\x2,\y1) coordinate (R);
\path let \p1=(O), \p2=(Q) in (\x1,{(\y1+\y2)/2}) coordinate (M);
\path[draw=white,line width=1.7em,overlay] (O|-P) -- (P);
\path[contour,] (M) |- (P) |- (O) -- (M);
\node[title, anchor=west, xshift=18pt - 5pt] at (Q) {\definitiontitle};
},
firstextra={%
\path let \p1=(O), \p2=(P) in (\x1,\y2) coordinate (Q);
\path let \p1=(O), \p2=(P) in (\x2,\y1) coordinate (R);
\path[contour] (O) -- (Q) -- (P) -- (R);
\node[title, anchor=west, xshift=18pt - 5pt] at (Q) {\definitiontitle};
\path[fakeshadow] ($(O)+(1pt,-1.5pt)$) -- ($(R)+(-1pt,-1.5pt)$); %% Hide the bottom shadow
},
secondextra={%
\path let \p1=(O), \p2=(P) in (\x1,\y2) coordinate (Q);
\path let \p1=(O), \p2=(P) in (\x2,\y1) coordinate (R);
\path[contour] (Q) -- (O) -- (R) -- (P);
},
middleextra={%
\path let \p1=(O), \p2=(P) in (\x1,\y2) coordinate (Q);
\path let \p1=(O), \p2=(P) in (\x2,\y1) coordinate (R);
\path[contour] (O) -- (Q);
\path[contour] (P) -- (R);
\path[fakeshadow] ($(O)+(1pt,-1.5pt)$) -- ($(R)+(-1pt,-1.5pt)$); %% Hide the bottom shadow
},
align=center,
backgroundcolor=yellow,
userdefinedwidth=.9\textwidth,
middlelinewidth=1.7em,middlelinecolor=white,
hidealllines=true,topline=true,
innertopmargin=6pt,
innerbottommargin=18pt,
innerleftmargin=18pt,
innerrightmargin=18pt,
splitbottomskip=8pt,
splittopskip=16pt,
roundcorner=2ex,
% shadow=true,
shadowsize=5,
shadowcolor=black!40,
%% Experimental
needspace=3em,
ignorelastdescenders=true,
}
\makeatletter
\let\mdf@putbox@single@orig\mdf@putbox@single
\mdfapptodefinestyle{definition}{%
settings={%
\def\mdf@putbox@single{%
\let\mdf@test@t\@gobbletwo
\let\mdf@test@noline\@firstoftwo
\mdf@putbox@single@orig
}%
}%
}
\makeatother
\begin{document}
\lipsum[3]
\vspace{1\baselineskip}
\begin{mdframed}[style=definition]
\lipsum[1]
\end{mdframed}
\vspace{1\baselineskip}
\lipsum[3]
\vspace{1\baselineskip}
\begin{mdframed}[style=definition]
\lipsum[1-2]
\end{mdframed}
\vspace{1\baselineskip}
\lipsum[3]
\vspace{1\baselineskip}
\begin{mdframed}[style=definition]
\lipsum[1-8]
\end{mdframed}
\end{document}