Page LayoutCustom Entry in ToC

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
LavaTyper
Posts: 69
Joined: Sat Feb 11, 2012 2:38 am

Re: Custom Entry in ToC

Post by LavaTyper »

[ - 10 days later - ]


So guys, I need some help, how do I add one little extra line to my table of contents that says "Appendix C.........................TEXT" where "TEXT" is literally a string of text, instead of a page number?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

LavaTyper
Posts: 69
Joined: Sat Feb 11, 2012 2:38 am

Re: Custom Entry in ToC

Post by LavaTyper »

[-- 12 days later --]

Anyone??
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Custom Entry in ToC

Post by cgnieder »

Stefan indeed did mention the relevant commands:

Code: Select all

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

\addtocontents{toc}{\protect\contentsline {section}{Foo}{bar}}

\end{document}
contents.png
contents.png (8.85 KiB) Viewed 10698 times
Regards
site moderator & package author
LavaTyper
Posts: 69
Joined: Sat Feb 11, 2012 2:38 am

Custom Entry in ToC

Post by LavaTyper »

No, Stefan did not tell me **how** to use those commands. I already tried exactly what he suggested, and I didn't see any examples anywhere on the internet that used either of those commands


Alright, but when I use that command in that way, for my working example, "Appendix C" is indented, there are no dots between "Appendix C" and "TEXT," and "TEXT" does not appear all the way to the right like it should.


So:

How do I fix this??


(See attachment - this is the output that I get)

Code: Select all

\documentclass[12pt]{book}
\usepackage[dotinlabels]{titletoc}
\usepackage[indentafter,newlinetospace]{titlesec}
\usepackage{ifthen}
\renewcommand{\contentsname}{TABLE OF CONTENTS}
\renewcommand{\listtablename}{LIST OF TABLES}
\renewcommand{\appendixname}{Appendix}

% Format the Chapter in the TOC
\titlecontents{chapter}[0in]{\ifthenelse{\equal{\thecontentslabel}{1}}{
        \vspace{1.3\baselineskip}Chapter\\ \normalsize}{
        \normalsize\vspace{.2\baselineskip}}}
    {\contentspush{\thecontentslabel.~}}
    {}
    {\titlerule*[.3em]{.}\thecontentspage}
    {}
\titlecontents{section}[.3in]{}
              {\contentspush{\thecontentslabel.~}}{}
              {\titlerule*[.3em]{.}\thecontentspage}{}


% Format the Appendix in the TOC
\renewcommand{\appendixname}{Appendix}
\let\oldAppendix\appendix
\renewcommand{\appendix}{
  \oldAppendix
  \addtocontents{toc}{\protect \setcounter{tocdepth}{0} \par}
  \titlecontents{chapter}[0in]{\normalsize\vspace{.2\baselineskip}}
    {\contentspush{Appendix~\thecontentslabel.~}}{}
    {\titlerule*[.3em]{.}\thecontentspage}{}
}



\linespread{2}

\title{An example with TEXT for a Page Number}

\author{Lava Typer}

\begin{document}

\maketitle
\tableofcontents
\listoftables


\chapter{First Chapter}

\section{First Section}

Here is a table. I have many tables in my project.

\begin{table}[h]
\caption{\label{table:mycaption}Caption goes here}
\begin{tabular}{lcr}
left & center & right \\
\end{tabular}
\end{table}

\section{Second Section of new chapter}

Content

\appendix

\chapter{First Appendix}

\section{Greetings}

I have multiple appendices...

\chapter{Second Appendix}

Here is another appendix.

\section{What I need from LaTeX-Community}

\textbf{After Appendix B in the actual Table of Contents page listing... \\
I need ``Appendix C," dots, and a string of text (say, ``TEXT") instead of a page number.}

But I want the document to end at Appendix B...and not actually have a physical ``Appendix C" in the \LaTeX file.

The suggestion by cgnieder meets my requirement part way, but it doesn't format correctly. "Appendix C" is indented, there are no dots between "Appendix C" and "TEXT," and "TEXT" does not appear all the way to the right like it should.

How do I fix this?

\addtocontents{toc}{\protect\contentsline {appendix}{Appendix C}{TEXT}}

\end{document}
Attachments
HowDoIFixThis.jpg
HowDoIFixThis.jpg (41.27 KiB) Viewed 10696 times
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Custom Entry in ToC

Post by cgnieder »

LavaTyper wrote:No, Stefan did not tell me **how** to use those commands.
I didn't say that, did I? :)
LavaTyper wrote:Alright, but when I use that command in that way, for my working example, "Appendix C" is indented, there are no dots between "Appendix C" and "TEXT," and "TEXT" does not appear all the way to the right like it should.

[...]

Code: Select all

\appendix
% code skipped
\addtocontents{toc}{\protect\contentsline {appendix}{Appendix C}{TEXT}}
The first argument of \contentsline should be the name of the sectioning level you want the entry to appear, i.e., chapter, section, subsection etc. »appendix« is no such level which is why your attempt fails. It is unfortunate that you're not getting an error but that is because \contentsline calls the relevant internal command via \csname ... \endcsname which means that if the internal command doesn't exist it is silently defined to \relax which does nothing.

Try

Code: Select all

\addtocontents{toc}{\protect\contentsline {section}{Appendix C}{TEXT}}
Regards
site moderator & package author
LavaTyper
Posts: 69
Joined: Sat Feb 11, 2012 2:38 am

Custom Entry in ToC

Post by LavaTyper »

It doesn't work when I use section, because then the extra entry just doesn't appear at all in the Table of Contents.


And, yes, I made absolutely sure to compile it twice.


See screenshot.
Attachments
Missing.png
Missing.png (50.55 KiB) Viewed 10693 times
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Custom Entry in ToC

Post by cgnieder »

LavaTyper wrote:It doesn't work when I use section, because then the extra entry just doesn't appear at all in the Table of Contents.
That is because \appendix calls \setcounter {tocdepth}{0} (I can't remember if this is the default or if this is due to some package you've loaded...) but since your other appendices are chapters, anyway, you will probably want

Code: Select all

\addtocontents{toc}{\protect\contentsline {chapter}{Appendix C }{TEXT}}
With this I get
toc.png
toc.png (12.86 KiB) Viewed 10688 times
It seems to me that the following would even be better:

Code: Select all

\stepcounter{chapter}
\addtocontents{toc}{\protect\contentsline {chapter}{Appendix \thechapter. }{TEXT}}
With this you don't have to worry about the appendix counter yourself. (And it adds the dot I've forgotten in the previous version.)

Regards
site moderator & package author
LavaTyper
Posts: 69
Joined: Sat Feb 11, 2012 2:38 am

Custom Entry in ToC

Post by LavaTyper »

Alright but in my project, I need to input the following file: bibliography.tex, below:

Code: Select all

\chapter*{BIOGRAPHY OF THE AUTHOR}
\addcontentsline{toc}{chapter}{BIOGRAPHY OF THE AUTHOR}

Here is my biography. It has to appear after the last appendix, in both the document and the Table of Contents.

as well as hyper-active, colored links, so I also need the following code added to my example:

Code: Select all

\usepackage[colorlinks=true, pdfstartview=FitV, linkcolor=blue, citecolor=blue, plainpages=false, pdfpagelabels=true, urlcolor=blue]{hyperref}


Your code, \addtocontents{toc}{\protect\contentsline {chapter}{Appendix C }{TEXT}}, works fine...

but it doesn't work if I require both hyperref, in the preamble,
and \input{bibliography}, after the last actual appendix.

I get a bunch of error messages and the end of my table of contents formats incorrectly.


I've attached an updated example...how do I fix this?

Code: Select all

\documentclass[12pt]{book}


% I want clickable links to content in my dissertation:

\usepackage[colorlinks=true, pdfstartview=FitV, linkcolor=blue, citecolor=blue, plainpages=false, pdfpagelabels=true, urlcolor=blue]{hyperref}


\usepackage[dotinlabels]{titletoc}
\usepackage[indentafter,newlinetospace]{titlesec}
\usepackage{ifthen}
\renewcommand{\contentsname}{TABLE OF CONTENTS}
\renewcommand{\listtablename}{LIST OF TABLES}
\renewcommand{\appendixname}{Appendix}

% Format the Chapter in the TOC
\titlecontents{chapter}[0in]{\ifthenelse{\equal{\thecontentslabel}{1}}{
        \vspace{1.3\baselineskip}Chapter\\ \normalsize}{
        \normalsize\vspace{.2\baselineskip}}}
    {\contentspush{\thecontentslabel.~}}
    {}
    {\titlerule*[.3em]{.}\thecontentspage}
    {}
\titlecontents{section}[.3in]{}
              {\contentspush{\thecontentslabel.~}}{}
              {\titlerule*[.3em]{.}\thecontentspage}{}


% Format the Appendix in the TOC
\renewcommand{\appendixname}{Appendix}
\let\oldAppendix\appendix
\renewcommand{\appendix}{
  \oldAppendix
  \addtocontents{toc}{\protect \setcounter{tocdepth}{0} \par}
  \titlecontents{chapter}[0in]{\normalsize\vspace{.2\baselineskip}}
    {\contentspush{Appendix~\thecontentslabel.~}}{}
    {\titlerule*[.3em]{.}\thecontentspage}{}
}



\linespread{2}

\title{An example with TEXT for a Page Number}

\author{Lava Typer}

\begin{document}

\maketitle
\tableofcontents
\listoftables


\chapter{First Chapter}

\section{First Section}

Here is a table. I have many tables in my project.

\begin{table}[h]
\caption{\label{table:mycaption}Caption goes here}
\begin{tabular}{lcr}
left & center & right \\
\end{tabular}
\end{table}

\section{Second Section of new chapter}

Content

\appendix

\chapter{First Appendix}

\section{Greetings}

I have multiple appendices...

\chapter{Second Appendix}

Here is another appendix.




% Here is the code by: cgnieder

\addtocontents{toc}{\protect\contentsline {chapter}{Appendix C}{TEXT}}
% ^^ This works...
% as long as I don't then include my biography.tex file,
% after the last appendix:


\input{biography}

% If I take out EITHER \usepackage{hyperref} or \input{biography}, then it works again.

% I need both in this order, for my dissertation.


% How do I fix this?


\end{document}


Here is a picture to show you what I get when I comment out the hyperref in my preamble: as you can see it works fine up to that point.
Attachments
NoHyperref.jpg
NoHyperref.jpg (72.9 KiB) Viewed 10685 times
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Custom Entry in ToC

Post by cgnieder »

You probably should have included hyperref in your example in the first place as it redefines many of the standard macros to be able to add its bookmarks and hyperlinks. It also redefines \contentsline.

In this case the best would be to safe the original meaning to a new macro before calling hyperref:

Code: Select all

\newcommand*\LTXcontentsline{}
\let\LTXcontentsline\contentsline
\usepackage{hyperref}
(The first line is just to ensure the no macro is overwritten by \let.)

Now you use the new macro in the relevant line:

Code: Select all

\stepcounter{chapter}
\addtocontents{toc}{\protect\LTXcontentsline{chapter}{Appendix \thechapter. }{TEXT}}

BTW: I just realized by looking at your example again that you yourself redefined \appendix where you call \setcounter{tocdepth}{0} so your previous question shouldn't have been necessary...

Regards
site moderator & package author
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Re: Custom Entry in ToC

Post by cgnieder »

I forgot important information in my last post. I updated it...
site moderator & package author
Post Reply