0

I want to create a list like toc, lof, lot. Is should be called listofsongs or similar. The question is: Is there any package which allows me to create similar lists or do I have to do this all by myself?

Is there a list of already used file extensions in LaTeX? i.e. I shouldnt use MyDocument.toc because this would cause conflicts with tableofcontents.

In the end, I should be able to write \listofsongs like I write \tableofcontents and it should behave similar.

MaestroGlanz
  • 2,348

2 Answers2

4

You can probably use the LaTeX2e-kernel-macros \@starttoc and \addtocontents{<file-extension>}{<tokens>}.

\addtocontents will write (unexpanded) to the aux-file some directives for unexpanded writing <tokens> into the file \jobname.<file-extension> in case that file is open for input when the aux-file is processed at the end of the LaTeX-run.

\@starttoc{<file-extension>} will make TeX read/process the file \jobname.<file-extension> in case that file exists and then destroy that file and then create it anew and open it for input so that when the aux-file is processed at the end of the LaTeX-run, that file is open for input.

In case you wish to do some more fancy things, have a look at the \addcontentsline-macro of the LaTeX2e-kernel.

Basiscally \addcontentsline{<file-extension>}{<command name>}{<tokens>} will write to the aux-file some directives for unexpanded writing the token-sequence \contentsline{<command name>}{<tokens>}{<page number>} into the file \jobname.<file-extension> in case that file is open for input when the aux-file is processed at the end of the LaTeX-run. After having written \jobname.<file-extension> during processing the aux-file at the end of the LaTeX-run, you will find that directive \contentsline{<command name>}{<tokens>}{<page number>} in it. Thus that directive will be carried out when -- due to \@starttoc -- the file \jobname.<file-extension> is read/processed.

\contentsline{<command name>}{<tokens>}{<page number>}in turn will call a macro \l@<command name> which processes two arguments, namely the <tokens>-argument and the <page number>-argument.

When this mechanism is used with the toc-file and sectioning-commands, <command name> will be something like section or subsection yielding the execution of the macros \l@section or \l@subsection but you can as well define your own \l@..-macros for your own categories of rubrification.

The \addtocontents...-\@starttoc{<file-extension>}-mechanism does beneath other things define a control-sequence \tf@<file-extension>. Therefore for finding out whether that mechanism has alrady allocated a \write-handle for writing a file \jobname.<file-extension>, you can use the \@ifundefined-macro in order to check whether the control-sequence \tf@<file-extension> is already defined.

But this does only work as long as the scrwfile-package is not in use -- by the way: You might be interested in the scrwfile-package in case you intend to create many lists like toc, lof or lot in your document. More information about that package can be found on CTAN: https://www.ctan.org/pkg/scrwfile

The LaTeX2e-kernel is explained and commented in the file source2e.pdf which is available at https://www.ctan.org/pkg/source2e .
The \addtocontents-\@starttoc-mechanism and the \addcontentsline-macro which makes use of that mechanism are explained in the section which is named File F ltsect.dtx → 59 Sectioning commands → 59.3 Table of Contents etc.

\documentclass{article}
\usepackage{hyperref}
\usepackage{verbatim}
\makeatletter
\newcommand\l@songline[2]{%
  \par Now we have data about another song:\\
  Title of song: #1. Song is printed on page: #2.%
}%
\makeatother
\begin{document}
\LaTeX{} will now write directives to aux-file for writing the first
 line into \jobname.weird. These directives will be carried out at
 the end of the \LaTeX-run when the aux-file is read/processed in
 case at that time the file \jobname.weird is open for writing to it.\\
\addtocontents{weird}{First line in file \jobname.weird.}%
\bigskip

This is what \jobname.weird looks like before calling \verb|\@starttoc|:    \\
\verbatiminput{\jobname.weird}
\bigskip

This is how \verb|\l@songline| is defined:\\{%
  \csname verbatim@font\endcsname\selectfont
  \expandafter\meaning\csname l@songline\endcsname
}%
\bigskip

The file \jobname.weird is \csname @ifundefined\endcsname{tf@weird}{not}{already} allocated.
\bigskip

\LaTeX will now read/process \jobname.weird and then destroy that file
and create it anew and open it for writing. Thus at the end of the
\LaTeX-run, when the aux-file is read/processed, that file will be open
for writing to it:\bigskip

\csname @starttoc\endcsname{weird}%
\bigskip

The file \jobname.weird is \csname @ifundefined\endcsname{tf@weird}{not}{already} allocated.
\bigskip

\LaTeX{} will now write directives to aux-file for writing the second
line into \jobname.weird. These directives will be carried out at
the end of the \LaTeX-run when the aux-file is read/processed in
case at that time the file \jobname.weird is open for writing to it.\\
\addtocontents{weird}{Second line in file \jobname.weird.}%
\bigskip

Now two \verb|\addcontentsline|-entries for writing things to \jobname.weird
that need to be "rubrified" by means of applying the \verb|l@songgline|-macro.
\addcontentsline{weird}{songline}{Morning has broken}
\addcontentsline{weird}{songline}{Final Countdown}

\end{document} 
Ulrich Diez
  • 28,770
1

Unfortunaly there is no MWE in the question. So I do not know what you really want to do. Reading your profile I guess that you are using scrbook. So here is a suggestion that needs an uptodate KOMA-Script.

\documentclass[listof=totoc,ngerman]{scrbook}[2016/06/14]% needs at least KOMA-Scritp version 3.21
\usepackage{babel}
\DeclareNewTOC[
  type=song,
  tocentryindent=0pt,
  tocentrynumwidth=2.3em,
  tocentrystyle=tocline,
  tocentrylinefill=\hfill,
  tocentryentryformat=\sffamily,
  tocentrypagenumberformat=\sffamily
]{los}
\newcaptionname{ngerman}{\listsongname}{Liederverzeichnis}

\DeclareNewSectionCommand[
  style=chapter,
  level=0,
  beforeskip=-1sp,
  innerskip=0pt,
  afterskip=\baselineskip,
  font=\usekomafont{section},
  prefixfont=\usekomafont{chapter},
  pagestyle=plain
]{song}

\renewcommand\songformat{}
\renewcommand\addsongtocentry[2]{\addxcontentsline{los}{song}{#2}}

\usepackage{hyperref}

\begin{document}
\tableofcontents
\addchap{Einleitung}
Text
\listofsongs
\song{Ein Liedtitel}
\end{document}

Here is another version using standard class book and KOMA package tocbasic. But note that I do not know how you define your \song command or song environment:

\documentclass[ngerman]{book}
\usepackage{babel}

\usepackage{tocbasic}[2016/06/14]% needs at least KOMA-Scritp version 3.21
\DeclareNewTOC[
  type=song,
  tocentryindent=0pt,
  tocentrynumwidth=2.3em,
  tocentrystyle=tocline,
  tocentrylinefill=\hfill,
  tocentryentryformat=\sffamily,
  tocentrypagenumberformat=\sffamily
]{los}
\newcaptionname{ngerman}{\listsongname}{Liederverzeichnis}

\usepackage{lipsum}
\usepackage{hyperref}

\begin{document}
\listofsongs

\clearpage
\phantomsection\addxcontentsline{los}{song}{Ein Liedtitel im Verzeichnis}
\section*{Ein Liedtitel}

\lipsum

\clearpage
\phantomsection\addxcontentsline{los}{song}{Ein anderer Liedtitel im Verzeichnis}
\section*{Ein anderer Liedtitel}
\end{document}
esdd
  • 85,675
  • In the last \renewcommand: Isnt there an x too much? After \add-x-.... – MaestroGlanz Nov 16 '16 at 09:13
  • If someone doesn't use koma-script. Is there a way to \RequirePackage the necessary parts of the koma-script? – MaestroGlanz Nov 16 '16 at 09:15
  • \addxcontentsline is defined by package tocbasic. It is possible to use tocbasic with a other classes. But I do not know how you insert/define the songs. And note that your question is marked as duplicat. There are many links to other solutions given in comments. – esdd Nov 16 '16 at 10:38