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.
Best Answer
This example works for me:
The use of
\refstepcounter
instead of\stepcounter
makes the current value of theinfobox
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.