The \contentsline
macro should never be used directly in a document. It's LaTeX's duty to write such macro with the appropriate arguments in the .toc
file or other auxiliary files for the various lists.
The instruction for this is \addcontentsline
, which is usually implicit in macros such as \chapter
, \section
, other sectional commands and \caption
.
If you have \addcontentsline{<file>}{<level>}{<text>}
in your document, LaTeX will write in the appropriate file a line of the form
\contentsline{<level>}{<text>}{<page>}
The <type>
can be toc
, lof
or lot
(table of contents, list of figures or list of tables) and corresponds to the extension for the auxiliary file. Note that the page number will be automatically supplied, because the write operation is performed when a page is being shipped out, when the page number is known precisely. Other <file>
types might be available, depending on packages used or on definitions of new lists done in the document (the package of choice is newfloat
).
There is also \addtocontents{<type>}{<text>}
, which will write the raw <text>
in the corresponding file, but this requires precise knowledge about how the file is subsequently used.
One should also remember that <text>
is subject to expansion, so \protect
should be used if some macro is not to be expanded prior to writing.
It's a duty of the document class (or of packages defining new lists) to define the working of \contentsline
: by default, the line
\contentsline{<level>}{<text>}{<page>}
will be transformed into
\l@<level>{<text>}{<page>}
For instance, \contentsline{chapter}{Prefazione}{2}
will become \l@chapter{Prefazione}{2}
and the macro \l@chapter
should be defined by the document class (it isn't in article
).
Note that hyperref
changes \addcontentsline
and \contentsline
to add more arguments for the purpose of hyperlinks.
\contentsline
doesn't setup a line in the content, but is used by the table of content to typeset its contents. To add something to your ToC you should use\addcontentsline
or\addtocontents
(the former being easier to use if you want to add an entry, the latter being for arbitrary stuff).\contentsline
? Write something to the ToC?\contentsline
is don't use\contentsline
yourself. It is only defined as\contentsline=macro: #1->\csname l@#1\endcsname
in all the standard classes. It gets used by the ToC, LoT and LoF.