1

The attached image shows the problems in an almost MWE:

  • In TOC, the appendices are called Chapter instead of Appendix
  • Much worse is that the sections in the Appendices find their way in the Chapter toc of the last Chapter.

What complicates matters are the construction of the Chapter Toc, the Oberdiek hack which loads showidx, and the hack which allows showidx to display mathematics and, say,

$\langle23\rangle$

(See image.)

enter image description here

The code is:

% !TEX TS-program = pdflatexmk
\documentclass[11pt]{book}

%Begin Chapter TOC
    \usepackage[titletoc]{appendix}
    \usepackage[dotinlabels]{titletoc}%a companion to the titlesec package which does Chapter Tocs
\titlecontents{chapter}[6pc]{\addvspace{1pc}\bfseries\filright}%
{\contentslabel[\chaptername \ \thecontentslabel]{6pc}}%
{}{\hfill\contentspage}[\addvspace{2pt}]%

\newcommand\xquad{\hspace{0.4em plus .2em minus .2em}}%
\titlecontents*{p-section}[0pt]{\filright\small}{}{}{, \thecontentspage}[\xquad\textbullet\xquad][.]%
\newcommand{\ChapterToc}{\startcontents[chapters]\printcontents[chapters]{p-}{1}{}\vspace{10mm}}%
%End Chapter TOC
%Begin References   
    \usepackage{makeidx} %
%package showidx must NOT be loaded because it is Hyperref that loads it in the Oberdiek hack below
    \usepackage[hyperfootnotes=false]{hyperref}%[pdfborder={0 0 0}]
    \hypersetup{colorlinks,linkcolor={black},}
%Begin the ``quick and dirty hack'' due to Heiko Oberdiek which loads showidx 
\makeatletter
\ifHy@hyperindex
  \def\HyInd@ParenLeft{(}%
% Hook in \HyInd@@wrindex
  \let\HyInd@showidx\@empty
% Hook in \HyInd@@wrindex for package showidx
  \def\HyInd@showidx#1{%
    \@showidx{#1}%
    \if@nobreak
      \ifvmode
        \nobrak
      \fi
    \fi
  }%
% Load package showidx
  \let\siOrg@makeindex\makeindex
  \let\siOrg@@index\@index
  \let\siOrg@@wrindex\@wrindex
  \let\siOrg@index\index
  \RequirePackage{showidx}
  \let\makeindex\siOrg@makeindex
  \let\@index\siOrg@@index
  \let\@wrindex\siOrg@@wrindex
  \let\index\siOrg@index
% rest of hyperref part
  \@ifpackageloaded{multind}{%
    \let\HyInd@org@wrindex\@wrindex
    \def\@wrindex#1#2{\HyInd@@wrindex{#1}#2||\\}%
    \def\HyInd@@wrindex#1#2|#3|#4\\{%
      \ifx\\#3\\%
        \HyInd@org@wrindex{#1}{#2|hyperpage}%
      \else
        \def\Hy@temp@A{#3}%
        \ifx\Hy@temp@A\HyInd@ParenLeft
          HyInd@org@wrindex{#1}{#2|#3hyperpage}%
        \else
          \HyInd@org@wrindex{#1}{#2|#3}%
        \fi
      \fi
    }%
  }{%
    \def\@wrindex#1{\@@wrindex#1||\\}
    \def\@@wrindex#1|#2|#3\\{%
      \ifx\\#2\\%
        \protected@write\@indexfile{}{%
          \string\indexentry{#1|hyperpage}{\thepage}%
        }%
      \else
        \def\Hy@temp@A{#2}%
        \ifx\Hy@temp@A\HyInd@ParenLeft
          \protected@write\@indexfile{}{%
             \string\indexentry{#1|#2hyperpage}{\thepage}%
          }%
        \else
          \protected@write\@indexfile{}{%
            \string\indexentry{#1|#2}{\thepage}%
          }%
        \fi
      \fi
      \endgroup
      \HyInd@showidx{#1}%
      \@esphack
    }%
  }%
\fi
\makeatother
%End the ``quick and dirty hack'' due to Heiko Oberdiek which loads showidx
%Begin hack to have math in Showindex
\let\oldopenparen\(
\let\oldcloseparen\)
\def\({\protect\oldopenparen}
\def\){\protect\oldcloseparen}
\newcommand{\INDEX}[1]{\index{#1}}
%Begin hack for \langle and \rangle.            
\let\oldlangle\langle
\def\langle{\protect\oldlangle}
\let\oldrangle\rangle
\def\rangle{\protect\oldrangle}
%End hacks          
    \makeindex

\begin{document}
         \tableofcontents

\chapter{ONE}
\ChapterToc
Some irrelevant text
\section{Plain Numbers}
Some text where \textbf{Number\(\langle23\rangle\)}\INDEX{number \(\langle23\rangle\)} is used. 

\appendixpage
\appendix
%\begin{appendices}
\chapter{Addition Formulas}
Some arelevant text
\section{Squares}
Appended text       
%\end{appendices}

\backmatter%
\phantomsection
\addcontentsline{toc}{chapter}{Index}   

\printindex
\end{document}
lockstep
  • 250,273
schremmer
  • 2,107
  • How essential is it to you to use the titletoc package? This package is used to typeset the entries in the table of contents. Unfortunately, it doesn't appear to be sufficiently flexible to permit a renewal of the \chaptername macro halfway through the document, viz., at the start of the appendix portion. – Mico Jun 05 '16 at 19:44
  • You can solve the “appendix” issue in the TOC by doing \addtocontents{toc}{\protect\renewcommand\protect\chaptername{\protect\appendixname}} after \appendix. For the short TOC issue, I don't know; maybe switching to etoc instead of titletoc? – egreg Jun 05 '16 at 22:15
  • @egreg When I added that line, instead of Chapter 1, Chapter A, I got Chapter A, ??name A. – schremmer Jun 06 '16 at 01:38
  • @Mico I don't know about essential but I think I would rather use a phony last chapter for the appendices to get into the chapter toc because we are talking about students who, even though they are of good will, do not really know how to read "pencil in hand". Which, by the way, is why I am spending a lot of time these days cross-referencing and heling them navigate. (Another thing they have no idea they should do: go back and see.) In fact, at first, I thought it was tcolorbox and cleveref which had been screwing up the chapter tocs because, a while ago, they were still ok. – schremmer Jun 06 '16 at 01:46
  • @schremmer To be honest, I removed all hacks related to the index. But it may just be a copy-paste problem: that code should be one line with no space in it. – egreg Jun 06 '16 at 07:01
  • @schremmer: titletoc causes much more problems than necessary. I don't say,that it's responsible for the current issues,but removing it might be a good idea. Of course neither cleveref nor tcolorbox screw up the ToC –  Jun 06 '16 at 09:48
  • @egreg It sure was a copy-paste issue: I copied the code off here but when I pasted it did not notice that I had gotten \appendix??name. No idea how ?? got in. Sorry about that. Now it works fine---with the hacks. – schremmer Jun 06 '16 at 11:39
  • for 2nd issue you need to add \stopcontents[chapters] at the end of the chapter. – touhami Jun 06 '16 at 23:07
  • @egreg I tried \addtocontents{toc}... in the "magnum opus" and it worked beautifully there too … except the first Appendix is still called Chapter in the TOC. (The other ones are correctly called Appendix.) Presumably because, in the magop, the chapter files are \included in a file which is \input in the master file. After which \stopcontents[chapters], \appendix, \addtocontents{toc}..., \include{Appdx1}, … etc. But \addtocontents{toc}... still worked in the above MWE with a second appendix. Any idea? In any case, thanks: the magop now looks perfect anyhow. – schremmer Jun 07 '16 at 16:54
  • @schremmer The problem with \appendix and \include has already been discussed on this site – egreg Jun 07 '16 at 17:30

1 Answers1

1

To use appendixname instead of \chaptername in the table of contents, add these lines to you preamble:

\usepackage{apptools, etoolbox}
\makeatletter
\patchcmd{\@chapter}{\protect {CHAPTER }}{\ifappendix{APPENDIX }\else{CHAPTER }\fi}{}{}
\makeatother
Bernard
  • 271,350