3

I defined a custom environment that allows me to define mathematical problem statements and puts them in a box. Since I have quite a few of them in my report, I decided to properly caption them, which also allows me to refer back to the problems with cleveref.

Here is my code [1]:

\usepackage{xparse}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{cleveref}

\definecolor{problemgray}{gray}{0.9}

\DeclareCaptionType{problem}[Problem][List of Problems]
\crefname{problem}{Problem}{Problems}
\Crefname{problem}{Problem}{Problems}

\newsavebox{\tmpbox}
\NewDocumentEnvironment{problembox}{mo}{%
    \noindent%
    \begin{lrbox}{\tmpbox}%
        \begin{minipage}{0.9\textwidth}%
            \par\noindent%
        }{%
        \end{minipage}\end{lrbox}%
    \begin{center}%
        \captionof{problem}{#1}%
        \vspace{0.25em}%
        \setlength{\fboxsep}{0.5em}
        \colorbox{problemgray}{\usebox{\tmpbox}}%
        \ifthenelse{\equal{#2}{}}{
            %
        }{
            \label{#2}%
        }
    \end{center}
}

The format is thus: \begin{problembox}{<problem caption>}[<optional label>].

In this way, the problems are currently numbered per chapter, e.g. Problem 2.1, Problem 2.2, etc. However, most of my problems build upon each other.

Thus my question,
How can I get caption to continuously number custom floats rather than per chapter?

[1] Note that I lied: The boxes are actually not floats, but I could make them floats the way I set them up. I just wanted to have a captioned but fixed environment.

Bonus question: How can I allow page breaks within a problem using my code?

Ingo
  • 20,035
  • Instead of using a lrbox use the package framed. – Marco Daniel May 20 '13 at 11:36
  • 2
    I do not see that this is a duplicate because http://tex.stackexchange.com/questions/28333/continuous-v-per-chapter-section-numbering-of-figures-tables-and-other-docume is a question about changing existing counters while in this case the question is "How to define the "problem" counter is a way I want". Defining it wrongly first, and correct it afterwards with the help of an additional package do not look very straight-ahead to me... –  May 20 '13 at 17:10

1 Answers1

7

I know that the linked answered your main question.

  1. Your main question can be answered with the possibilities of the package caption. The command \DeclareCaptionType has an optional argument where you can specify the behaviour of the number. In your case the option within=none is needed:

    \DeclareCaptionType[within=none]{problem}[Problem][List of Problems]
    
  2. If you use a lrbox you can split across pages without special handling. For your case you can use the package framed. The example below demonstrate it.

  3. You are using xparse. To test if a value for an optional argument is given you can use \IfNoValue(TF).

Example:

\documentclass[]{report}
\usepackage{xparse}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{cleveref}
\usepackage{framed}
\definecolor{shadecolor}{gray}{0.9}

\DeclareCaptionType[within=none]{problem}[Problem][List of Problems]
\crefname{problem}{Problem}{Problems}
\Crefname{problem}{Problem}{Problems}
\NewDocumentEnvironment{problembox}{mo}{%
    \begin{trivlist}\item\relax%
        \captionof{problem}{#1}%
         \IfNoValueF { #2 } { \label{#2} }%
         \vspace{0.25em}%
        \setlength{\fboxsep}{0.5em}
        \begin{shaded}
        }{%
        \end{shaded}
    \end{trivlist}
}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{problembox}{caption}
\lipsum
\end{problembox}
\end{document}

Addendum by Axel Sommerfeldt

When \DeclareCaptionType was integrated within the caption package people were complaining that they need to load the caption package to use that functionality. Therefore I outsourced it into the newborn newfloat package, and have removed the documentation of \DeclareCaptionType in the caption package documentation since people were complaining about the length of the caption documentation anyway. (I see that this wasn't the best idea so I will re-add a short reference to the newfloat package in the next version.) So for up-to-date documentation, please take a look at the newfloat package documentation, command \DeclareFloatingEnvironment. (In fact the caption package still offers \DeclareCaptionType to be compatible to existing documents, but simply use \DeclareFloatingEnvironment of the newfloat package internally. This will be done in future versions, too, so using \DeclareCaptionType is still fine.)

Marco Daniel
  • 95,681