0

To keep it simple, I have created a custom table of contents for LaTeX,

% ===TOC (Alphabetical)===
% - - - - - - - - - - - - - - - -
\makeatletter
\newcommand\tableofcontentsalphabetical{%
    \if@twocolumn
        \@restonecoltrue\onecolumn
    \else
        \@restonecolfalse
    \fi
    \chapter*{%
        \contentsname   \\
        (Alphabetical*)
        \@mkboth{\MakeUppercase\contentsname (Alphabetical*)}
                {\MakeUppercase\contentsname (Alphabetical*)}%
    }%
    \@starttoc{tocabc}%
    \if@restonecol\twocolumn\fi
}
\newcommand{\addtocabc}[2]{%
    \addcontentsline{tocabc}{#1}{\protect\numberline{\csname the#1\endcsname}#2}%
}
\makeatother

And, I was wondering if there were a way to declare the order in which whatever will appear, that I add with the afforementioned command. I don't understand TeX or LaTeX, but I am gradually seeing things as I look for answers and ask.

In my case, I judge it would be easier for me to declare which order an entry should go, rather than worry about declaring the page (if that is even possible to do.) Because the order will never change (it is alphabetical) but the pages will.

I hope this is clear. But, that said, should I just forego caring and manually reorder the appropriate *.tocabc file, that LaTeX creates?

I write in Vim, so I have Vim call a scrit to look at the ending of a file name and compile it appropriately, so it wouldn't be hard to make it compile the file two or three times, then reorder that TOC file, then recompile it.

Thank you in advance!

  • As with the answer by Max, I'm curious why you have an alphabetical TOC. Do you also have a regular TOC? Could you do this as an index, which Max and I seem to think this is resembling? – Teepeemm Jan 17 '21 at 23:26

2 Answers2

1

Well if you do the sorting manually it is rather easy, you only need labels:

\documentclass{book}
\begin{document}
\tableofcontents

\chapter*{Alphatical toc} \makeatletter @starttoc{tocabc} \makeatother

\addtocontents{tocabc}{\protect\contentsline{chapter}{B}{\pageref{B}}{}} \addtocontents{tocabc}{\protect\contentsline{chapter}{C}{\pageref{C}}{}} \addtocontents{tocabc}{\protect\contentsline{chapter}{X}{\pageref{X}}{}}

\chapter{C\label{C}} \chapter{X\label{X}} \chapter{B\label{B}}

\end{document}

If you want hyperlinks it could get a bit more complicated.

Ulrike Fischer
  • 327,261
1

Since you say you're new to LaTeX I hope it's ok to give first some general advice not directly answering the question. Let me first say that in general, unless you're really an expert, you should not define custom commands for what already exists, but rather look up documentation about possible options. Nearly everything that is really desirable is already implemented, and if you have trouble finding what you want to do, it is probably a bad idea.

When you say "order of items won't change but pages will" I think you mix up (the purpose of) the table of contents and an index. The table of contents should be in the order of the pages, not alphabetical, and the index is there for the opposite. Doing otherwise could confuse the readers. (This was also answered in this SE question.)

I suggest you should document yourself about LaTeX's makeindex package and command, see, e.g., wikibooks. Then you would, e.g., use:

\usepackage{makeidx} % this goes in the preamble
\makeindex
(...)
\chapter{Zebra}\index{Zebra}
(...)
\chapter{Apple}\index{Apple}
(...)
\printindex
\end{document}

(and use the makeindex command in addition to (pdf)latex or whatever, see the above doc).

You can (in principle) avoid the need for the \index commands by redefining the \chapter command, as, e.g., with

\let\ochap\chapter \def\chapter#1{\ochap{#1}\index{#1}}

(If you have titles with commands inside you may need to use \protect; also, if you want to use \chapter* more care is needed.)

That said, the order of what's in the TOC is indeed defined through the \addcontentsline commands, and you can add the chapters in any order you wish to a custom TOC by hand as shown in Ulrike's answer. But this requires that you maintain by hand an alphabetically ordered list with a duplicate of each chapter title. One way to avoid this could be to redefine the \chapter command as above to add the title in a custom list, and use available commands/packages (e.g., datatool) or your own code, as explained, e.g., in this answer or this or this or this question, to produce a sorted list.

If you want the list at the beginning of the document you certainly need to use an auxiliary file. So you could go with a second custom TOC file as in Ulrike's answer, in which you can either put the required commands to make the list, or maybe easier, locally redefine the usual commands in the TOC to do what you need. (I can edit my answer to elaborate on that if needed.)

But once again, I think building the alphabetical list of the contents should be left to makeindex.

Max
  • 145