5

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}}%
\MakeFramed{\advance\hsize-\width \FrameRestore}}%
{\endMakeFramed}
\begin{document}
\begin{framewithtitle}{Test} This is a test.
\end{framewithtitle}
 \end{document}
Sergio Parreiras
  • 1,786
  • 1
  • 15
  • 32

2 Answers2

6

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):

enter image description here

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}}%
    \MakeFramed{\advance\hsize-\width \FrameRestore}}
  {\endMakeFramed}

\begin{document}
\chapter{Test chapter}

Using your environmet:
\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.

Gonzalo Medina
  • 505,128
3
\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}}%
    \MakeFramed{\advance\hsize-\width \FrameRestore}}
  {\endMakeFramed}
\begin{document}
\begin{framewithtitle}{Test\label{foo}} This is a test.
\end{framewithtitle}

As seen in Frame \ref{foo} \ldots
\end{document} 

enter image description here