I am using a variant of the answer provided here to remove the List of Figures and List of Tables part of the contents when none are there. The issue I have is when using longtable, if there is only a single longtable and no other table, the List of Tables gets removed as the following line (I believe) does not account for long tables. What do I need to do to amend this?
\def\tables@in@document {% \immediate\write\@mainaux {\global\string\tablestrue}% \global\let\tables@in@document\empty } MWE below
\documentclass[10pt]{article} \usepackage{lipsum} \usepackage[margin=0.75in]{geometry} \usepackage{longtable} \usepackage{float} \floatstyle{boxed} \restylefloat{figure} \restylefloat{table} \newif\iffigures \newif\iftables \makeatletter \let\OLDfigure\figure \def\figure {\figures@in@document\OLDfigure } \let\OLDtable\table \def\table {\tables@in@document\OLDtable } \def\figures@in@document {% \immediate\write\@mainaux {\global\string\figurestrue}% \global\let\figures@in@document\empty } \def\tables@in@document {% \immediate\write\@mainaux {\global\string\tablestrue}% \global\let\tables@in@document\empty } \makeatother % for the purpose of testing % this will make a MWE without tables \long\def\IGNORE #1\ENDIGNORE{} % uncomment to make a MWE with tables %\let\IGNORE\empty %\let\ENDIGNORE\empty \begin{document} \tableofcontents % \iffigures \addcontentsline{toc}{section}{List of Figures} \listoffigures \fi % \iftables \addcontentsline{toc}{section}{List of Tables} \listoftables \fi \section{First section} \lipsum[\inputlineno] \begin{figure}[H]\centering \LaTeX\LaTeX \caption{my figure} \end{figure} \lipsum[\inputlineno] \begin{figure}[H]\centering \LaTeX\TeX \caption{another figure} \end{figure} \section{Second section} \lipsum[\inputlineno] \begin{table}[H]\centering \TeX\LaTeX \caption{my table} \end{table} \lipsum[\inputlineno] \IGNORE \begin{table}[H]\centering \TeX\TeX \caption{another table} \end{table} \ENDIGNORE \lipsum[\inputlineno] \begin{longtable}{|c|c|c|c|} \caption{A simple longtable example}\\ \hline \textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\ \hline \endfirsthead \multicolumn{4}{c}% {\tablename\ \thetable\ -- \textit{Continued from previous page}} \\ \hline \textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\ \hline \endhead \hline \multicolumn{4}{r}{\textit{Continued on next page}} \\ \endfoot \hline \endlastfoot 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ \end{longtable} \end{document}