當在使用 tikz 的 mdframed 環境中嵌套 mdframed 環境時,嵌套的 mdframed 環境似乎採用了未嵌套的環境的許多屬性,替換了它自己的屬性。這些幾乎包括所有內容,包括節點、背景顏色、外線寬度(和顏色)和圓角,但嵌套 mdframed 環境也使用 tikz 的情況除外,在這種情況下僅存在節點問題。下面用幾個不同的例子來說明這一點:
\documentclass{article}
\usepackage{tikz}
\usepackage[framemethod=tikz]{mdframed}
%%%%%%%%%%%%%%%%% mdframed style with node
\tikzset{tetregris/.style =
{draw=gray, thick, fill=white, shading = exampletitle, %
text=darkgray, rectangle, rounded corners, right,minimum height=.7cm}}
\pgfdeclarehorizontalshading{examplebackground}{100bp}
{color(0bp)=(black!2); color(100bp)=(teal!30)}
\pgfdeclarehorizontalshading{exampletitle}{100bp}
{color(0bp)=(black!2);color(100bp)=(purple!20)}
\newcounter{example}
\renewcommand*\theexample{Example~\arabic{example}}
\makeatletter
\mdfdefinestyle{examplestyle}{%
outerlinewidth=1em,outerlinecolor=white,%
leftmargin=-1em,rightmargin=-1em,%
middlelinewidth=1.2pt,roundcorner=5pt,linecolor=gray,
apptotikzsetting={\tikzset{mdfbackground/.append style ={%
shading = examplebackground}}},
innertopmargin=1.2\baselineskip,
skipabove={\dimexpr0.5\baselineskip+\topskip\relax},
skipbelow={-1em},
needspace=\baselineskip,
frametitlefont=\sffamily\scshape,
settings={\global\stepcounter{example}},
singleextra={%
\node[tetregris,xshift=-3cm] at (P) %
{~\mdf@frametitlefont{\theexample}\hbox{~}};
},
firstextra={%
\node[tetregris,xshift=-3cm] at (P) %
{~\mdf@frametitlefont{\theexample}\hbox{~}};
},
}
\makeatother
%%%%%%%%%%%%%%%%%% simple mdframed style
\global\mdfdefinestyle{simplestyle}{%
linecolor=red,linewidth=3pt,%
leftmargin=1cm,rightmargin=1cm
}
\begin{document}
\begin{mdframed}[style=examplestyle]
blah
\begin{mdframed}[style=simplestyle]
blah
\end{mdframed}
blah
\end{mdframed}
\begin{mdframed}[style=simplestyle]
blah
\end{mdframed}
\end{document}
無論是否存在節點(或給定的選項是什麼,即 \singleextra、\doubleextra 等都不會產生不同),問題仍然存在。問題才不是發生在與包含使用 tikz 的 mdframed 環境的正常 mdframed 環境相反的情況中。
有什麼方法可以阻止嵌套的 mdframed 環境從它所包含的環境中取得屬性?特別是,有什麼方法可以確保節點不重複,因為這也會擾亂編號?
答案1
只需將default
選項新增至simplestyle
:
\global\mdfdefinestyle{simplestyle}{%
default,
linecolor=red,linewidth=3pt,%
leftmargin=1cm,rightmargin=1cm
}
這會將樣式重設為開始時的預設值。
或也可以申請style=defaultoptions
。一個更簡單的例子說明了兩種可能性的使用
代碼:
\documentclass{article}
\usepackage{xcolor}
\usepackage[framemethod=Tikz]{mdframed}
\mdfsetup{middlelinecolor=red, middlelinewidth=2pt, backgroundcolor=red!10, roundcorner=10pt}
\listfiles
\begin{document}
\noindent
Without \verb|default| option:
\begin{mdframed} Text\par Text
\end{mdframed}
% --------------------------------------
With \verb|default| option applied:
\begin{mdframed}[default] Text \par Text
\end{mdframed}
% --------------------------------------
With \verb|style=defaultoptions| option applied:
\begin{mdframed}[style=defaultoptions] Text\par Text
\end{mdframed}
\end{document}
答案2
也許有辦法進去mdframed
,但我不知道。然而,我在這裡所做的是在外部環境之外創建內部盒子,保存它,並在需要時將其插入。
編輯以顯示如何在外部框調用之前保存多個[命名]內部框(我最初的答案使用一個臨時框)。
\documentclass{article}
\usepackage{tikz}
\usepackage[framemethod=tikz]{mdframed}
%%%%%%%%%%%%%%%%% mdframed style with node
\tikzset{tetregris/.style =
{draw=gray, thick, fill=white, shading = exampletitle, %
text=darkgray, rectangle, rounded corners, right,minimum height=.7cm}}
\pgfdeclarehorizontalshading{examplebackground}{100bp}
{color(0bp)=(black!2); color(100bp)=(teal!30)}
\pgfdeclarehorizontalshading{exampletitle}{100bp}
{color(0bp)=(black!2);color(100bp)=(purple!20)}
\newcounter{example}
\renewcommand*\theexample{Example~\arabic{example}}
\makeatletter
\mdfdefinestyle{examplestyle}{%
outerlinewidth=1em,outerlinecolor=white,%
leftmargin=-1em,rightmargin=-1em,%
middlelinewidth=1.2pt,roundcorner=5pt,linecolor=gray,
apptotikzsetting={\tikzset{mdfbackground/.append style ={%
shading = examplebackground}}},
innertopmargin=1.2\baselineskip,
skipabove={\dimexpr0.5\baselineskip+\topskip\relax},
skipbelow={-1em},
needspace=\baselineskip,
frametitlefont=\sffamily\scshape,
settings={\global\stepcounter{example}},
singleextra={%
\node[tetregris,xshift=-3cm] at (P) %
{~\mdf@frametitlefont{\theexample}\hbox{~}};
},
firstextra={%
\node[tetregris,xshift=-3cm] at (P) %
{~\mdf@frametitlefont{\theexample}\hbox{~}};
},
}
\makeatother
%%%%%%%%%%%%%%%%%% simple mdframed style
\global\mdfdefinestyle{simplestyle}{%
linecolor=red,linewidth=3pt,%
leftmargin=1cm,rightmargin=1cm
}
\begin{document}
% SAVE MULTIPLE INNER BOXES IN ADVANCE
\newsavebox\boxA
\savebox\boxA{\vbox{\begin{mdframed}[style=simplestyle]BLAH-A\end{mdframed}}}
\newsavebox\boxB
\savebox\boxB{\vbox{\begin{mdframed}[style=simplestyle]BLAH-B\end{mdframed}}}
\begin{mdframed}[style=examplestyle]
\parindent=0pt
blah\par
\usebox{\boxA}
blah\par
\usebox{\boxB}
blah
\end{mdframed}
\end{document}
答案3
您可以將mdframed
盒子與其他盒子混合進行嵌套,以避免這個問題。在這裡,我使用 atcolorbox
作為嵌套框。
\documentclass{article}
\usepackage{tikz}
\usepackage[framemethod=tikz]{mdframed}
\usepackage[skins,breakable]{tcolorbox}
%%%%%%%%%%%%%%%%% mdframed style with node
\tikzset{tetregris/.style =
{draw=gray, thick, fill=white, shading = exampletitle, %
text=darkgray, rectangle, rounded corners, right,minimum height=.7cm}}
\pgfdeclarehorizontalshading{examplebackground}{100bp}
{color(0bp)=(black!2); color(100bp)=(teal!30)}
\pgfdeclarehorizontalshading{exampletitle}{100bp}
{color(0bp)=(black!2);color(100bp)=(purple!20)}
\newcounter{example}
\renewcommand*\theexample{Example~\arabic{example}}
\makeatletter
\mdfdefinestyle{examplestyle}{%
outerlinewidth=1em,outerlinecolor=white,%
leftmargin=-1em,rightmargin=-1em,%
middlelinewidth=1.2pt,roundcorner=5pt,linecolor=gray,
apptotikzsetting={\tikzset{mdfbackground/.append style ={%
shading = examplebackground}}},
innertopmargin=1.2\baselineskip,
skipabove={\dimexpr0.5\baselineskip+\topskip\relax},
skipbelow={-1em},
needspace=\baselineskip,
frametitlefont=\sffamily\scshape,
settings={\global\stepcounter{example}},
singleextra={%
\node[tetregris,xshift=-3cm] at (P) %
{~\mdf@frametitlefont{\theexample}\hbox{~}};
},
firstextra={%
\node[tetregris,xshift=-3cm] at (P) %
{~\mdf@frametitlefont{\theexample}\hbox{~}};
},
}
\makeatother
%%%%%%%%%%%%%%%%%% simple style
\tcbset{
simplestyle/.style={%
sharp corners,
boxsep=0pt,left skip=1cm,right skip=1cm,boxrule=3pt,
colframe=red,colback=white
}
}
\begin{document}
\begin{mdframed}[style=examplestyle]
blah
\begin{tcolorbox}[simplestyle]
blah
\end{tcolorbox}
\noindent blah
\end{mdframed}
\begin{tcolorbox}[simplestyle]
blah
\end{tcolorbox}
\end{document}
為了好玩,我還為下面的完整解決方案添加了一些程式碼tcolorbox
,但我認為使用 和 的混合方法沒有問題mdframed
,tcolorbox
如上所述。
\documentclass{article}
\usepackage[skins,breakable]{tcolorbox}
\pgfdeclarehorizontalshading{examplebackground}{100bp}
{color(0bp)=(black!2); color(100bp)=(teal!30)}
\pgfdeclarehorizontalshading{exampletitle}{100bp}
{color(0bp)=(black!2);color(100bp)=(purple!20)}
\newcounter{example}
\renewcommand*\theexample{Example~\arabic{example}}
\tcbset{
examplestyle/.style={%
enhanced,breakable,arc=5pt,beforeafter skip=1em,
colframe=gray,interior style={shading=examplebackground},
fonttitle=\sffamily\scshape,coltitle=darkgray,
step=example,
title=\theexample,
attach boxed title to top right={xshift=-6mm,yshift*=-\tcboxedtitleheight/2},
boxed title style={enhanced,size=fbox,arc=3pt,boxrule=0.3mm,boxsep=2mm,
interior style={shading=exampletitle}},
},
simplestyle/.style={%
sharp corners,
boxsep=0pt,left skip=1cm,right skip=1cm,boxrule=3pt,
colframe=red,colback=white
}
}
\begin{document}
\begin{tcolorbox}[examplestyle]
blah
\begin{tcolorbox}[simplestyle]
blah
\end{tcolorbox}
\noindent blah
\end{tcolorbox}
\begin{tcolorbox}[simplestyle]
blah
\end{tcolorbox}
\end{document}