tcolorbox - list of listings?
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}
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.
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}