8

Is there a way to make a single page table of contents in this style

Introduction ..................................... 1
Next Point ....................................... 2

Without generating it form a LaTex-Document? I printed a bundle of scripts for programming classes that are not writting in latex but i want to have a clean table of contents with a custom of the pages.

Thank you.

  • Welcome to TeX.SE! – Mico Jan 24 at 0:33
9

The lines of the table of contents are generated using \contentsline. You can also call this macro yourself to create a toc manually. The general syntax is

\contentsline{<chapter/section/subsection/subsubsection>}{<title>}{<page>}

and if you want these entries to be numbered you should insert \numberline{<section number>} at the start of the second argument. To use the chapter level heading you need to use a document class that supports it (like book or report).

Here is an example. I'm numbering only half of the sections to illustrate how this works and what the result looks like.

\documentclass{article}

\begin{document}

\section*{Table of contents}

\contentsline{section}{Introduction}{1}
\contentsline{subsection}{First subsection}{1}
\contentsline{subsection}{Second subsection}{3}
\contentsline{subsection}{Third subsection}{3}

\contentsline{section}{\numberline{2}First real section}{5}
\contentsline{subsection}{\numberline{2.1}First subsection}{5}
\contentsline{subsection}{\numberline{2.2}Second subsection}{6}
\contentsline{subsection}{\numberline{2.3}Third subsection}{8}

\end{document}

output

Here is a version that takes care of the section numbers automatically.

\documentclass{article}

\newcommand*\tocsection[2]{%
  \stepcounter{section}%
  \contentsline{section}{\numberline{\thesection}#1}{#2}%
}
\newcommand*\tocsubsection[2]{%
  \stepcounter{subsection}%
  \contentsline{subsection}{\numberline{\thesubsection}#1}{#2}%
}

\begin{document}

\section*{Table of contents}

\tocsection{Introduction}{1}
\tocsubsection{First subsection}{1}
\tocsubsection{Second subsection}{3}
\tocsubsection{Third subsection}{3}
\tocsection{First real chapter}{5}
\tocsubsection{First subsection}{5}
\tocsubsection{Second subsection}{6}
\tocsubsection{Third subsection}{8}

\end{document}

output

In a document class with actual chapters, \tocchapter could be implemented in the same way.


If you need to customise the appearance of the toc you can use either titletoc or tocloft.

For instance, if you don't want the to be in boldface and you want them to come with (the dots), you could add the following to your preamble:

\usepackage{tocloft}
\renewcommand\cftsecfont{\normalfont}
\renewcommand\cftsecpagefont{\normalfont}
\renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}}

If you only use section level headings, this would match the appearance described in your question.


Remark: \contentsline takes an additional argument if hyperref is loaded (the target for the hyperlink). If someone reading this wants to incorporate the above in a document that uses hyperref, you should add an additional {} after {<page>}.

  • How would you adapt this solution to the OP's apparent objective of listing a bunch of unnumbered entries in the ToC? – Mico Jan 24 at 0:36
  • @Mico: I should've considered that. By leaving out \numberline{…} is the answer. – Circumscribe Jan 24 at 0:44
  • 1
    @mico: see modified answer ↑↑. – Circumscribe Jan 24 at 1:09
6

You asked,

Is there a way to make a single page table of contents ... [w]ithout generating it [from] a LaTex-Document?

I assume that by "without generating it from a LaTeX document", you mean "without creating a LaTeX document prefaced by a \tableofcontents directive". Since you posted your query to this site, I'm also assuming that you are not actually averse to creating this table of contents via a \documentclass ... \begin{document} ... \end{document} structure, i.e., via a LaTeX document.

If these assumptions are correct, the following solution may be of interest to you.

enter image description here

\documentclass{article}
\usepackage{tocloft} %% used only for '\cftsubsecleader' macro
\begin{document}

\setlength\parindent{0pt}
\obeylines
\textbf{Table of Contents}
\smallskip
Introduction \cftsubsecleader 1
Next Point   \cftsubsecleader 2
\end{document}
6

If you want make manually a ToC just as LaTeX does, it has been already answered, but the question is a duplicate of How to make Table of Contents manually.

If you want just the example as you posted, it could be using tocloft as Mico showed, or as simple as:

\documentclass{article}
\begin{document}
\section*{Table of Contents} 
{\setlength\parindent{0pt}
Introduction \dotfill  1\par
Next Point   \dotfill 2\par}
\end{document}

The dotted line of \dotfill is more dense that those of a normal ToC, but you can construct your own dotted line. The below example is a macro with a more sparse dots (bonus: a "WYSIWYG" odd syntax).

\documentclass{article}
\def\toc#1...#2 {\noindent#1\leaders\hbox to 1em{\hss.\hss}\hfill#2\par}
\begin{document}
\section*{Table of Contents}
\toc Introduction...1
\toc Next Point...22
\toc Last Point...312
\end{document}  

mwe

Of course, if you prefer a more orthodox syntax (e.g. \toc{Introduction}{1}) simply define the macro with \newcommand\toc[2]{...} or \def\toc#1#2{...}.

  • Perhaps my solution is a little elaborate for the intended application, and I probably should have found that question… – Circumscribe Jan 24 at 13:57

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.