0

In the table of content, I want to have Appendix A instead of chapter A, Appendix B instead of chapter B, and show on. I can't show this when I use \include{appendix}. Following is my code:

\documentclass{report}
\usepackage{tocloft,calc}
\renewcommand\cftchapaftersnum{}% adds dot after chapter title in ToC
\renewcommand\cftchapdotsep{\cftdotsep}% adds leader dots from chapter titles to page numbers
% chapter and chapter number rather than just number
\renewcommand{\cftchappresnum}{\chaptername\space}
\setlength{\cftchapnumwidth}{\widthof{\textbf{Appendix~999~}}}
\makeatletter
\g@addto@macro\appendix{%
  \addtocontents{toc}{%
    \protect\renewcommand{\protect\cftchappresnum}{\appendixname\space}%
  }%
}
\makeatother
\begin{document}
\tableofcontents
\cleardoublepage
\addcontentsline{toc}{chapter}{List of Tables}
\listoftables 
\include{chp1}
\include{chp2}
\appendix
\include{appendix} : 
\end{document}

%chp1.txt is 
\chapter{Introduction1}
%chp2.txt is 
\chapter{Introduction2}

%appendix.txt is 

% Appendix A
\chapter{}
\section{hello1}
In the ...
\section{hello2 }

 % Appendix B
\clearpage
\newpage
\chapter{}

\section{bello1}
\section{bello2 }

%table of content output

Contents
    List of Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . .2
    Chapter 1 Introduction1 . . . . . . . . . . . . . . . . . . . . . . . 3
    Chapter 2 Introduction2 . . . . . . . . . . . . . . . . . . . . . . . 4
    Chapter A . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  .5
    A.1 hello1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  5
    A.2 hello2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5
    Chapter B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
    B.1 bello1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  6
    B.2 bello2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  6

P.S. I am NOT looking for the solution with \input{appendix} for \include{appendix}. I already tried that; although it works for this example, it does not solve my real problem.

Desired table of content output:

Contents
List of Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . .2
Chapter 1 Introduction1 . . . . . . . . . . . . . . . . . . . . . . . 3
Chapter 2 Introduction2 . . . . . . . . . . . . . . . . . . . . . . . 4
Appendix A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5
A.1 hello1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  5
A.2 hello2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5
Appendix B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6
B.1 bello1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  6
B.2 bello2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  6
Metrics
  • 365
  • Please see my answer to a similar question. – Bernard Feb 14 '16 at 23:28
  • Thanks. But with report class, I didn't get that output. – Metrics Feb 14 '16 at 23:59
  • I'd say it's because you load tocloft which must have some interaction with my code. Anyway, your code gives you what you want, or do I miss something? – Bernard Feb 15 '16 at 00:14
  • As to my code, it doesn't. If you see my expected output and actual output, you see that there is chapter A instead of Appendix A and chapter B instead of Appendix B. With regard to your solution, I ran your code without loading tocloft. – Metrics Feb 15 '16 at 00:24
  • On my system, compiling your code produces ‘appendix A’, &c.. However I didn't input files, I directly used their contents in the main file. – Bernard Feb 15 '16 at 01:08
  • You are correct. The problem doesn't occur when I don't use include{appendix} (directly use contents in the main file). However, I do have to use include{appendix} and hence the question. Thanks once again. – Metrics Feb 15 '16 at 01:22
  • 1
    I'll try to think about the problem tomorrow. – Bernard Feb 15 '16 at 02:21
  • i don't get the problem AND I used \include{appendix}. I did replace the \include{chp1} and \include{chp2} though. – John Kormylo Feb 15 '16 at 02:37
  • @John: That is also true. But, still I do need to use include{chp1} and \include{chp2}. – Metrics Feb 15 '16 at 03:07
  • 1
    I certainly didn't expect it, but adding the other includes caused the problem. Anyway, putting \appendix into the appendix.tex file fixes it. You may have to add it to every appendix file. – John Kormylo Feb 15 '16 at 05:10

1 Answers1

2

Moving the \appendix command from the main file to appendix.tex is one of many ways to fix the problem. (Reproducing the problem is the hard part) Having multiple \appendix commands doesn't harm anything except resetting the chapter counter each time.

Everything else is the same except for appendix.tex:

\appendix
% Appendix A
\chapter{}
\section{hello1}
In the ...
\section{hello2 }

 % Appendix B
\clearpage
\newpage
\chapter{}

\section{bello1}
\section{bello2 }
John Kormylo
  • 79,712
  • 3
  • 50
  • 120