Retrieving page number of table for a "list of tables"-like list
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}