我想在自己的图片列表中添加子图片标题(如“61(a)”)。我的代码(参见下面的示例文档)运行正常,但只要我额外加载caption
或subcaption
包,我的代码就会产生一个空列表。
我需要的是我的\tableoffigures
命令必须与以下命令配合:
\usepackage[...,list=true]{subcaption}
示例文档:
\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}
答案1
由于\caption
在许多环境中需要重新定义(subfigure
,,longtable
...),这种方法修补了标题包,将处理所有不同实现的负担转交给\caption
它。
缺点:
- 仅当加载了字幕包时,此解决方案才有效。
- 该解决方案修补了标题包的内部命令。
- 由于我们没有修补,所以
\caption
我们需要一个额外的命令来指定源。(我\source
在下面的示例代码中调用了它。)
\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}