6

Let me please introduce a bit of context first: Building up from Gonzalo Medina's answer to Include chapters in List of Figures with titletoc?, I added the same features to a List of Tables. While everything went fine with my List of Figures, the List of Tables got the chapter line (for each new chapter where a table occurs) saved to the \jobname.lot file after the line corresponding to the first table.

There is something related with the contents of the chapters in my document that leads to this weird result, as I've tried with exactly the same class and packages but with MWE-like chapters and everything went fine about the tables, too. This is the reason why I'm not asking here about the occurrence of this strange feature --it may be something lurking in my own chapter files, although I haven't been able to disclose what it may be and what I've been checking results in really weird stuff. Nonetheless, if somebody has seen the same feature occurring, any suggestions about its origin will be welcome.

I'm trying with a different approach: My class is based on memoir and I'm using a new file extension (lotb) to store the data about the chapters and the tables in the file \jobname.lotb using the same approach as put forward by Gonzalo Medina (except for a few minor changes based on memoir). Everything runs smoothly with this approach, but I don't know how I'm supposed to retrieve the page numbers for the tables in order to take them to the \jobname.lotb file.

What follows is an MWE, my true setting being much more complex than this.

Also, any suggestions on how to improve my approach overall would be very welcome.

\documentclass{memoir}

\usepackage{etoolbox}

\makeatletter
 \def\thischaptertitle{}
 \def\thischapternumber{}
 \def\thistabtitle{}
 \newtoggle{noFigs}
 \newtoggle{noTabs}

 %<- My new "list of"
 \def\listoftabsname{Tables}
 \newlistof{listoftabs}{lotb}{\listoftabsname}
 \def\tabsmark{\listoftabsname}

 \AtBeginDocument{%
   % I part ways slightly from Gonzalo Medina's answer by resorting 
   % to a memoir-based hook 
   \apptocmd{\memendofchapterhook}{%
     \gdef\thischaptertitle{\f@rtoc}%
     \gdef\thischapternumber{\thechapter}%
     \global\toggletrue{noTabs}%
     \global\toggletrue{noFigs}%
   }{}{}

   % This is going to be used to get the content of the caption of a table. 
   % But, how do I get the page number?
   % Plus, I guess it can be done much better than this 
   \apptocmd{\@caption}{%
     \gdef\thistabtitle{\@currentlabelname}%
     \gdef\thistabpage{}%<- How to?
   }{}{}

   % In my true setting I have to do this AtEnd, instead of AtBeginning
   \AtEndEnvironment{table}{%
     \iftoggle{noTabs}{
       \global\togglefalse{noTabs}
       \addtocontents{lotb}{%
         \protect\contentsline {chapter}%
         {\protect\numberline {\thischapternumber}{\protect\ignorespaces\thischaptertitle}}{}{}
         \protect\vskip0.125\protect\baselineskip}%\addvspace{5\p@}}}
     }{}
   }
   % This goes AtEndEnvironment{table} in order to have the right counter value;
   % otherwise, it is lagging one unit behind; e.g., the first is \thechapter.0
   \AtEndEnvironment{table}{%
     \addtocontents{lotb}{%
       \protect\contentsline {table}%
       {\protect\numberline {\thetable}{\protect\ignorespaces\thistabtitle}}{\thistabpage}{}
       \protect\vskip0.125\protect\baselineskip}%\addvspace{5\p@}}}
   }%

   % Everything runs smoothly for the figures  
   \AtBeginEnvironment{figure}{%
     \iftoggle{noFigs}{
       \addtocontents{lof}{\protect\contentsline {chapter}%
         {\protect\numberline {\thischapternumber}{\protect\ignorespaces \thischaptertitle}}{}{}
         \protect\vskip0.125\protect\baselineskip}%\addvspace{5\p@}}
       \global\togglefalse{noFigs}
     }{}
   }%
 }
\makeatother

% I'm using all of the packages below (and more) in my original setting.
% They appear here just because I think they can be relevant in order to provide a context 
% for any possible answers to work in my real-life scenario  

\usepackage[figurewithin=chapter,tablewithin=chapter]{caption}
\usepackage{titletoc}
\usepackage[toctitles,explicit]{titlesec}
\usepackage{hyperref}
\usepackage{cleveref}

\begin{document}
\tableofcontents
\listoffigures*
\listoftabs
\chapter{One}
\begin{figure}
  One
  \caption{One.}
\end{figure}

\begin{table}
    \caption{One.}
  One
\end{table}

\begin{table}
  \caption{Oneb.}
  Oneb
\end{table}

\chapter{Two}

\begin{figure}
  Two
  \caption{Two.}
\end{figure}

\begin{table}
  Two
  \caption{Two.}
\end{table}

\chapter{Three}

\begin{figure}
  Three
  \caption{Three.}
\end{figure}

\begin{table}
  Threeb
  \caption{Threeb.}
\end{table}
\end{document}

EDIT: My original problem, which brought all of the above on stage, has been solved with the approach I explain in my non-answer post below.

2
  • \gdef\thistabpage{\thepage} works for me, but your approach does not allow for hyperlinks
    – user31729
    Aug 28, 2016 at 4:04
  • I had not even tried it! But your comment points to one of the reasons why I'm not fond of my hacking here. Any amendments would be great, including to my overall approach.
    – Marcos
    Aug 28, 2016 at 4:09

2 Answers 2

6

Using a normal \addcontentsline in the 2nd \AtEndEnvironment{table} is cleaner and easier, and it provides the correct hyperanchors as well. The additional vertical spacing is written with \addtocontents.

In fact, \addcontentsline is a wrapper for \addtocontents{...}{\protect\contentsline{...}{...}{...}}, letting it do whatever it is designed to do using the default style will guarantee the correct page numbers and hyperanchors are used.

In principle, the same could be done for the extra chapter lines in the .lotb.

\documentclass{memoir}

\usepackage{etoolbox}

\makeatletter
\def\thischaptertitle{}
\def\thischapternumber{}
\def\thistabtitle{}
\newtoggle{noFigs}
\newtoggle{noTabs}

% <- My new "list of"
\def\listoftabsname{Tables}
\newlistof{listoftabs}{lotb}{\listoftabsname}
\def\tabsmark{\listoftabsname}

\AtBeginDocument{%
  % I part ways slightly from Gonzalo Medina's answer by resorting 
  % to a memoir-based hook 
  \apptocmd{\memendofchapterhook}{%
    \gdef\thischaptertitle{\f@rtoc}%
    \gdef\thischapternumber{\thechapter}%
    \global\toggletrue{noTabs}%
    \global\toggletrue{noFigs}%
  }{}{}

  % This is going to be used to get the content of the caption of a table. 
  % But, how do I get the page number?
  % Plus, I guess it can be done much better than this 
  \apptocmd{\@caption}{%
    \gdef\thistabtitle{\@currentlabelname}%
    \gdef\thistabpage{\thepage}%<- This is the how-to
  }{}{}

  % In my true setting I have to do this AtEnd, instead of AtBeginning
  \AtEndEnvironment{table}{%
    \iftoggle{noTabs}{
      \global\togglefalse{noTabs}
      \addtocontents{lotb}{%
        \protect\contentsline {chapter}%
        {\protect\numberline {\thischapternumber}{\protect\ignorespaces     \thischaptertitle}}{\thepage}{}
        \protect\vskip0.125\protect\baselineskip}%\addvspace{5\p@}}}
    }{}
   }
   % This goes AtEndEnvironment{table} in order to have the right counter value;
   % otherwise, it is lagging one unit behind; e.g., the first is \thechapter.0
   \AtEndEnvironment{table}{%
     \addcontentsline{lotb}{table}{\protect\numberline {\thetable}{\protect\ignorespaces     \thistabtitle}}
%     \addtocontents{lotb}{%
%       \protect\contentsline {table}%
%       {\protect\numberline {\thetable}{\protect\ignorespaces     \thistabtitle}}{\thistabpage}{\thetable.\theHtable}
     \addtocontents{lotb}{\protect\vskip0.125\protect\baselineskip}%\addvspace{5\p@}}}
   }%

   % Everything runs smoothly for the figures  
   \AtBeginEnvironment{figure}{%
     \iftoggle{noFigs}{
       \addtocontents{lof}{\protect\contentsline {chapter}%
         {\protect\numberline {\thischapternumber}{\protect\ignorespaces \thischaptertitle}}{}{}
         \protect\vskip0.125\protect\baselineskip}%\addvspace{5\p@}}
       \global\togglefalse{noFigs}
     }{}
   }%
 }
\makeatother

% I'm using all of the packages below (and more) in my original setting.
% They appear here just because I think they can be relevant in order to provide a context 
% for any possible answers to work in my real-life scenario  

\usepackage[figurewithin=chapter,tablewithin=chapter]{caption}
\usepackage{titletoc}
\usepackage[toctitles,explicit]{titlesec}
\usepackage{hyperref}
\usepackage{cleveref}

\begin{document}
\tableofcontents
\listoffigures*
\listoftabs
\chapter{One}
\begin{figure}
  One
  \caption{One.}
\end{figure}

\begin{table}
    \caption{One.}
  One
\end{table}

\begin{table}
  \caption{Oneb.}
  Oneb
\end{table}

\chapter{Two}

\begin{figure}
  Two
  \caption{Two.}
\end{figure}

\begin{table}
  Two
  \caption{Two.}
\end{table}

\chapter{Three}

\begin{figure}
  Three
  \caption{Three.}
\end{figure}

\begin{table}
  Threeb
  \caption{Threeb.}
\end{table}
\end{document}

enter image description here

4
  • 2
    It works perfectly. Thank you very much for going further than just the page topic and for the explanations provided.
    – Marcos
    Aug 28, 2016 at 4:26
  • @Marcos: You're welcome. Actually, the page number issue puzzled me right from the start and then I wondered why \addcontentsline isn't used here. \addtocontents is the more powerful command of course to write 'anything' to a contents file, but care has to be taken if hyperref is involved, since hyperref extends \contentsline considerably!
    – user31729
    Aug 28, 2016 at 4:28
  • Lists of contents are still amongst the least clear topics to me. Thanks again for the insights provided, which I believe will prove helpful in the future, too. Btw, in the midst of all the captioning macros and lists of contents stuff, using simply \thepage didn't even come across my mind.
    – Marcos
    Aug 28, 2016 at 4:36
  • 1
    @Marcos: The reason why \thepage can be used is that the value of the page counter (or better: the formatting of the page counter value) is written explicitly at the position where \addcontentsline is used, i.e. it has the correct page value directly written to the contents file. See lines 5936f of latex.ltx for more information on this -- it's quite readable code there... and yes, I've have answered 'one' or 'two' ToC - related questions here ;-)
    – user31729
    Aug 28, 2016 at 4:43
0

This is more of an amendment or an alternative approach than an answer to my post properly, but I think it may be useful to somebody anyway, and I like it much better than my original approach. It still borrows from Gonzalo Medina's contribution and, of course, from Christian Hupfer's --which I take as the answer to my post.

The basics of my current approach rely on hacking the captioning command instead of using the environments hooks. In this way I enhance the original approach insofar as the non-floats captioned via the \captionof command are also written to the corresponding list of contents, which was not the case by hacking the float-based environments. In my setting, it is all about table and non-floating tabular-like environments, which is why I make the hack testing (\ifthenelse command from the xifthen package) for table-based captions. The same can be done for figures. Of course, the hooks on the environments for the type of float considered are to be removed.

I introduce the hack below. For the sake of brevity I do not repeat the entire MWE, but if someone thinks I should type it entirely again, please feel free to let me know.

\apptocmd{\@caption}{%
  \gdef\thistabtitle{\@currentlabelname}%
  \gdef\thistabpage{\thepage}%
  %
  \ifthenelse{\equal{\@captype}{table}}{%
    \iftoggle{noTabs}{%
      \global\togglefalse{noTabs}
      \addtocontents{lotb}{%
        \protect\contentsline {chapter}%
        {\protect\numberline {\thischapternumber}%
           {\protect\ignorespaces \thischaptertitle}}{}{}}%
      }{}%
      \addcontentsline{lotb}{table}%
        {\protect\numberline {\thetable}%
          {\protect\ignorespaces  \thistabtitle}}}{}
}{}{}

EDIT: Well, after testing the captioning hack for figures, too, I found that my original problem about the list of tables, which brought me here, was solved by using \pretocmd and forgetting about the hooks on to the environments. I don't even need a new list of contents anymore! Writing to the default list of tables (\jobname.lot) works perfectly. Thus, the "solve-it-all" hack for me is as follows, including both tables and figures (further code enhancements will be welcome):

\newcommand{\addchapterto}[1]{%
  \addtocontents{#1}{%
    \protect\contentsline {chapter}%
    {\protect\numberline {\thischapternumber}{\protect\ignorespaces \thischaptertitle}}{}{}}%
  \addtocontents{#1}{\protect\vskip0.125\protect\baselineskip}%
}

\pretocmd{\@caption}{%
  \ifthenelse{\equal{\@captype}{table}}{%
    \iftoggle{noTabs}{
      \global\togglefalse{noTabs}
      \addchapterto{lot}
    }{}
  }{%
     \ifthenelse{\equal{\@captype}{figure}}{%
       \iftoggle{noFigs}{
         \global\togglefalse{noFigs}
         \addchapterto{lof}
       }{}%
     }{}%
  }%
}{}{}
1
  • If you want to omit the pagenumbers for the chapter entries in the LoT you can use the \addtocontents approach, of course, otherwise, a simple \addcontentsline{toc}{chapter}{....} would do to!
    – user31729
    Aug 29, 2016 at 12:09

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.