3

I have defined listings. How to create a list of listings? I mean something similar to \listoftables. Thank you

\documentclass[12pt,a4paper]{report}   
\usepackage{xcolor}
\usepackage{listings}
\usepackage{caption}

\usepackage{tcolorbox}
\tcbuselibrary{breakable}
\tcbuselibrary{listings}
\newtcbinputlisting[auto counter]{\mylisting}[2][]{listing file={#2},title=Listing,colback=white,colframe=gray!75!black,fonttitle=\bfseries,listing only,breakable,title=Soubor \thetcbcounter: #1}

\renewcommand{\lstlistingname}{Soubor}

\newcommand{\listingsfont}{\ttfamily}   

\begin{document}
\lstinputlisting{code.txt}    
    \end{document}
6

Here is the tcolorbox - way to generate list of 'something', here list of listings with the settings list type=... and list inside.

The title=... is used by default for inserting the name of the entry.

\tbclistof[\chapter*]{lol}{\lstlistingname} is the tcolorbox - way of saying \listof....

\documentclass[12pt,a4paper]{report}   
\usepackage{xcolor}
%\usepackage{listings}
\usepackage{caption}

\usepackage[most]{tcolorbox}
\newtcbinputlisting[auto counter,list inside=lol,list type={lstlisting}]{\mylisting}[2][]{%
  listing file={#2},
  title=Listing,
  colback=white,
  colframe=gray!75!black,
  fonttitle=\bfseries,
  listing only,
  breakable,
  title=Soubor \thetcbcounter: #2,
  #1
}


\renewcommand{\lstlistingname}{Soubor}

\newcommand{\listingsfont}{\ttfamily}   


\begin{document}
\tcblistof[\chapter*]{lol}{\lstlistingname}
\clearpage
\mylisting{helloworldexample.c}
\end{document}

enter image description here enter image description here

The helloworldexample.c file follows

#include<stdio.h>

int main(int argc,char **argv)
{
  printf("Hello World!\n");
  return(0);
}

Update

\documentclass[12pt,a4paper]{report}   
\usepackage{xcolor}
%\usepackage{listings}
\usepackage{caption}

\usepackage[most]{tcolorbox}
\newtcbinputlisting[auto counter,list inside=lol,list type={lstlisting}]{\mylisting}[3][]{%
  listing file={#3},
  title=Listing,
  colback=white,
  colframe=gray!75!black,
  fonttitle=\bfseries,
  listing only,
  breakable,
  title={Soubor \thetcbcounter: #2},
  #1
}


\renewcommand{\lstlistingname}{Soubor}

\newcommand{\listingsfont}{\ttfamily}   


\begin{document}
\tcblistof[\chapter*]{lol}{\lstlistingname}
\clearpage
\mylisting{Some caption}{helloworldexample.c}
\end{document}
  • Thank you and what is this ! Missing $ inserted.<inserted text>$ ... file.txt}}{27}{tcb@cnt@mylisting.1} ? There should be some $? – Elisabeth Jan 3 at 5:37
  • @Elisabeth: Sorry, I don't understand. My example runs without problems, having the newest TL 2018 installed, i.e. updated daily – user31729 Jan 3 at 9:21
  • 1
    \mylisting[title={Vstupn...},label=input]{input.txt}. Vstupni is no parameter and tcolorbox would complain. Please read the manual!!!! – user31729 Jan 3 at 20:00
  • 1
    @Elisabeth: \mylisting[label=somelabel]{Some caption}{helloworldexample.c}. -- It's all described in the manual :-( – user31729 Jan 3 at 21:31
  • 1
    @Elisabeth: Of course you can not use both \tcblistof and \lstlistoflistings (order does not matter). Regardless which comes first, it will close the .lol file and rewrite it, leaving it empty. The second macro will then open an empty file just print the heading... – user31729 Jan 3 at 22:14
5

You can use \lstlistoflisting that is introduced by the listings package:

 \documentclass[12pt,a4paper]{report}   
\usepackage{xcolor}
\usepackage{listings}
\usepackage{caption}

\usepackage{tcolorbox}
\tcbuselibrary{breakable}
\tcbuselibrary{listings}
\newtcbinputlisting[auto counter]{\mylisting}[2][]{listing file={#2},title=Listing,colback=white,colframe=gray!75!black,fonttitle=\bfseries,listing only,breakable,title=Soubor \thetcbcounter: #1}

\renewcommand{\lstlistingname}{Soubor}

\newcommand{\listingsfont}{\ttfamily}   

\begin{document}
\lstlistoflistings 
\begin{lstlisting}[caption=test]
content of the listing
\end{lstlisting} 
\end{document}

enter image description here

From the listings manual:

\lstlistoflistings prints a list of listings. Each entry is with descending priority either the short caption, the caption, the file name or the name of the listing, [...]

If you want to change the heading of the list of listings from 'Listings' to something else, you can use \renewcommand{\lstlistlistingname}{<new title>}

  • Thank you very much and how to set an another name instead of "Listings" . – Elisabeth Jan 2 at 22:01
  • Btw, \lstinputlisting takes caption as an argument: \lstinputlisting[caption=test]{code.txt} – DG' Jan 2 at 22:03
  • @Elisabeth: Regarding the different title for the list of listings, please see the last sentence of my answer. – leandriis Jan 2 at 22:04
  • Thank you. I don't know where is mistake but I have only the New title (Listings) in the final file. When I compile your code separately it works well. Is it problem that codes are in \include{kap00}? – Elisabeth Jan 2 at 22:08

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.