Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
    
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}}%
    \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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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