The easiest way to obtain this is to insert your listing directly into a figure
environment:
\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\lstset{% general command to set parameter(s)
basicstyle=\small, % print whole listing small
keywordstyle=\color{black}\bfseries\underbar, % underlined bold black keywords
identifierstyle=, % nothing happens
commentstyle=\color{white}, % white comments
stringstyle=\ttfamily, % typewriter type for strings
showstringspaces=false} % no special string spaces
\begin{document}
\listoffigures
\begin{figure}
\rule{150pt}{100pt}%
\caption{A normal figure}
\end{figure}
\begin{figure}
\begin{lstlisting}
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{lstlisting}
\caption{This is a listing}
\end{figure}
\end{document}
The above listing example was taken from the listings
documentation (section 1.3 Figure out the appearance, p 5).
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}
Best Answer
Here is the
tcolorbox
- way to generate list of 'something', here list of listings with the settingslist type=...
andlist inside
.The
title=...
is used by default for inserting the name of the entry.\tbclistof[\chapter*]{lol}{\lstlistingname}
is thetcolorbox
- way of saying\listof....
The
helloworldexample.c
file followsUpdate