# Memoir boxed example environment

Memoir's manual has this example of frame with title environment. I was wondering how can I add a counter to it? Say, to mimic the AMS theorem environments. Any better way of achieving this in memoir is also welcomed.

 \documentclass[12pt,twoside,openright,a4paper]{memoir}
\newcommand{\FrameTitle}[2]{%
\fboxrule=\FrameRule \fboxsep=\FrameSep
\fbox{\vbox{\nobreak \vskip -0.7\FrameSep
\rlap{\strut#1}\nobreak\nointerlineskip% left justified
\vskip 0.7\FrameSep
\hbox{#2}}}}
\newenvironment{framewithtitle}[2][\FrameFirst@Lab\ (cont.)]{%
\def\FrameFirst@Lab{\textbf{#2}}%
\def\FrameCont@Lab{\textbf{#1}}%
\def\FrameCommand##1{%
\FrameTitle{\FrameFirst@Lab}{##1}}%
\def\FirstFrameCommand##1{%
\FrameTitle{\FrameFirst@Lab}{##1}}%
\def\MidFrameCommand##1{%
\FrameTitle{\FrameCont@Lab}{##1}}%
\def\LastFrameCommand##1{%
\FrameTitle{\FrameCont@Lab}{##1}}%
{\endMakeFramed}
\begin{document}
\begin{framewithtitle}{Test} This is a test.
\end{framewithtitle}
\end{document}

-
Can you turn this into a compilable example rather than a code fragment? It would be much easier for people to play with your code. –  Alan Munn Aug 8 at 17:22
@AlanMunn: Done. –  Sergio Parreiras Aug 8 at 17:25
For a solution, perhaps the mdframed package would be helpful. It's designed to do this sort of thing out of the box (no pun intended). See e.g. TikZ -The pretty boxes to frame the theorems-lemma-proposition-etc (and I'm sure there are other examples on the site.) –  Alan Munn Aug 8 at 17:26

I'd suggest you to use either the mdframed or the tcolorbox packages; both offer you an easy key-value interface to define decorated theorem-like structures that will automatically include numeration (and easy control over the counter used).

In the following example code I show a comparison between the environment that Herbert provided in his answer with five other environments built using the packages mentioned above. Using those packages you can easily produce as elaborate decorations as you wish (in my example I restricted to somehow "sober" structures, but you can easily experiment with different styles):

The code:

 \documentclass[12pt,twoside,openright,a4paper]{memoir}
\usepackage[framemethod=tikz]{mdframed}
\usepackage[many]{tcolorbox}
\usepackage{amsthm}

\newtcbtheorem[number within=chapter]{testiv}{Test}{
outer arc=0pt,arc=0pt}{tha}
\newtcbtheorem[number within=chapter]{testv}{Test}{
outer arc=0pt,
arc=0pt,
colback=white,
colframe=black,
colbacktitle=white,
titlerule=0pt,
fonttitle=\normalcolor\itshape}{thb}

\newmdtheoremenv{testi}{Test}[chapter]
\newmdtheoremenv[
backgroundcolor=gray!50
]{testii}{Test}[chapter]
\theoremstyle{remark}
\newmdtheoremenv{testiii}{Test}[chapter]

\newcounter{FrameCnt} \setcounter{FrameCnt}{0}
\newcommand{\FrameTitle}[2]{%
\fboxrule=\FrameRule \fboxsep=\FrameSep
\fbox{\vbox{\nobreak \vskip -0.7\FrameSep
\rlap{\strut#1}\nobreak\nointerlineskip% left justified
\vskip 0.7\FrameSep
\hbox{#2}}}}
\newenvironment{framewithtitle}[2][\FrameFirst@Lab\ (cont.)]
{\refstepcounter{FrameCnt}%
\def\FrameFirst@Lab{\textbf{#2~\theFrameCnt}}%
\def\FrameCont@Lab{\textbf{#1}}%
\def\FrameCommand##1{\FrameTitle{\FrameFirst@Lab}{##1}}%
\def\FirstFrameCommand##1{\FrameTitle{\FrameFirst@Lab}{##1}}%
\def\MidFrameCommand##1{\FrameTitle{\FrameCont@Lab}{##1}}%
\def\LastFrameCommand##1{\FrameTitle{\FrameCont@Lab}{##1}}%
{\endMakeFramed}

\begin{document}
\chapter{Test chapter}

\begin{framewithtitle}{Test}
This is a test.
\end{framewithtitle}

\noindent Using \texttt{mdframed}:
\begin{testi}
This is a test.
\end{testi}

\begin{testii}
This is a test.
\end{testii}

\begin{testiii}
This is a test.
\end{testiii}

\noindent Using \texttt{tcolrbox}:
\begin{testiv}{}{}
This is a test.
\end{testiv}

\begin{testv}{}{}
This is a test.
\end{testv}

\end{document}


As Alan has mentioned in his comment, more colorful options can be found in the answers to TikZ -The pretty boxes to frame the theorems-lemma-proposition-etc and i many other answers in this site.

-
Why is there such an uneven vertical spacing in the mdframed examples? –  cgnieder Aug 8 at 19:12
\documentclass[12pt,twoside,openright,a4paper]{memoir}
\newcounter{FrameCnt} \setcounter{FrameCnt}{0}
\newcommand{\FrameTitle}[2]{%
\fboxrule=\FrameRule \fboxsep=\FrameSep
\fbox{\vbox{\nobreak \vskip -0.7\FrameSep
\rlap{\strut#1}\nobreak\nointerlineskip% left justified
\vskip 0.7\FrameSep
\hbox{#2}}}}
\newenvironment{framewithtitle}[2][\FrameFirst@Lab\ (cont.)]
{\refstepcounter{FrameCnt}%
\def\FrameFirst@Lab{\textbf{#2~\theFrameCnt}}%
\def\FrameCont@Lab{\textbf{#1}}%
\def\FrameCommand##1{\FrameTitle{\FrameFirst@Lab}{##1}}%
\def\FirstFrameCommand##1{\FrameTitle{\FrameFirst@Lab}{##1}}%
\def\MidFrameCommand##1{\FrameTitle{\FrameCont@Lab}{##1}}%
\def\LastFrameCommand##1{\FrameTitle{\FrameCont@Lab}{##1}}%