1

I have two convolved questions related to mdframed.

1) I have three types of boxes: Cautions, Recommendations, and Tips. I would like to add a List of Cautions, List of Recommendations, and List of Tips in the Table of Contents. Following this example, I can get a List of Boxes, that has the Cautions, Recommendations, and Tips in one list. Is there a way to separate the List of Boxes into three separate lists?

2) Sometimes, I have Cautions with associated Recommendations. In this case, I would like to keep the Recommendation in the same box as the Caution, using subtitles in mdframed. However, I would like the “Caution” to be numbered with the stand-alone cautions. (The Recommendation does not need to be numbered.) Is there a way to do this? The MWE I have now does not number the Cautions that are attached to a Recommendation.

\documentclass{book}
    \usepackage{lipsum}
    \usepackage[english]{babel}


    \newcounter{TipBox}[chapter]
    \newcounter{RecBox}[chapter]
    \newcounter{CatBox}[chapter]

    \renewcommand{\theTipBox}{\thechapter.\arabic{TipBox}}
    \renewcommand{\theRecBox}{\thechapter.\arabic{RecBox}}
    \renewcommand{\theCatBox}{\thechapter.\arabic{CatBox}}


    \usepackage[framemethod=tikz]{mdframed}

    \makeatletter
    \tikzset{
    mdfsubtitlebackground/.style={
        draw=none,
        fill=\mdf@subtitlebackgroundcolor}
    }
    \makeatother

    \definecolor{LightRed}{rgb}{0.784,0.537,0.714}
    \definecolor{LightTeal}{rgb}{0.53,.745,.745}

    % Tip boxes
    \newenvironment{TipBox}[1][]{%
        \refstepcounter{TipBox}
        \begin{mdframedtoc}[%
            frametitle={Tip \theTipBox\ #1},
            backgroundcolor=gray!15,
            roundcorner=5pt,
            linecolor=teal,
            linewidth=2pt,
            middlelinecolor=white,
            frametitlebackgroundcolor=LightTeal,
            frametitlefontcolor=white,
            ]%
        }{%
        \end{mdframedtoc}
    }

    % Recommendation boxes
    \newenvironment{RecBox}[1][]{%
        \refstepcounter{RecBox}
        \begin{mdframedtoc}[%
            frametitle={Recommendation \theRecBox\ #1},
            backgroundcolor=gray!15,
            roundcorner=5pt,
            linecolor=teal,
            linewidth=2pt,
            middlelinecolor=white,
            frametitlebackgroundcolor=gray!80,
            frametitlefontcolor=white,
            ]%
        }{%
        \end{mdframedtoc}
    }

    % Caution boxes
    \newenvironment{CatBox}[1][]{%
        \refstepcounter{CatBox}
        \begin{mdframedtoc}[%
            frametitle={Caution \theCatBox\ #1},
            backgroundcolor=gray!15,
            roundcorner=5pt,
            linecolor=teal,
            linewidth=2pt,
            middlelinecolor=white,
            frametitlebackgroundcolor=LightRed,
            frametitlefontcolor=white,
            ]%
        }{%
        \end{mdframedtoc}
    }

% Caution plus recommendation boxes
\newmdenv[%
    roundcorner=5pt,
    subtitlebelowline=false,
        subtitleaboveline=false,
    subtitlebackgroundcolor=gray!80,
        %subtitlefontcolor=white,
    backgroundcolor=gray!15,
    frametitle={Caution},
        frametitlerule=true,
    frametitlebackgroundcolor=LightRed,
        frametitlefontcolor=white,
        linewidth=2pt,
        middlelinecolor=white,
        linecolor=gray!15,
        %innerlinewidth=0pt
]{CatRecBox}


\makeatletter
\newmdenv[startinnercode=\addcontentsline{mdbox}{subsection}\mdf@frametitle]
{mdframedtoc}
\newcommand{\listofboxes}{%
 \section*{List of Boxes}
 \@starttoc{mdbox}%
}
\makeatother


%\newcommand{\listCautions}{List of Cautions}
%\newlistof{caution}{mdboxCat}{\listCautions}


\begin{document}

\tableofcontents
\listofboxes

\chapter{Stuff}

\lipsum[65]

\begin{TipBox}
Here's a tip.
  \label{ibx:Tip1}
\end{TipBox}


\begin{RecBox}
Here, we have a stand-alone recommendation.
  \label{ibx:Rec1}
\end{RecBox}

\begin{RecBox}
Here's another stand-alone recommendation.
  \label{ibx:Rec2}
\end{RecBox}

\begin{CatBox}
Here is a stand-alone caution.
 \label{ibx:Cat1}
\end{CatBox}

\begin{CatRecBox}
Here's a caution that is associated with a specific recommendation. 
\mdfsubtitle{\textcolor[rgb]{1,1,1}{Recommendation}}
And here's the associated recommendation \ldots
\label{ibx:CatRec1}
\end{CatRecBox}


\begin{TipBox}
Another tip
 \label{ibx:anothertip}
\end{TipBox}


\chapter{Other Stuff}

\begin{TipBox}
Here's a tip in the next chapter.
  \label{ibx:tipCh2}
\end{TipBox}

\begin{CatRecBox}
Here's another caution that is associated with a specific recommendation. 
\mdfsubtitle{\textcolor[rgb]{1,1,1}{Recommendation}}
And here's the associated recommendation \ldots
\label{ibx:CatRec2}
\end{CatRecBox}

\begin{RecBox}
Here's another stand-alone recommendation in Chapter 2
  \label{ibx:RecCh2}
\end{RecBox}

\begin{CatBox}
Here is a stand-alone caution in Chapter 2
 \label{ibx:CatCh2}
\end{CatBox}

\end{document}
  • 2
    Use tcolorbox instead of mdframed. It has much better options for this type of questions. – Ulrike Fischer Aug 13 '18 at 19:04
  • For a List of Tips etc. to be useful, wouldn't it be better to give the tips/recommendations/cautions informative titles instead of Tip 1.1 etc? – Marijn Aug 13 '18 at 21:13
1

The solution from the other question can be extended to include different types of boxes. Instead of defining a single newmdenv called mdframedtoc, you can define three different environments with corresponding contentsline, listof and \@starttoc arguments. For the Caution plus Recommendation case you can define a \newenvironment{CatRecBox} similar to the other box environments, instead of a newmdenv. For this environment you can use the same counter and identifier as for the Caution boxes, in order to show them in the Table of Contents (and numbered in the main text).

MWE (note that I changed the List of to \subsection* to make the list titles a bit smaller):

\documentclass{book}
\usepackage{lipsum}
\usepackage[english]{babel}


\newcounter{TipBox}[chapter]
\newcounter{RecBox}[chapter]
\newcounter{CatBox}[chapter]

\renewcommand{\theTipBox}{\thechapter.\arabic{TipBox}}
\renewcommand{\theRecBox}{\thechapter.\arabic{RecBox}}
\renewcommand{\theCatBox}{\thechapter.\arabic{CatBox}}


\usepackage[framemethod=tikz]{mdframed}

\makeatletter
\tikzset{
mdfsubtitlebackground/.style={
  draw=none,
  fill=\mdf@subtitlebackgroundcolor}
}
\makeatother

\definecolor{LightRed}{rgb}{0.784,0.537,0.714}
\definecolor{LightTeal}{rgb}{0.53,.745,.745}

% Tip boxes
\newenvironment{TipBox}[1][]{%
  \refstepcounter{TipBox}
  \begin{mdframedtiptoc}[%
      frametitle={Tip \theTipBox\ #1},
      backgroundcolor=gray!15,
      roundcorner=5pt,
      linecolor=teal,
      linewidth=2pt,
      middlelinecolor=white,
      frametitlebackgroundcolor=LightTeal,
      frametitlefontcolor=white,
      ]%
  }{%
  \end{mdframedtiptoc}
}

% Recommendation boxes
\newenvironment{RecBox}[1][]{%
  \refstepcounter{RecBox}
  \begin{mdframedrectoc}[%
      frametitle={Recommendation \theRecBox\ #1},
      backgroundcolor=gray!15,
      roundcorner=5pt,
      linecolor=teal,
      linewidth=2pt,
      middlelinecolor=white,
      frametitlebackgroundcolor=gray!80,
      frametitlefontcolor=white,
      ]%
  }{%
  \end{mdframedrectoc}
}

% Caution boxes
\newenvironment{CatBox}[1][]{%
  \refstepcounter{CatBox}
  \begin{mdframedcauttoc}[%
      frametitle={Caution \theCatBox\ #1},
      backgroundcolor=gray!15,
      roundcorner=5pt,
      linecolor=teal,
      linewidth=2pt,
      middlelinecolor=white,
      frametitlebackgroundcolor=LightRed,
      frametitlefontcolor=white,
      ]%
  }{%
  \end{mdframedcauttoc}
}

% Caution plus recommendation boxes
\newenvironment{CatRecBox}[1][]{%
  \refstepcounter{CatBox}
  \begin{mdframedcauttoc}[%
  frametitle={Caution \theCatBox\ #1},
  roundcorner=5pt,
  subtitlebelowline=false,
  subtitleaboveline=false,
  subtitlebackgroundcolor=gray!80,
  %subtitlefontcolor=white,
  backgroundcolor=gray!15,
  frametitlerule=true,
  frametitlebackgroundcolor=LightRed,
  frametitlefontcolor=white,
  linewidth=2pt,
  middlelinecolor=white,
  linecolor=gray!15,
  %innerlinewidth=0pt
     ]%
  }{%
  \end{mdframedcauttoc}
}

\makeatletter
\newmdenv[startinnercode=\addcontentsline{mdtipbox}{subsection}\mdf@frametitle]
{mdframedtiptoc}
\newcommand{\listoftips}{%
\subsection*{List of Tips}
\@starttoc{mdtipbox}%
}
\newmdenv[startinnercode=\addcontentsline{mdrecbox}{subsection}\mdf@frametitle]
{mdframedrectoc}
\newcommand{\listofrecs}{%
\subsection*{List of Recommendations}
\@starttoc{mdrecbox}%
}
\newmdenv[startinnercode=\addcontentsline{mdcautbox}{subsection}\mdf@frametitle]
{mdframedcauttoc}
\newcommand{\listofcauts}{%
\subsection*{List of Cautions}
\@starttoc{mdcautbox}%
}
\makeatother


%\newcommand{\listCautions}{List of Cautions}
%\newlistof{caution}{mdboxCat}{\listCautions}


\begin{document}

\tableofcontents
\listoftips
\listofrecs
\listofcauts

\chapter{Stuff}

\lipsum[65]

\begin{TipBox}
Here's a tip.
\label{ibx:Tip1}
\end{TipBox}


\begin{RecBox}
Here, we have a stand-alone recommendation.
\label{ibx:Rec1}
\end{RecBox}

\begin{RecBox}
Here's another stand-alone recommendation.
\label{ibx:Rec2}
\end{RecBox}

\begin{CatBox}
Here is a stand-alone caution.
\label{ibx:Cat1}
\end{CatBox}

\begin{CatRecBox}
Here's a caution that is associated with a specific recommendation. 
\mdfsubtitle{\textcolor[rgb]{1,1,1}{Recommendation}}
And here's the associated recommendation \ldots
\label{ibx:CatRec1}
\end{CatRecBox}


\begin{TipBox}
Another tip
\label{ibx:anothertip}
\end{TipBox}


\chapter{Other Stuff}

\begin{TipBox}
Here's a tip in the next chapter.
\label{ibx:tipCh2}
\end{TipBox}

\begin{CatRecBox}
Here's another caution that is associated with a specific recommendation. 
\mdfsubtitle{\textcolor[rgb]{1,1,1}{Recommendation}}
And here's the associated recommendation \ldots
\label{ibx:CatRec2}
\end{CatRecBox}

\begin{RecBox}
Here's another stand-alone recommendation in Chapter 2
\label{ibx:RecCh2}
\end{RecBox}

\begin{CatBox}
Here is a stand-alone caution in Chapter 2
\label{ibx:CatCh2}
\end{CatBox}

\end{document}

Result:

enter image description here

Note that this can probably be automated a bit more to reduce code duplication.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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