How can i let Tex only display the list of tables if there exists at least one table and not if there's none?
1 Answer
You can save the meaning of \listoftables
, make the macro a no-op, but then add at the beginning of the first table
the instruction to write in the log file that \listoftables
has to receive its original meaning.
With \AddToHookNext
, this is executed just once.
\documentclass{article}
\makeatletter
\let\orig@listoftables\listoftables
\let\listoftables\relax
\AddToHookNext{env/table/begin}{%
\write\@auxout{\global\let\string\listoftables\string\orig@listoftables}%
}
\makeatother
\begin{document}
\listoftables
\section{Test}
\begin{table}[htp]
There is a table
\caption{Test}
\end{table}
\begin{table}[htp]
There is a table
\caption{Test}
\end{table}
\end{document}
The .aux
file contains
\relax
\@writefile{toc}{\contentsline {section}{\numberline {1}Test}{1}{}\protected@file@percent }
\global \let \listoftables\orig@listoftables
\@writefile{lot}{\contentsline {table}{\numberline {1}{\ignorespaces Test}}{1}{}\protected@file@percent }
\@writefile{lot}{\contentsline {table}{\numberline {2}{\ignorespaces Test}}{1}{}\protected@file@percent }
\gdef \@abspage@last{1}
Note that \global
is necessary, because the .aux
file is read in a group.
If I comment out the two table environments, the output is
and no list of tables is produced.
-
Your example works out a strange way. If i test it in "TeXstudio", sometimes the list of tables is seen, sometimes is not whenever tables are present and if i delete the tables then after first run, the list is present, after second run, it's not. If i test your example with "bookdown" then the list of tables won't appear no matter the tables are present or not. Without your example, the empty list of tables is present if no tables are present. Jul 24 at 22:59
-
@user7351278 I don’t know what
bookdown
is, sorry. Are you sure you have an up-to-date TeX distribution?– egregJul 25 at 7:45 -
bookdown is a framework for creating different output out of R markdown files. The distribution is up-to-date. Jul 27 at 0:47
-
@user7351278 Still unclear, sorry. Possibly the conversion engine doesn’t handle the code right.– egregJul 27 at 8:42
-
\listoftables
, but have a page with that heading produced only if a table exists. Is that so?