[Tex/LaTex] tcolorbox – list of listings

listingstable of contentstcolorbox

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}
public class Main
{
    public Main(String args[])
    {
        // Java logic in listing 1
    }
}
\end{lstlisting}
%\label{code:Listing-1}

\begin{lstlisting}
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

Best Answer

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}
Related Question