How to store an mdframe in a savebox?

The title question is the last piece of information I would need, to solve my problem my way. Putting a \sbox or lrbox-environment around a mdenvironment yields a

! Missing \endgroup inserted.


which I don't know how to resolve.

(A MWE can be found at the bottom)

Maybe there is an entirely different better approach, so here is the full story:

I am working on a couple of environments to ease creating slides with the beamer class. Currently, I want to place a box (an image if the content matters) relative to the position and/or dimension of a mdframe environment, as a sort of label.

The figure illustrates the kind of element I want to create. Ultimately many such elements will be grouped as an environment, which arranges the elements with each other at compile time, to make things look nicely.

My problem is placing the label, the lightning in the example image. This is very difficult, because \ldots

1. if I put it right before the mdenvironment, the mdenvironment will draw over it so that only the top half of the lightning is visible.

2. if I put it into the mdenvironment I can't find a reliable method of calculating how to move the lightning where it's supposed to be.

3. if I put it just after the mdenvironment I need to determine the height and depth of the mdenvironment to calculate how far up I need to move the label

From my point of view the problem with option 1 can't be fixed. In total I think I spent 8 hours trying to make option 2 work, but failed. The third options seems viable, but putting a \sbox or lrbox-environment around the mdenvironment yields a

! Missing \endgroup inserted.


which I can't resolve.

Here's an almost minimal working example:

\documentclass{article}
\usepackage[framemethod=pgf]{mdframed}

\newmdenv[%
innerlinewidth=2pt,%
roundcorner=10pt,%
linecolor=black,%
backgroundcolor=white,%
skipabove=.5\baselineskip,skipbelow=.5\baselineskip,%
innerleftmargin=5pt,innerrightmargin=5pt,innertopmargin=5pt,innerbottommargin=5pt%
]{mybox}

\begin{document}

\sbox0{%
\begin{mybox}
This is some text.
\end{mybox}
}

\end{document}


Almost minimal, because I left my specification of the mdenvironment, because I don't know how important it is for solving the problem.

I (think I) need to put this in a box to obtain its height and depth, needn't I?

• Would you be wiiling to switch to tcolorbox if case be? – Gonzalo Medina Jul 15 '15 at 22:29
• @GonzaloMedina: If tcolorbox has an equivalent for every mdframed option I use in the example, sure. – Bananguin Jul 15 '15 at 22:54
• Sure it has equivalent for all of them and, additionally, offers you many, many more features. Besides, its implementation is "more robust". – Gonzalo Medina Jul 15 '15 at 22:56

You need to wrap it in a vertical box such as minipage:

\documentclass{article}
\usepackage[framemethod=pgf]{mdframed}

\newmdenv[%
innerlinewidth=2pt,%
roundcorner=10pt,%
linecolor=black,%
backgroundcolor=white,%
skipabove=.5\baselineskip,skipbelow=.5\baselineskip,%
innerleftmargin=5pt,innerrightmargin=5pt,innertopmargin=5pt,innerbottommargin=5pt%
]{mybox}

\begin{document}

\begin{lrbox}{0}\begin{minipage}{\textwidth}
\begin{mybox}
This is some text.
\end{mybox}
\end{minipage}\end{lrbox}

\the\ht0,\the\dp0

\end{document}

• Arghhh! By 29 secs. +1 just for that :) – Gonzalo Medina Jul 15 '15 at 22:32
• Painfully simple answer´... again. Why do I have to put it in a vertical box? This seems entirely arbitrary to me. I notice many answers using lrbox actually use lrbox+minipage. When do I not need a minipage? And now to something completely diffent: Why has autocompletion for @David Carlisle stopped working? Other names work fine! – Bananguin Jul 15 '15 at 22:52
• @user1129682 because mdframe is a vertical construct that allows page breaking etc, same as you can not have equation or itemize environments in a horizontal (LR) box such a \mbox or \lrbox. You would not want a minipage if you were measuring horizontal material eg \sbox{0}{hello} allows you to measure the width of hello if you wrapped that in minipage the box would have whatever width you specified for minipage, so would not measure the text. – David Carlisle Jul 15 '15 at 22:56
• isn't better if one use TeX primitives \setbox \vbox \unvbox just in case mdframe is breakable? – touhami Jul 15 '15 at 23:05
• @touhami no, why? – David Carlisle Jul 15 '15 at 23:05

As David has commented in his answer, you need a minipage. If you switch to tcolorbox, you can do this in a simpler (and shorter) way using the capture key which allows you to capture the box in a different number of ways, including a minipage. A little example reproducing your box:

\documentclass{article}
\usepackage[many]{tcolorbox}

\newtcolorbox{myboxi}[1][]{
breakable,
colback=white,
colframe=black,
arc=8pt,
boxrule=2pt,
left=5pt,
right=5pt,
top=5pt,
bottom=5pt,
boxsep=0pt,
minipage,
#1
}

\begin{document}

\sbox0{%
\begin{myboxi}
This is some text.
\end{myboxi}}

Width: \the\wd0,
height: \the\ht0,
depth: \the\dp0.

\end{document}


tcolorbox also offers you nice features to smoothly blend with beamer through its beamer skin.