# How to produce a tight box with a fancy border?

I'd like to be able to typeset a tightly-boxed formula with a fancy border.

My progress so far has been to produce a box (with mdframed) with the fancy border I want (with tikz), but instead of being tight around its contents, it always extends to the width of the line.

\documentclass{beamer}

\mode<presentation>

\usepackage[framemethod=tikz]{mdframed}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

% Caution-tape frame style
\mdfdefinestyle{caution}{linewidth=10pt,
tikzsetting={decorate,decoration={markings,
mark=between positions 0 and 1 step 6pt
with {
\fill[yellow] (-3pt,-2pt) -- (0pt,-2pt) -- (3pt,2pt) -- (0pt,2pt) -- (-3pt,-2pt);
\fill[black] (0pt,-2pt) -- (3pt,-2pt) -- (6pt,2pt) -- (3pt,2pt) -- (0pt,-2pt);
}}}}

\begin{document}

\begin{frame}{testing}

\begin{mdframed}[style=caution]
$\frac{d}{dx} x^2 = 2x$
\end{mdframed}

\end{frame}

\end{document}


I am not sure if mdframed is capable of drawing a tight box. If it is, I would appreciate any pointer to the right incantation. If not, I am in need of an alternative. I have looked at the empheq package, which allows a custom "box command" to be specified, but I am at a loss about how to translate my fancy border code into the form it wants.

Additionally, I'd like for it to be possible for the box to be embedded in a line of text, like:

\begin{frame}{testing}
My cool equation is \begin{foo}$\frac{d}{dx} x^2 = 2x$\end{foo}, isn't it neat?
\end{frame}

-
A quick experiment shows that this is nothing to do with beamer or maths but that mdframed environments are full width. (I've no idea if it is possible to make them not full width, though). –  Loop Space Mar 20 '12 at 19:36
Without changing the margins it is not possible. By the way. Why do you need the package mdframed? Instead you can simple use a tikz picture with a \node. –  Marco Daniel Mar 20 '12 at 19:39
I tried that as well - however, it seems to be difficult to draw a node with a fancy border. The tikz manual suggests that doing so is not a simple matter, so I did not pursue it. –  GRedner Mar 20 '12 at 19:44

I can provide a solution which uses the packages environ and varwidth. Putting the mdframed into a minipage prevents it from taking up the whole line, so it can be embedded into other text.

\documentclass{beamer}

\mode<presentation>
\usepackage{calc}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

% Caution-tape frame style
\mdfdefinestyle{caution}{linewidth=10pt,
tikzsetting={decorate,decoration={markings,
mark=between positions 0 and 1 step 6pt
with {
\fill[yellow] (-3pt,-2pt) -- (0pt,-2pt) -- (3pt,2pt) -- (0pt,2pt) -- (-3pt,-2pt);
\fill[black] (0pt,-2pt) -- (3pt,-2pt) -- (6pt,2pt) -- (3pt,2pt) -- (0pt,-2pt);
}}},
innerleftmargin=5pt,innerrightmargin=5pt,align=center,nobreak,
}

\usepackage{environ,varwidth}
\newsavebox\MyTempBox
\NewEnviron{caution}{%
\savebox\MyTempBox{%
\begin{varwidth}{\linewidth}
\BODY
\end{varwidth}}%
\begin{minipage}{\dimexpr\wd\MyTempBox+12pt\relax}
\begin{mdframed}[style=caution,userdefinedwidth=\dimexpr\wd\MyTempBox+12pt\relax]
\BODY
\end{mdframed}%
\end{minipage}
}%

\begin{document}

\begin{frame}{testing}
My cool equation is \begin{caution}$\frac{d}{dx} x^2 = 2x$\end{caution}, isn't it neat?
\end{frame}

\end{document}

-
Thanks - that's definitely closer to what I'm looking for, but I was hoping for a construct that could be embedded in a line of text. I'll edit the original question to make it clear what I'm looking for. –  GRedner Mar 21 '12 at 1:43
Aha, I figured out a fix. Putting the mdframed inside a minipage of the same size appears to work! –  GRedner Mar 21 '12 at 2:22
@GRedner: Thanks. –  Marco Daniel Mar 21 '12 at 10:29