4

So, I have the file backmatter.tex

\clearpage
\printindex
\addcontentsline{toc}{section}{\indexname}

\newpage
\listoffigures
\addcontentsline{toc}{section}{Перелік ілюстрацій}

\newpage
\listoftables
\addcontentsline{toc}{section}{Перелік таблиць}

\clearpage
\bibliographystyle{alpha}
\bibliography{bibliography}

This file is included at very end of main document (right before \end{document}). If I use no tables in my document, it produces the title for list of tables but no table entries obviously. So I could just remove this fragment of code but doing it everytime as I do not use something would be... ...not convenient.

So, for example, how to prevent

\newpage
\listoftables
\addcontentsline{toc}{section}{Перелік таблиць}

from typesetting if there are no tables in my document?

2 Answers 2

3

You can use tocbibind to avoid problems in the page number. I also define conditionals for deciding whether a table or a figure was indeed present in the document; such code must go in the preamble.

Using the value of the counters is not safe, because the counters might be tied to chapter and if there's no figure or table in the last chapter, the value is useless.

For the index, I suggest imakeidx that takes care of the inclusion in the table of contents. Using Xindy and not Makeindex is necessary for Cyrillic based documents.

\documentclass[a4paper]{book}
\usepackage[T2A]{fontenc}
\usepackage[ukrainian]{babel}
\usepackage[nottoc]{tocbibind}

\usepackage[xindy]{imakeidx}

\makeindex[intoc,options=-L ukrainian -C utf8]

% conditional printing of lists
\newif\iffigures
\AtBeginEnvironment{figure}{%
  \iffigures\else\global\figurestrue\fi
}
\newif\iftables
\AtBeginEnvironment{table}{%
  \iftables\else\global\tablestrue\fi
}

\begin{document}

\frontmatter

\tableofcontents

\mainmatter

\chapter{Спочатку}

\index{Спочатку}

\begin{figure}\caption{підпис}\end{figure}
\begin{table}\caption{підпис}\end{table}

\backmatter

\printindex

\iffigures\listoffigures\fi

\iftables\listoftables\fi

\end{document}

I typeset the document four times, suitably commenting or uncommenting the lines for figure and table.

No figures, no tables

enter image description here

Figures, no tables

enter image description here

No figures, tables

enter image description here

Figures, tables

enter image description here

1
  • Oh, that works exactly as I planned it to be, thank You! Jan 10, 2023 at 14:19
1

See package tocbibind:

"The tocbibind package can be used to add document elements like a bibliography or an index to the Table of Contents."

1
  • 1
    Hmm. I have read documentation, can't get the point how to use it, sorry... (I'm pretty new to LaTex) Jan 10, 2023 at 14:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .