Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'd like to define my own table of contents in TeX (not LaTeX, so without packages) to have a fine control on what goes there, how the numbers are displayed, etc. Basically what I would like is to write a file that looks like

1.0.0.Title of chapter 1
1.1.0.Title of section 1
1.1.1.Title of subsection 1.1
1.1.2.Title of subsection 1.2
2.0.0.Title of chapter 2
A.0.0.Title of appendix A

and read it with these kind of options:

  • write a table of contents including only the chapters
  • write a table of contents including only the chapter 1 and its sections
  • ...

To do so I have 2 questions:

  1. How can I (re)write some content at a particular line to keep my file sorted? (e.g. if after a second compilation I change the title of chapter 2 and I add a subsection 1.3 in chapter 1, how do I update my file?)
  2. How do \read this file? I'm new with file writing/reading in TeX so I don't know if there is any care to take or a special way to do this?

After thinking of my question, question 2 might trivial if the file is written in the template

\toccontent{1}{0}{0}{Title of chapter 1}
...

so that it only has to be \inputted, after redefining \toccontent according to what and how I want to display (e.g. "do nothing if the 2nd argument is not 0").

share|improve this question
    
I don't understand very well what you mean with your second question. Could you give an example? –  Bernard Feb 1 at 20:43
    
@Bernard my question is: once the file is written, how do I \read it back in TeX? I never used file writing/reading in TeX so I don't know how it works or if there is any care to take of something. (I updated the question) –  lvaneesbeeck Feb 2 at 16:48
    
@Ivaneesbeeck: I still can't grasp what you try to obtain exactly –  Bernard Feb 2 at 16:59
    
LaTeX makes the table of contents with help of an auxiliary .toc file – that's why when you change the title of a chapter, say, seeing the modification in the final pdf requires2 compilations. Could you give a minimal concrete example of what you want to do? –  Bernard Feb 2 at 17:05
    
@Bernard I thought I gave a minimal example in my post: write a file with all those chapters/sections/subsections titles and from this file 1, extract a table of chapters; 2, extract a table of contents restricted to chapter 1 and its sections. (I forgot to mention, but of course I'm aware that this will need 2 compilations as LaTeX' normal \tableofcontents) –  lvaneesbeeck Feb 2 at 17:08

3 Answers 3

up vote 3 down vote accepted

Using eplain macros you can write to the .toc file and read the result in. Commands such as \writenumberedtocentry produce lines in the toc file prefaced by e.g. \tocchapterentry. You thus need to define commands the \tocchapterentry, \tocsectionentry for formatting/processing the material read in.

Here is a very basic example of a document with two versions of the table of contents, for the same toc file, the second only containing the chapters.

Sample output

\input eplain

\newif\ifchaponlytoc
\chaponlytocfalse

\def\tocchapterentry#1#2#3{{\bf #2 #1 page #3}\par}
\def\tocsectionentry#1#2#3{\ifchaponlytoc\else#2 #1 page #3\par\fi}

\newcount\chapno
\newcount\secno
\newcount\subsecno

\chapno=0\secno=0\subsecno=0

{\bf Table of Contents}
\smallskip

\readtocfile

\bigskip

{\bf Table of Chapters}
\smallskip

\chaponlytoctrue
\readtocfile

\bigskip

\advance\chapno by 1\secno=0\subsecno=0
\xdef\fullchapno{\the\chapno .\the\secno .\the\subsecno}
{\bf \the\chapno\ First Chapter}
\writenumberedtocentry{chapter}{First Chapter}{\fullchapno}

Text of first chapter.

\medskip
\advance\secno by 1\subsecno=0
\xdef\fullchapno{\the\chapno .\the\secno .\the\subsecno}
{\it \the\chapno.\the\secno\ First Section}
\writenumberedtocentry{section}{First Section}{\fullchapno}

Text of first section.

\bigskip

\advance\chapno by 1\secno=0\subsecno=0
\xdef\fullchapno{\the\chapno .\the\secno .\the\subsecno}
{\bf \the\chapno\ Second Chapter}
\writenumberedtocentry{chapter}{Second Chapter}{\fullchapno}

Text of second chapter.

\bye

In the body of the document, each chapter, section etc. updates its counters, defines \fullchapno and writes the infomation out to the toc file. Note that for \writenumberedtocentry the last argument only gets its first term exanded, so \fullchapno needs to contain the expanded chapter number.

Now at the top of the document we introduce a toggle via \ifchaponlytoc to determine whether we want to only have chapters in the toc or all levels. The lower level toc print commands then test this toggle before deciding whether to output anything. An alternative way to proceed would be to simply define these commands differently before the second reading of the toc file.

Of course much of the code above can be packaged in to macros, but also many of the details will depend on how you set up your sectioning commands and add entries to the toc. I hope this gives you a starting idea. Look at the eplain documentation for much more information.

share|improve this answer

The titletoc package offers what you need, and in particular, its command printcontents command can be used.

The printcontents command has syntax

\printcontents[name]{prefix}{start-level}{toc-code}

which I have used in two ways in the code below:

\startcontents[chapters]
...
...
% prints only chapters
\printcontents[chapters]{}{0}{
% locally set the tocdepth for *this* printing of the toc
\setcounter{tocdepth}{0}
}

which will print out only your chapter files because I have set the tocdepth to 0 locally.

I have also used

\startcontents[zebras]
\chapter{zebras}
% prints this chapter and its sections
\printcontents[zebras]{}{0}{
% locally set the tocdepth for *this* printing of the toc
\setcounter{tocdepth}{1}
}
\section{}
\lipsum
\section{}
\section{}
\stopcontents[zebras]

to print you a minitoc just for that chapter. You can increase the tocdepth locally if you wish to include other heading levels in this partial toc.

Here's a complete MWE that demonstrates the idea- for future reading, please consult the documentation; you may also be interested in the minitoc package which offers some similar functionality.

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{report}
\usepackage{titletoc}   % customize tableofcontents
\usepackage{lipsum} % sample text

% the printcontents command is from the titletoc package
% and it has the form
%
% \printcontents[name]{prefix}{start-level}{toc-code}
\begin{document}

% start partial toc for the chapters
\startcontents[chapters]

% start partial toc for this chapter- call it zebras, for example
\startcontents[zebras]
\chapter{zebras}
% prints this chapter and its sections- adjust the tocdepth
% to get deeper headings such as subsection, etc
\printcontents[zebras]{}{0}{
    % locally set the tocdepth for *this* printing of the toc
    \setcounter{tocdepth}{1}
}
\section{}
\lipsum
\section{}
\section{}
\stopcontents[zebras]

% new chapter
\chapter{}
\section{}
\section{}
\section{}
\lipsum

% prints only chapters by setting the tocdepth locally
\printcontents[chapters]{}{0}{
    % locally set the tocdepth for *this* printing of the toc
    \setcounter{tocdepth}{0}
}
\end{document}
share|improve this answer
    
Sorry! you posted while I was writing and checking my answer. –  Bernard Feb 1 at 21:01
1  
I clarified my question: I'm looking for a solution in "pure" TeX, not LaTeX. However thanks for your answer, it may be useful for a LaTeX user :-) –  lvaneesbeeck Feb 2 at 17:10
    
dear down voter: please let me know why you down voted my answer- I'd love to improve my answer –  cmhughes Feb 2 at 18:05
    
I down voted it for the main reason that it's not a package-free solution and it doesn't answer any of my two questions. This is not the kind of answer I was looking for (I rewrote my question which may be clearer now). –  lvaneesbeeck Feb 2 at 19:23
4  
You are, of course, free to use your votes as you please, but generally speaking on this site we use down votes very sparingly. Your question does not contain a minimal working example (MWE), nor was it obvious that you only wanted a plain-tex solution- you might consider modifying your tags –  cmhughes Feb 2 at 20:08

For the first question, you only have to compile twice: once to modify the .toc file, the second time to incorporate it to the pdf or dvi.

To write a table of contents including only the chapters, write:

\setcounter{\tocnumdepth}{0} \tableofcontents  

The titletoc package (included in titlesec, but can be used alone) lets you customise the table of contents, and allows partial table of contents (/figures/tables) with specific commands:

\startcontents, \stopcontents, \resumecontents and \printcontents

Details are in the titlesec documentation, § 6.3 to 6.5

share|improve this answer
1  
Well, ignoring the fact that the question got changed after you posted this, still, the other answer contains more information than this one, and it was posted before this. –  yo' Feb 3 at 10:31
    
The question is not about doing it in LaTeX. –  egreg Feb 3 at 12:44
    
It was not quite clear from the first formulation… –  Bernard Feb 3 at 16:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.