6

I'm trying to make an mdframed environment for numbered multi-page boxes. The problem I'm having is that I can't seem to make the formatting and the numbering work as I want it to. I've tried the following two approaches.

mdtheorem

The following code gives me the correct labelling behaviour, but I can't use the mdfdefinestyle frametitle commands to make a nice shaded box like in this example. Even worse, it won't split the box over multiple pages!

%% Define box environment as a mock theorem
% Give the box style
\mdfdefinestyle{boxstyle}{%
    linewidth=2pt,%
    innertopmargin=\topskip
}

% Define the theorem style with amsthm
\newtheoremstyle{box}% <name>
{3pt}% <Space above>
{3pt}% <Space below>
{}% <Body font>
{}% <Indent amount>
{\bfseries}% <Theorem head font>
{\\}% <Punctuation after theorem head>
{.5em}% <Space after theorem headi>
{#1 #2: #3}% <Theorem head spec (can be left empty, meaning `normal')>

% Define the new environment
\theoremstyle{box}
\mdtheorem[style=boxstyle]{infobox}{Box}[chapter]


% Example usage: 
%  - xrefs works as expected
%  - formatting not ideal
%  - box won't break across multiple pages
Box~\ref{box:mybox}
\begin{infobox}[Box title]
blah blah
\end{infobox}

newmdenv

Alternatively if I try to define a newmdenv, the numbering doesn't want to work and I can't get the label in the right place. I can't post images but I basically get the counter on Box 1.0 and the xref on Box 1.1, and the "Box 1.0" part is outside of the frame.

\documentclass{book}
\usepackage{xcolor}
\usepackage{mdframed}
\usepackage{blindtext}
\usepackage{color}

%% set the counter for your environment
\newcounter{infobox}[chapter]
\renewcommand{\theinfobox}{\thechapter.\arabic{infobox}}

%% define the style
\mdfdefinestyle{boxstyle}{%
    linewidth=2pt,%
    frametitlerule=true,%
    frametitlebackgroundcolor=gray!20,%
    innertopmargin=\topskip,
}

%% setup the environments
\newmdenv[%
    style=boxstyle,%
    settings={\global\refstepcounter{infobox}},%
    frametitlefont={\bfseries Box~\theinfobox\quad},%
]{infobox}

\begin{document}
\chapter{Section One}
Box~\ref{box:infobox} but compare with \theinfobox
\begin{infobox}[frametitle=Some Headlinetext]
\label{box:infobox}
    \blindtext
\end{infobox}

\end{document}

It seems like I should be able to do this with some version of the second template. After all, a framed box isn't a theorem.

1
  • Can you make your first example a fully compilable document as is already the case for the second. Commented Jan 25, 2012 at 20:46

1 Answer 1

4

This example works for me:

\documentclass{article}
\usepackage{lipsum}

\newcounter{infobox}[section]

\renewcommand{\theinfobox}{\thesection.\arabic{infobox}}

\usepackage[framemethod=TikZ]{mdframed}
\newenvironment{infobox}[1][]{%
    \refstepcounter{infobox}
    \begin{mdframed}[%
        frametitle={Infobox \theinfobox\ #1},
        skipabove=\baselineskip plus 2pt minus 1pt,
        skipbelow=\baselineskip plus 2pt minus 1pt,
        linewidth=0.5pt,
        frametitlerule=true,
        frametitlebackgroundcolor=gray!30
    ]%
}{%
    \end{mdframed}
}


\begin{document}

\section{Stuff}

\lipsum[1]

\begin{infobox}[Sausage]
\lipsum[2]
\label{ibx:sausage}
\end{infobox}

Infobox \ref{ibx:sausage} says:
% 
\lipsum[2]

\begin{infobox}[Fried eggs]
\lipsum[4-5]
\label{ibx:eggs}
\end{infobox}

Infobox \ref{ibx:eggs} says:
% 
\lipsum[4]

\lipsum[5]

\end{document}

The use of \refstepcounter instead of \stepcounter makes the current value of the infobox counter available for referencing in the text.

Note that I used the current github version of mdframed to compile this example. I didn't test whether or not it works with the current ctan version, which is older.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .