具有嵌套框架的遗传属性(mdframed 和 tikz)

具有嵌套框架的遗传属性(mdframed 和 tikz)

当在使用 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盒子与其他盒子混合嵌套以规避此问题。在这里,我使用 来tcolorbox作为嵌套盒子。

\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}

相关内容