2

How can I put a page range including the number of the last page into the Table of Contents (see picture as example below) in LaTeX? I have searched in forums but found no solution.

Put last page number into ToC

  • 1
    Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Apr 15 '15 at 16:06
  • 1
    Could you provide the community with a minimal working example (MWE) that we can use as a starting point? This should start with \documentclass and end with \end{document}. The document class is important to know, as well as whether or not you're using any packages related to ToC formatting. – Werner Apr 15 '15 at 16:06
1

A MWE would be necessary to give an answer applicable to the specific case:

Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}.

Unlike any other programming languages, it makes a lot of difference if you change the preamble of your document in terms of the output, such as clashing packages or page settings changed by some detail in the code and so on. That's why we need to have a complete example together with the relevant parts of your preamble included. Otherwise we might not be able to reproduce your problem.

(from Text building blocks)

But because the idea is interesting and several things can be demonstrated here, I will nevertheless try to give an answer by treating different cases:

  1. "Appendices A - Y ... [page number]" is already added automatically to the Table of Contents (ToC). Without knowing the automatic process, it cannot be known how to change it. It is neither \chapter{Appendices A - Y}, because then it would be a numbered chapter, nor \chapter*{Appendices A - Y}, because then it would not be listed in the ToC. To change such a ToC it is possible (warning following below ) to change the .toc file, which contains the code for the ToC. For example \chapter{Example} in the simlest case results in \contentsline {chapter}{\numberline {<number/name of chapter>}Example}{<page number>}. If the hyperref package is used, this would read \contentsline {chapter}{\numberline {<number/number of chapter>}Example}{<page number>}{chapter.<number of chapter>} - therefore a MWE is necessary! Exchanging <page number> in the .toc file with "37 - 62" will give you exactly that when compiling the document one more time, but compiling will also overwrite the .toc file, i.e. you would need to apply the manual change for each and every compilation run. Additionally the page numbers might change, i.e. you would always have to check whether it is still "37 - 62" or maybe "38 - 63" or whatsoever. If hyperref is used with the option linktoc=all or linktocpage=true (without MWE we do not know this) then the page numbers are hyperlinked to that page, and changing the entry in the .toc to "37 - 62" will not change the link target but create one link "37 - 62" still linking to "37" - maybe that is as intended, but maybe separate links to "37" and "62" are wanted.

  2. Assuming that the entry "Appendices A - Y ... [page number]" is not added automatically to the ToC, for example \chapter*{Appendices A - Y} was used and the ToC entry created by - well, you see why a MWE would be helpful, don't you?

a) If \addcontentsline{toc}{chapter}{Appendices A - Y} was used (\addcontentsline then would be the command to look up) after e.g. \chapter*{Appendix A}, then the entry in the .toc file could read \contentsline {chapter}{Appendices A - Y}{37}{chapter*.11}. Changing "37" manually in the .toc works as explained (and warned against ) above.

b) If \addtocontents{toc}{\noindent Appendices A - Y \hfill \thepage} was used, it is easily changed to \addtocontents{toc}{\noindent Appendices A - Y \hfill \thepage{} - \pageref{LastPage}}, which requires a \usepackage{lastpage} in the preamble. (Instead of the lastpage package you might want/need to use the pageslts package.) If the last page of "Appendix Y" shall be referred to (and there is another page in the document behind it), then it is necessary to set an own label \label{EndOfY} at the last page of "Appendix Y" and refer to that page instead of LastPage. If the hyperref package is used with option linktoc=all or linktocpage=true (MWE...), the reference to the last page is even hyperlinked, but to the given first page of "Appendices A" there is no hyperlink, which is obviously ugly because it is inconsistent.

i) no hyperlinks: Just use \pageref*{LastPage} to make the hyperlink go away.

ii) hyperlinks: Instead of \thepage a reference to the beginning of "Appendix A" is needed. Setting a \label, e.g. \chapter*{Appendix A\label{AppA}}, and referring to it, e.g. \addtocontents{toc}{\noindent Appendices A - Y \hfill \pageref{AppA} - \pageref{LastPage}}, do the trick.

Note that hyperref with e.g. linktoc=all also creates hyperlinks for the text in the ToC, i.e. it would be necessary to set a label AppY for "Appendix Y" and use

\addtocontents{toc}{\noindent Appendices \hyperref[AppA]{A} - %
  \hyperref[AppY]{Y} \hfill \pageref{AppA} - \pageref{LastPage}}

But wait: What happens, when Appendix Y is no longer Appendix Y but Appendix Z, because you added an Appendix between Appendix M and Appendix N? Because of this it is better to use \ref{AppA} and ensure that the references for the appendices are alphabetic.

iii) If the tocloft package is used, then the format of the \addtocontents{toc}{...} will need to be changed, e.g. there could be a "dotfill" instead of \hfill. (MWE!)

I hope that gives some ideas about possibilities, which can than lead to specific search terms or specific questions (with MWE ;-) ).

Good luck for your thesis!

| improve this answer | |
  • This may be the old post. But I would like to thank all involved to help me during thesis writing. Now I already graduated PhD. Thanks all for the help :) – Muhammad Aizzat Zakaria Aug 13 '16 at 11:35
  • @MuhammadAizzatZakaria: Congratulations! – Stephen Aug 14 '16 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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