1

I am trying to build an example of text with numbered boxes, that can also be referenced in the table of contents. This example is a mix of the two questions, one of which asked by me: mdframed boxes in table of content.

It seems to me that I should pass the startinnercode=\addcontentsline{mdbox}{subsection}\mdf@frametitle command in the \newenvironment{infobox}, as an argument of mdframed. But I am unable to make it work. Keeping it separate, either I call it with infobox, and then I have numbered boxes but not in the ToC, or I call it with mdframedtoc, and they are not numbered.

Help appreciated, thanks!

\documentclass{book}
\usepackage{lipsum}
 \usepackage[english]{babel}
  \newcounter{infobox}[chapter]

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


  \usepackage[framemethod=tikz,rightmargin=5,
  leftmargin=5,backgroundcolor=gray!10,
   frametitlerule=true,frametitlebackgroundcolor=gray!30,roundcorner=10pt]
  {mdframed}

\newenvironment{infobox}[1][]{%
 \refstepcounter{infobox}
 \begin{mdframed}[%
    frametitle={Infobox \theinfobox\ #1},

]%
 }{%
  \end{mdframed}
 }


\makeatletter
\newmdenv[startinnercode=\addcontentsline{mdbox}{subsection}\mdf@frametitle]
{mdframedtoc}
\newcommand{\listofboxes}{%
 \section*{Liste des focus}
 \@starttoc{mdbox}%
  }
 \makeatother


  \begin{document}

  \tableofcontents
   \listofboxes

   \chapter{Stuff}

   \lipsum[1]

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

    Infobox \ref{ibx:sausage} says:
      \lipsum[2]
  \chapter{Other Stuff}

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

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

   \lipsum[5]

    \end{document}
1
  • Shouldn't you use \begin{mdframedtoc} etc. within infobox instead of \begin{mdframe} then? If I do so, I get both numbered boxes and entries to the separate Liste de Focus
    – user31729
    Commented Apr 18, 2017 at 17:04

1 Answer 1

1

The mdframedtoc environment should be called within the wrapper infobox, not mdframed. The list entries are generated since mdframedtoc does use an \addcontentsline as start code and they are numbered, because infobox is numbered. The numbering is correct since infobox sets the frametitle with its numbering scheme.

\documentclass{book}
\usepackage{lipsum}
\usepackage[english]{babel}
\newcounter{infobox}[chapter]

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


\usepackage[framemethod=tikz,rightmargin=5,
leftmargin=5,backgroundcolor=gray!10,
frametitlerule=true,frametitlebackgroundcolor=gray!30,roundcorner=10pt]
{mdframed}

\newenvironment{infobox}[1][]{%
  \refstepcounter{infobox}
  \begin{mdframedtoc}[%
    frametitle={Infobox \theinfobox\ #1},
    ]%
  }{%
  \end{mdframedtoc}
}


\makeatletter
\newmdenv[startinnercode=\addcontentsline{mdbox}{subsection}\mdf@frametitle]
{mdframedtoc}
\newcommand{\listofboxes}{%
 \section*{Liste des focus}
 \@starttoc{mdbox}%
}
\makeatother


\begin{document}

\tableofcontents
\listofboxes

\chapter{Stuff}

\lipsum[1]

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


\begin{infobox}[Garam Masala]
 A delicious and important spice mixture from India!
 \label{ibx:garammasala}
\end{infobox}

Infobox \ref{ibx:sausage} says:
\lipsum[2]
\chapter{Other Stuff}

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

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

\lipsum[5]

\end{document}

enter image description here

enter image description here

Update with babel usage

\documentclass{book}
\usepackage{lipsum}
\usepackage[T1]{fontenc}
\usepackage[english,french]{babel}
\newcounter{infobox}[chapter]

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



\usepackage[framemethod=tikz,rightmargin=5,
leftmargin=5,backgroundcolor=gray!10,
frametitlerule=true,frametitlebackgroundcolor=gray!30,roundcorner=10pt]
{mdframed}

\usetikzlibrary{babel}



\newenvironment{infobox}[1][]{%
  \refstepcounter{infobox}
  \begin{mdframedtoc}[%
    frametitle={Infobox \theinfobox\ #1},
    ]%
  }{%
  \end{mdframedtoc}
}


\makeatletter
\newmdenv[startinnercode=\addcontentsline{mdbox}{subsection}\mdf@frametitle]
{mdframedtoc}
\newcommand{\listofboxes}{%
 \section*{Liste des focus}
 \@starttoc{mdbox}%
}
\makeatother


\begin{document}

\tableofcontents
\listofboxes

\chapter{Stuff}

\lipsum[1]

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


\begin{infobox}[Garam Masala]
  A delicious and important spice mixture from India!
  \label{ibx-garammasala}
\end{infobox}


Infobox \ref{ibx-sausage} says:
\lipsum[2]
\chapter{Other Stuff}

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

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

\lipsum[5]

\end{document}
8
  • Thanks!! The example worked perfectly fine for me as well! Unfortunately in my original file I have an error message: ! Argument of \tikz@next has an extra } at the first occurence of infobox. It mus be a conflict with some other setting or package I have; I'll try to sort that out commenting each package out in turn. But thanks a lot! Commented Apr 18, 2017 at 21:23
  • I found out, for some reason it is incompatible with babel using French as first language. I have no idea why... Commented Apr 18, 2017 at 21:38
  • The : used in your labels has a specific meaning in French typography -- either use \shorthands command or change from : to -, for example
    – user31729
    Commented Apr 18, 2017 at 21:54
  • It does not seem to work. But I stop bothering you... Commented Apr 18, 2017 at 23:01
  • @FrancescoSaraceno: See my update. Store it as a new file. There is also \usetikzlibrary{babel}
    – user31729
    Commented Apr 19, 2017 at 4:32

You must log in to answer this question.

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