mdframed – How to Create a List for Mdframed Example Boxes

countersenvironmentsexamplesmdframed

So i found the question Make an Example and shaded box like in the book which fitted to my desires.

I modified the box and counter so it fits to my whishes. (first number is the chapter second number is the example numbe. Is this chapterwise?!)

%example Box
\usepackage[framemethod=TikZ]{mdframed}
\usepackage{xcolor}
% EXAMPLES
%% set the counter for your environment
\newcounter{example}
\renewcommand{\theexample}{\thechapter.\arabic{example}}

%% set up the examplebox
\newmdenv[%
    backgroundcolor=black!2,
    linecolor=black,
    outerlinewidth=1pt,
    roundcorner=2mm,
    skipabove=\baselineskip,
    skipbelow=\baselineskip,
    settings={\global\refstepcounter{example}},
    frametitlefont={\bfseries Beispiel~\theexample\quad},
]{example}

With the simple usage of:

\begin{example}[frametitle=Wie wird ein Ausführungsgraph erstellt]
select * from database where name='somename' and age>10
\end{example}

enter image description here

The question now is how i can get some kind of list of examples like list of figures? Moreover maybe some way to autolabel it?

Best Answer

You can use tocbasic from KOMA-Script, like this:

\documentclass{scrartcl}

\usepackage[framemethod=TikZ]{mdframed}
\usepackage{xcolor}

% EXAMPLES
%% set the counter for your environment
\newcounter{example}
\renewcommand{\theexample}{\thesection.\arabic{example}}

%% define the style
\mdfdefinestyle{example}{%
    backgroundcolor=black!2,
    linecolor=black,
    outerlinewidth=1pt,
    roundcorner=2mm,
    skipabove=\baselineskip,
    skipbelow=\baselineskip,
}
%% setup the environments
\makeatletter
   %%% with number
   \newmdenv[%
       style=example,
       settings={%
          \global\refstepcounter{example}
          \addxcontentsline{loe}{example}[\theexample]{\mdf@frametitle}
       },
       frametitlefont={\bfseries Example~\theexample\quad},
   ]{example}
   %%% without number (starred version)
   \newmdenv[%
       style=example,
%% uncomment the following three lines to get unnumberd examples in the list
%       settings={%
%          \addxcontentsline{loe}{example}{\mdf@frametitle}
%       },
       frametitlefont={\bfseries Example~\quad},
   ]{example*}
\makeatother

% LIST: loe = list of examples
% requieres tocbasic.sty which is loaded by scrreprt
%% setup new TOC
\addtotoclist{loe}
%% set command for headline
\newcommand{\listofloename}{List of Examples}
%% set up diplay in list like for figures
\makeatletter
   \newcommand{\l@example}{\l@figure}
\makeatother
%% decalere new TOC
\setuptoc{loe}{chapteratlist}
%% own command to print list
\newcommand{\listofexamples}{\listoftoc{loe}}

% for testing
\usepackage[english]{babel}
\usepackage{blindtext}

\begin{document}

\listofexamples

\section{Section One}
\blindtext
\begin{example}[frametitle=Some Headlinetext]
    \blindtext
\end{example}
\blindtext
\begin{example*}[frametitle=Foobar baz]
    \blindtext
\end{example*}
\blindtext
\begin{example}
    \blindtext
\end{example}
\blindtext
\end{document}

I added some comments in the code … if you have more questions regarding the implementation let me know …