1

I want to add subfigure captions (like "61(a)") in my own list of figures. My code (see example document below) works fine, but as soon as I load the caption or subcaption package additionally my code results in an empty list.

What I need is that my \tableoffigures command has to get along with the following command:

\usepackage[...,list=true]{subcaption}

Example document:

\documentclass{report}
%\usepackage{caption} %figure caption
%\usepackage[list=true,lofdepth=2]{subcaption} %subfigure caption
\usepackage{longtable,letltxmacro}

\newcounter{fullfigurecounter}
\newcounter{fulltablecounter}

\makeatletter

\newcommand*\@starttof[2]{%
    \begingroup
    \renewcommand{\arraystretch}{1.5}
    % Table format may be changed%%%
    \begin{longtable}{|p{2cm}|p{6cm}|p{7cm}|}
%       \hline
        \multicolumn{3}{c}{}\tabularnewline
        \multicolumn{3}{c}{\Large \bfseries #2}\tabularnewline
        \multicolumn{3}{c}{}\tabularnewline
%       \hline
        \multicolumn{3}{c}{}\tabularnewline[0.5ex]
        \hline
        \large Table No.  & \large Title of the Table & \large Source \tabularnewline
        \hline
        \endhead % Repeating head
        \@input{\jobname.#1}%  Input the ToF or ToT file 
        \tabularnewline
        \hline
    \end{longtable}
    \setcounter{table}{0}%
    \if@filesw
    \expandafter\newwrite\csname tf@#1\endcsname
    \immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
    \fi
    \endgroup
}

\newcommand{\tableoffigures}{%
    \cleardoublepage
    \@starttof{tof}{Abbildungs- und Quellenverzeichnis}
}%

\newcommand{\tableoftables}{%
    \clearpage
    \@starttof{tot}{LIST OF TABLES}%
}%

\def\temp@@a{figure}%
\def\temp@@b{table}%

\LetLtxMacro\captionpkg@caption\caption

\newcommand{\writetofline}[3]{%
    \ifnum\value{fullfigurecounter} > 1
    \tabularnewline
    \protect\hline
    \fi
    \figurename\ #1 & #2 & #3 
}

\newcommand{\writetotline}[3]{%
    \ifnum\value{fulltablecounter} > 1
    \tabularnewline
    \protect\hline
    \fi
    \tablename\ #1 & #2 & #3 
}

\RenewDocumentCommand{\caption}{somO{Eigene Darstellung}}{%
    \IfBooleanTF{#1}{%
        \captionpkg@caption{#3}%
    }{%
        \ifx\@currenvir\temp@@a
        \stepcounter{fullfigurecounter}%
        \else
        \ifx\@currenvir\temp@@b
        \stepcounter{fulltablecounter}%
        \fi
        \fi
        \IfValueTF{#2}{%
            \captionpkg@caption[#2]{#3}%
            \ifx\@currenvir\temp@@a
            \addtocontents{tof}{\writetofline{\thefigure}{#2}{#4}}%
            \else
            \ifx\@currenvir\temp@@b
            \addtocontents{tot}{\writetotline{\thetable}{#2}{#4}}%
            \fi
            \fi
        }{%
            \captionpkg@caption{#3}%
            \ifx\@currenvir\temp@@a
            \addtocontents{tof}{\writetofline{\thefigure}{#3}{#4}}%
            \else%
            \ifx\@currenvir\temp@@b
            \addtocontents{tot}{\writetotline{\thetable}{#3}{#4}}%
            \fi
            \fi
        }%
    }%
}

\makeatother

\begin{document}

\tableoffigures
\clearpage
\tableoftables 
\clearpage

\chapter{A sample for multicol table of contents}
\begin{table}
    \caption[A nice table]{A nice table}[Me!]
\end{table}
\clearpage
\begin{figure}
    \caption{Irgendein Bild}
\end{figure}
\begin{figure}
    \caption[Die Sonnenblumen]{Irgendein Bild}[Vincent van Gogh]
\end{figure}
\clearpage
\begin{table}[!ht]
    \caption[List of $ \theta $ signature]{$\theta$ signature}
    \label{theta}
    \centering
    \begin{tabular}{|c|*{6}{c|}}
        \hline
%       \backslashbox{$ \boldsymbol{\theta} $}{signature}& \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & $ \boldsymbol{\cdots} $ & $ \boldsymbol{t} $\\ \hline
        \textbf{1} & 1 &    2 & 3&  4&  $ \ldots $ &    1+c \\
        \hline
        \textbf{4} &    5 & 6 &     \multicolumn{3}{c|}{$ \ldots $} & 7\\ \cline{4-5}
        \hline
        $ \vdots $ &    \multicolumn{6}{c|}{$ \ldots $} \\ \cline{2-6}
        \hline
    \end{tabular}
\end{table}

\ifx\subfigure\undefined\else

\begin{figure}
    \begin{subfigure}{.5\linewidth}
        \caption[flower]{}
    \end{subfigure}
\end{figure}
\begin{figure}\ContinuedFloat
    \begin{subfigure}{.5\linewidth}
        \caption[animal]{}
    \end{subfigure}
\end{figure}

\fi

\end{document}
4
  • 1
    It seems your redefinition of \caption only supports environmants figure and table but not subfigure or subtable and also not using \caption inside another environment inside a figure or table. So I would call is: broken by design. \caption should never depend on \@currenvir. Maybe depending on \@captype could be used, but not to compare it, but for something like \csname the\@captype\endcsname, \csname fnum@\@captype\endcsname etc.
    – cabohah
    Commented Mar 23, 2023 at 14:40
  • i tried.. but not getting expected output. may i ask you to upload the correct code if possible.
    – Nandy
    Commented Mar 24, 2023 at 7:44
  • 1
    Sorry, but the shown code is not a minimal working example and has to many errors (starting with option English instead of english to babel) to fix all of them. So you should first reduce it and make it compile without error messages.
    – cabohah
    Commented Mar 24, 2023 at 7:48
  • can you help me out with this problem. else refer me some article or site to modify my code. because searching for creating my own list of figures along with subfigure caption to be made. but its complicated. kindly do helpme..
    – Nandy
    Commented Mar 24, 2023 at 7:53

1 Answer 1

0

Since \caption is subject to re-definition for many environments (subfigure, longtable, ...), this approach patches the caption package instead, forwarding the burden of dealing with all the different \caption implementations to it.

Disadvantages:

  1. This solution works only if the caption package is loaded.
  2. This solution patches an internal command of the caption package.
  3. Since we do not patch \caption we need an extra command to specify the source. (I have called it \source in my example code below.)
\documentclass{report}
\usepackage[listformat=simple]{caption} %figure caption
\usepackage[list=true,lofdepth=2]{subcaption} %subfigure caption
\usepackage{longtable}

\makeatletter

\newcommand*\@starttof[2]{% (not altered)
    \begingroup
    \renewcommand{\arraystretch}{1.5}
    % Table format may be changed%%%
    \begin{longtable}{|p{2cm}|p{6cm}|p{7cm}|}
%       \hline
        \multicolumn{3}{c}{}\tabularnewline
        \multicolumn{3}{c}{\Large \bfseries #2}\tabularnewline
        \multicolumn{3}{c}{}\tabularnewline
%       \hline
        \multicolumn{3}{c}{}\tabularnewline[0.5ex]
        \hline
        \large Table No.  & \large Title of the Table & \large Source \tabularnewline
        \hline
        \endhead % Repeating head
        \@input{\jobname.#1}%  Input the ToF or ToT file 
    \end{longtable}
%   \setcounter{table}{0}% What is this line good for?
    \if@filesw
    \expandafter\newwrite\csname tf@#1\endcsname
    \immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
    \fi
    \endgroup
}

\newcommand{\tableoffigures}{% (not altered)
    \cleardoublepage
    \@starttof{tof}{Abbildungs- und Quellenverzeichnis}
}%

\newcommand{\tableoftables}{% (not altered)
    \clearpage
    \@starttof{tot}{LIST OF TABLES}%
}%

\newcommand{\writetofline}[4]{% (one argument added)
    #1~#2 & #3 & #4%
    \tabularnewline
    \protect\hline
}

% New \source{...} command (avoiding patching \caption)
\newcommand*\our@source{}
\newcommand*\source[1]{\renewcommand\our@source{#1}}

% Patch the caption package code which actually writes an entry into the list of figures/tables
% WARNING: We are patching an internal command of the caption package here
\let\caption@@@addcontentsline@ORI\caption@@@addcontentsline
\renewcommand*\caption@@@addcontentsline[4]{%
  % #1 = "lof" / "lot"
  % #2 = "figure" / "table" / "subfigure" / "subtable"
  % #3 = caption list number (content could be altered using option "listformat=...")
  % #4 = caption list entry (as given in \caption)
  % \@captype = "figure" / "table"
  %
  % Make entry in regular List of Figures/Tables (this could be dropped if no regular lof/lot is desired)
  \caption@@@addcontentsline@ORI{#1}{#2}{#3}{#4}%
  % lof -> tof / lot -> tot
  \edef\our@ext{#1}\edef\our@ext{t\expandafter\@gobble\our@ext}%
  % Set default source, if none was set by the user
  \ifx\our@source\@empty
      \def\our@source{Eigene Darstellung}%
  \fi
  % Add contents line to tof/tot
  \addtocontents{\our@ext}{\writetofline{\@nameuse{\@captype name}}{#3}{#4}{\our@source}}%
}

\makeatother

\begin{document}

\tableoffigures
\clearpage
\tableoftables 
\clearpage

\chapter{A sample for multicol table of contents}
\begin{table}
    \source{Me!}
    \caption[A nice table]{A nice table}
\end{table}
\clearpage
\begin{figure}
    \caption{Irgendein Bild}
\end{figure}
\begin{figure}
    \source{Vincent van Gogh}
    \caption[Die Sonnenblumen]{Irgendein Bild}
\end{figure}
\clearpage
\begin{table}[!ht]
    \caption[List of $ \theta $ signature]{$\theta$ signature}
    \label{theta}
    \centering
    \begin{tabular}{|c|*{6}{c|}}
        \hline
%       \backslashbox{$ \boldsymbol{\theta} $}{signature}& \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & $ \boldsymbol{\cdots} $ & $ \boldsymbol{t} $\\ \hline
        \textbf{1} & 1 &    2 & 3&  4&  $ \ldots $ &    1+c \\
        \hline
        \textbf{4} &    5 & 6 &     \multicolumn{3}{c|}{$ \ldots $} & 7\\ \cline{4-5}
        \hline
        $ \vdots $ &    \multicolumn{6}{c|}{$ \ldots $} \\ \cline{2-6}
        \hline
    \end{tabular}
\end{table}

\ifx\subfigure\undefined\else

\begin{figure}
    \begin{subfigure}{.5\linewidth}
        \caption[flower]{}
    \end{subfigure}
\end{figure}
\begin{figure}\ContinuedFloat
    \begin{subfigure}{.5\linewidth}
        \caption[animal]{}
    \end{subfigure}
\end{figure}

\fi

\end{document}
3
  • thank you so much and you save my day. but one problem occurs while compiling, that what i used is xelatex compiler. but it doesn't support xelatex. where it supports only pdftex and luatex. what is the reason behind and what can be done for that? thank you in advance.
    – Nandy
    Commented Mar 28, 2023 at 10:38
  • I can compile the document above without problems using xelatex as well (MikTeX, up-to-date). What error message do you get?
    – user2574
    Commented Mar 28, 2023 at 19:30
  • Drivers other than pdftex' and luatex' are not supported.
    – Nandy
    Commented Apr 4, 2023 at 10:35

You must log in to answer this question.

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