4

When nesting an mdframed environment in a mdframed environment that uses tikz, it seems that the nested mdframed environment takes on many of the properties of the environment isn't being nested in, replacing its own properties. These include, well, pretty much everything, including nodes, background color, outer line width (and color), and the rounding of corners, except in the case where the nested mdframed environment also uses tikz, in which case only the node problem exists. This is illustrated with several different examples below:

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

The issue persists whether or not there are nodes (or what options they are given, i.e. \singleextra, \doubleextra, etc don't make a different). The issue does not occur in the opposite scenario with a normal mdframed environment containing an mdframed environment using tikz.

Is there any way to stop the nested mdframed environment from taking the properties from the environment it is contained in? In particular, is there any way to ensure that the nodes aren't repeated, since this also messes with the numbering?

3

Simply add the default option to the simplestyle:

\global\mdfdefinestyle{simplestyle}{%
    default,
    linecolor=red,linewidth=3pt,%
    leftmargin=1cm,rightmargin=1cm
}

This resets the styles to their default setting at the start.

enter image description here


Alternatively one can apply style=defaultoptions. A simpler example illustrating the use of both possibilities

enter image description here

Code:

\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

There may be a way in mdframed, but I don't know it. However, what I did here was create the inner box outside of the outer environment, saved it, and stuck it in when needed.

EDITED to show how multiple [named] inner boxes may be saved in advance of the outer box invocation (my initial answer used one temporary box).

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

enter image description here

  • Do you know of any way to automate this? For example, if I have the scenario where I am using a lot of nested framed environments, how could I make it such that a nested framed environment does as you recommend? – Hayden Dec 27 '14 at 16:59
  • @Hayden So far, the best I can do is shown in my revised solution. I will keep looking for further automation. – Steven B. Segletes Dec 27 '14 at 20:06
1

You could mix mdframed boxes with other boxes for nesting to circumvent the problem. Here, I use a tcolorbox for the nested box.

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

enter image description here

For fun, I also add some code for an complete tcolorbox solution below, but I see no problem in using a mixed approach with mdframed and tcolorbox as described above.

\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}
  • Thanks, but sadly the scenario I'm in usually involves the same mdframed style being used for both the nested and nesting environments. However, your example using only tcolorbox might just be what I decide to do! – Hayden Dec 27 '14 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.