20

In a book with figures, tables and listings, I figured out that all listings and some tables and figures look better when presented in a tcolorbox. The book has the usual tables of contents:

\tableofcontents{}
\listoffigures{}
\listoftables{}
\lstlistoflistings{}

but tcolorboxes have their own headlines (and may define their own counters), so it seems redundant (and unattractive) to have the usual captions and labels for figures, tables and listings too.

So my question is how to produce the lists of figures/tables/listings the reader expects after the ToC when

  1. tcolorbox is used exclusively?
  2. tcolorbox is used sometimes, while the plain listings package and regular table/figure environments are used in other cases?

EDIT: Here is a MWE (adapted from T.F.Sturm) that has figures, tables and listings as well as a tcolorbox theorem and listing with a label and a counter. How do I make them appear in the ToC section in the beginning of the file?

\documentclass{article}
\usepackage{tikz}
\usepackage{here}
\usepackage[listings,theorems]{tcolorbox}
\tcbset{noparskip}
\begin{document}
\tableofcontents{}
\listoffigures{}
\listoftables{}
\lstlistoflistings{}
\pagebreak{}
%----------------------------------------------------------
\section{Colored boxes}

\begin{tcolorbox}[colback=red!5,colframe=red!75!black]
  My box.
\end{tcolorbox}

\begin{tcolorbox}[colback=blue!5,colframe=blue!75!black,title=My title]
  My box with my title.
\end{tcolorbox}

%----------------------------------------------------------
\section{\LaTeX-Examples}

\begin{tcblisting}{colback=red!5,colframe=red!75!black}
This is a \LaTeX\ example:
$\displaystyle\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}$.
\end{tcblisting}

 %----------------------------------------------------------
\section{Theorems}

\newcounter{mytheorem}[section]
\def\themytheorem{\thesection.\arabic{mytheorem}}

\tcbmaketheorem{theo}{Theorem}{fonttitle=\bfseries\upshape, fontupper=\slshape,
     arc=0mm, colback=blue!5,colframe=blue!75!black}{mytheorem}{theorem}

\begin{theo}{Summation of Numbers}{summation}
  For all natural number $n$ it holds:\\[2mm]
  $\displaystyle\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}$.
\end{theo}

We have given Theorem \ref{theorem:summation} on page \pageref{theorem:summation}.

\pagebreak{}
%----------------------------------------------------------

\section{Tables, Listings and Figures}
%----------------------------------------------------------
This is table 1
%----------------------------------------------------------

\begin{table}[H]
  \centering
  \begin{tabular}{ll}
    Header1 & Header2 \\ \hline
    Cell1 & Cell2 \\
  \end{tabular}
  \caption{My Table No1}
  \label{tab:tab-1}
\end{table}
  %----------------------------------------------------------
This is table 2

 \begin{table}[H]
  \centering
  \begin{tabular}{ll}
    Header3 & Header4 \\ \hline
    Cell3 & Cell4 \\
  \end{tabular}
  \caption{My Table No2}
  \label{tab:tab-1}
\end{table}

%----------------------------------------------------------
This is figure 1
%----------------------------------------------------------
\begin{figure}[H]
  \centering
\begin{tikzpicture}
    \draw (0,0) node {A} --
          (2,0) node {B};
\end{tikzpicture} 
  \caption{Figure No1}
  \label{fig:fig-1}
\end{figure}
%----------------------------------------------------------
This is figure 2
%----------------------------------------------------------
\begin{figure}[H]
  \centering
\begin{tikzpicture}
    \draw (0,0) node {C} --
          (2,0) node {D};
\end{tikzpicture} 
  \caption{Figure No2}
  \label{fig:fig-2}
\end{figure}

\begin{lstlisting}[caption={Java Logic 1},label=java1]
public class Main
{
    public Main(String args[])
    {
        // Java logic in listing 1
    }
}
\end{lstlisting}
%\label{code:Listing-1}

\begin{lstlisting}[caption={Java Logic 2},label=java2]
public class Main
{
    public Main(String args[])
    {
        // More Java logic in listing 2
    }
}
\end{lstlisting}
%\label{code:Listing-2}

\end{document}

Here is the output:
enter image description here enter image description here enter image description here

2
  • Could you clarify what you'd like the toc to look like in your specific example? You don't have to create it using TeX, just type it out in your question :)
    – cmhughes
    Dec 16, 2012 at 17:17
  • Nothing special, the tcolorbox listings should appear in the 'list of listings' just like the two Java listings, and the theorems maybe in a new 'list of theorems' in the same standard format like the 'list of listings'.
    – user17106
    Dec 17, 2012 at 15:28

2 Answers 2

13
+100

In the solution below I've used the \@starttoc command to start a file \jobname.thm

% make a List of Theorems using the 
% \@starttoc command which creates \jobname.thm
\newcommand\listtheoremname{List of Theorems}
\newcommand\listoftheorems{%
  \section*{\listtheoremname}\@starttoc{thm}}

and have then copied the definition of the theorem command from tcbtheorems.code.tex and added

   \addcontentsline{thm}{subsection}{\protect\numberline{\csname the#2\endcsname} #3}, % new bit

which adds the theorem number and description to \jobname.thm

% copied from tcbtheorems.code.tex and added
% \addcontentsline...
\tcbset{
  theorem/.code args={#1#2#3#4}{%
  \refstepcounter{#2}\label{#4}%
   \addcontentsline{thm}{subsection}{\protect\numberline{\csname the#2\endcsname} #3}, % new bit
  \pgfkeysalso{title={\setlength{\hangindent}{\widthof{#1~\csname the#2\endcsname:\ }}\hangafter=1 \mbox{#1~\csname the#2\endcsname:\ }#3}}}%
}

I have also created a addtolol key which stands for add to list of listings

% add tcblistings to \jobname.lol (list of listings)
\tcbset{
  addtolol/.code={\addcontentsline{lol}{subsection}{\kvtcb@title}},
  }

which can be used as a key in any of the tcolorbox and friends environments

\begin{tcolorbox}[colback=blue!5,colframe=blue!75!black,title=My title,addtolol]

One problem with my implementation is that the addtolol key must be placed after the title key; if anyone has suggestions on how to fix this, let me know.

Here's a screenshot from the solution below.

screenshot

Complete MWE

\documentclass{article}
\usepackage{tikz}
\usepackage{float} % here.sty is considered obsolete (according to documentation)
\usepackage[listings,theorems]{tcolorbox}
\tcbset{noparskip}

\makeatletter

% make a List of Theorems using the 
% \@starttoc command which creates \jobname.thm
\newcommand\listtheoremname{List of Theorems}
\newcommand\listoftheorems{%
  \section*{\listtheoremname}\@starttoc{thm}}

% copied from tcbtheorems.code.tex and added
% \addcontentsline...
\tcbset{
  theorem/.code args={#1#2#3#4}{%
  \refstepcounter{#2}\label{#4}%
   \addcontentsline{thm}{subsection}{\protect\numberline{\csname the#2\endcsname} #3}, % new bit
  \pgfkeysalso{title={\setlength{\hangindent}{\widthof{#1~\csname the#2\endcsname:\ }}\hangafter=1 \mbox{#1~\csname the#2\endcsname:\ }#3}}}%
}

% add tcblistings to \jobname.lol (list of listings)
\tcbset{
  addtolol/.code={\addcontentsline{lol}{subsection}{\kvtcb@title}},
  }
\makeatother

% moved new theorem declaration to the preamble
\newcounter{mytheorem}[section]
\def\themytheorem{\thesection.\arabic{mytheorem}}

\tcbmaketheorem{theo}{Theorem}{fonttitle=\bfseries\upshape, fontupper=\slshape,
     arc=0mm, colback=blue!5,colframe=blue!75!black}{mytheorem}{theorem}

\begin{document}
\tableofcontents{}
\listoffigures{}
\listoftables{}
\lstlistoflistings{}
\listoftheorems % new bit
\pagebreak{}
%----------------------------------------------------------
\section{Colored boxes}

\begin{tcolorbox}[colback=red!5,colframe=red!75!black,title=My title]
  My box.
\end{tcolorbox}

\begin{tcolorbox}[colback=blue!5,colframe=blue!75!black,title=My title,addtolol]
  My box with my title.
\end{tcolorbox}

%----------------------------------------------------------
\section{\LaTeX-Examples}

\begin{tcblisting}{colback=red!5,colframe=red!75!black,title=Some title,addtolol}
This is a \LaTeX\ example:
$\displaystyle\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}$.
\end{tcblisting}

 %----------------------------------------------------------
\section{Theorems}


\begin{theo}{Summation of Numbers}{summation}
  For all natural number $n$ it holds:\\[2mm]
  $\displaystyle\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}$.
\end{theo}

We have given Theorem \ref{theorem:summation} on page \pageref{theorem:summation}.

\pagebreak{}
%----------------------------------------------------------

\section{Tables, Listings and Figures}
%----------------------------------------------------------
This is table 1
%----------------------------------------------------------

\begin{table}[H]
  \centering
  \begin{tabular}{ll}
    Header1 & Header2 \\ \hline
    Cell1 & Cell2 \\
  \end{tabular}
  \caption{My Table No1}
  \label{tab:tab-1}
\end{table}
  %----------------------------------------------------------
This is table 2

 \begin{table}[H]
  \centering
  \begin{tabular}{ll}
    Header3 & Header4 \\ \hline
    Cell3 & Cell4 \\
  \end{tabular}
  \caption{My Table No2}
\end{table}

%----------------------------------------------------------
This is figure 1
%----------------------------------------------------------
\begin{figure}[H]
  \centering
\begin{tikzpicture}
    \draw (0,0) node {A} --
          (2,0) node {B};
\end{tikzpicture} 
  \caption{Figure No1}
  \label{fig:fig-1}
\end{figure}
%----------------------------------------------------------
This is figure 2
%----------------------------------------------------------
\begin{figure}[H]
  \centering
\begin{tikzpicture}
    \draw (0,0) node {C} --
          (2,0) node {D};
\end{tikzpicture} 
  \caption{Figure No2}
  \label{fig:fig-2}
\end{figure}

\begin{lstlisting}[caption={Java Logic 1},label=java1]
public class Main
{
    public Main(String args[])
    {
        // Java logic in listing 1
    }
}
\end{lstlisting}
%\label{code:Listing-1}

\begin{lstlisting}[caption={Java Logic 2},label=java2]
public class Main
{
    public Main(String args[])
    {
        // More Java logic in listing 2
    }
}
\end{lstlisting}
%\label{code:Listing-2}

\end{document}
4
  • This is great, exactly what I wanted, thank you very much.
    – user17106
    Dec 18, 2012 at 12:54
  • @user17106 sweet, I'm glad it's worked out :)
    – cmhughes
    Dec 18, 2012 at 17:09
  • @cmhughes I made a copy of your answer and gave a new answer on its base (see my comments there). You also asked about the placement of your new addtolol key; the updated key in my answer can be placed before the title key is given :-) Jul 16, 2013 at 8:45
  • @ThomasF.Sturm that's great, thanks a lot, and thanks for a great package :) +1
    – cmhughes
    Jul 16, 2013 at 9:16
20

Since version 2.40 of tcolorbox (2013/07/15), there is integrated support for lists of boxes, listings, etc. I took the excellent answer of cmhughes and updated it to this recent version. My suggestion is to keep the answer of cmhughes unchanged because it is the correct answer for versions before 2.40 which may be around for quite a long time. Secondly, comparing both answers may help people to adapt older code to the new features, if they want or need to. Therefore, I tried to keep very near the original answer; all code beneath \pagebreak{} is not changed at all.

\documentclass{article}
\usepackage{tikz}
\usepackage{float}
\usepackage[listings,theorems]{tcolorbox}

\makeatletter
% add tcblistings to \jobname.lol (list of listings)
\tcbset{
  addtolol/.style={list entry={\kvtcb@title},add to list={lol}{subsection}},
  }
\makeatother

\newtcbtheorem[auto counter,number within=section,list inside=thm]{theo}{Theorem}%
  {fonttitle=\bfseries\upshape, fontupper=\slshape,
     arc=0mm, colback=blue!5,colframe=blue!75!black}{theorem}

\begin{document}
\tableofcontents{}
\listoffigures{}
\listoftables{}
\lstlistoflistings{}
\tcblistof[\section*]{thm}{List of Theorems}
\pagebreak{}
%----------------------------------------------------------
\section{Colored boxes}

\begin{tcolorbox}[colback=red!5,colframe=red!75!black,title=My title]
  My box.
\end{tcolorbox}

\begin{tcolorbox}[colback=blue!5,colframe=blue!75!black,title=My title,addtolol]
  My box with my title.
\end{tcolorbox}

%----------------------------------------------------------
\section{\LaTeX-Examples}

\begin{tcblisting}{colback=red!5,colframe=red!75!black,title=Some title,addtolol}
This is a \LaTeX\ example:
$\displaystyle\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}$.
\end{tcblisting}

 %----------------------------------------------------------
\section{Theorems}


\begin{theo}{Summation of Numbers}{summation}
  For all natural number $n$ it holds:\\[2mm]
  $\displaystyle\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}$.
\end{theo}

We have given Theorem \ref{theorem:summation} on page \pageref{theorem:summation}.

\pagebreak{}
%----------------------------------------------------------

\section{Tables, Listings and Figures}
%----------------------------------------------------------
This is table 1
%----------------------------------------------------------

\begin{table}[H]
  \centering
  \begin{tabular}{ll}
    Header1 & Header2 \\ \hline
    Cell1 & Cell2 \\
  \end{tabular}
  \caption{My Table No1}
  \label{tab:tab-1}
\end{table}
  %----------------------------------------------------------
This is table 2

 \begin{table}[H]
  \centering
  \begin{tabular}{ll}
    Header3 & Header4 \\ \hline
    Cell3 & Cell4 \\
  \end{tabular}
  \caption{My Table No2}
\end{table}

%----------------------------------------------------------
This is figure 1
%----------------------------------------------------------
\begin{figure}[H]
  \centering
\begin{tikzpicture}
    \draw (0,0) node {A} --
          (2,0) node {B};
\end{tikzpicture}
  \caption{Figure No1}
  \label{fig:fig-1}
\end{figure}
%----------------------------------------------------------
This is figure 2
%----------------------------------------------------------
\begin{figure}[H]
  \centering
\begin{tikzpicture}
    \draw (0,0) node {C} --
          (2,0) node {D};
\end{tikzpicture}
  \caption{Figure No2}
  \label{fig:fig-2}
\end{figure}

\begin{lstlisting}[caption={Java Logic 1},label=java1]
public class Main
{
    public Main(String args[])
    {
        // Java logic in listing 1
    }
}
\end{lstlisting}
%\label{code:Listing-1}

\begin{lstlisting}[caption={Java Logic 2},label=java2]
public class Main
{
    public Main(String args[])
    {
        // More Java logic in listing 2
    }
}
\end{lstlisting}
%\label{code:Listing-2}

\end{document}

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.