我正在用这个回答控制文档中浮动标题的外观和顺序。相关部分是:
\renewcommand{\thetable}{S\arabic{table}}
\usepackage{tocloft}
\makeatletter
\newcommand{\listfloatname}{\normalsize Contents:}
\newlistof{float}{flt}{\listfloatname}
\long\def\@caption#1[#2]#3{%
\par
\refstepcounter{float}%
\addcontentsline{\csname ext@#1\endcsname}{#1}%
{\protect\numberline{\csname the#1\endcsname}{\ignorespaces #2}}%
\addcontentsline{flt}{#1}%
{\protect\numberline{\csname the#1\endcsname}{ #2}}%
\begingroup
\@parboxrestore
\if@minipage
\@setminipage
\fi
\normalsize
\@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}\par
\endgroup}
\makeatother
这用于按在特定文档中出现的顺序标记浮点数。但是,当我尝试使用\ref
它来引用浮点数时,不会出现额外的 S 字符。如果我删除上述部分,它就可以完美运行,但我需要保留该功能。
以下是 MWE:
\documentclass{article}
\usepackage{tocloft}
\renewcommand{\thetable}{S\arabic{table}}
%% TOC handling
\makeatletter
\newcommand{\listfloatname}{\normalsize Contents:}
\newlistof{float}{flt}{\listfloatname}
\long\def\@caption#1[#2]#3{%
\par
\refstepcounter{float}%
\addcontentsline{\csname ext@#1\endcsname}{#1}%
{\protect\numberline{\csname the#1\endcsname}{\ignorespaces #2}}%
\addcontentsline{flt}{#1}%
{\protect\numberline{\csname the#1\endcsname}{ #2}}%
\begingroup
\@parboxrestore
\if@minipage
\@setminipage
\fi
\normalsize
\@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}\par
\endgroup}
\makeatother
\begin{document}
\begin{table}
\caption[Short title]{A table \label{table:a_label}}
\begin{tabular}{c|c}
1 & 2 \\
3 & 4
\end{tabular}
\end{table}
As in \ref{table:a_label}
\end{document}
答案1
我花了几分钟才明白为什么这个引用是错误的。
嗯,table
计数器已经增加了\caption
\@caption
在调用之前,\refstepcounter{float}
,因此将覆盖\@currentlabel
用于\label
将相关信息写入.aux
文件的定义。
解决方案:只要您的特定浮点数不应被称为float
不要使用\refstepcounter
但\stepcounter
。
\documentclass{article}
\usepackage{tocloft}
\renewcommand{\thetable}{S\arabic{table}}
%% TOC handling
\makeatletter
\newcommand{\listfloatname}{\normalsize Contents:}
\newlistof{float}{flt}{\listfloatname}
\long\def\@caption#1[#2]#3{%
\par
\stepcounter{float}% No refstepcounter here!
\addcontentsline{\csname ext@#1\endcsname}{#1}%
{\protect\numberline{\csname the#1\endcsname}{\ignorespaces #2}}%
\addcontentsline{flt}{#1}%
{\protect\numberline{\csname the#1\endcsname}{ #2}}%
\begingroup
\@parboxrestore
\if@minipage
\@setminipage
\fi
\normalsize
\@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}\par
\endgroup}
\makeatother
\begin{document}
\listoffloat
\begin{table}
\caption[Short title]{A table \label{table:a_label}}
\begin{tabular}{c|c}
1 & 2 \\
3 & 4
\end{tabular}
\end{table}
As in \ref{table:a_label}
\end{document}
答案2
如果你要做的唯一一件事就是创建一个文件来包含你的每个浮点数(可能是s 和/或s.flt
的组合),你不必重新定义整个。相反,使用figure
table
\@caption
etoolbox
补丁;它更干净:
\documentclass{article}
\usepackage{tocloft,etoolbox}
\renewcommand{\thetable}{S\arabic{table}}
\newcommand{\listfloatname}{\normalsize Contents:}
\newlistof{float}{flt}{\listfloatname}
\makeatletter
\patchcmd{\@caption}% <cmd>
{\addcontentsline}% <search>
{\addcontentsline{flt}{#1}{\protect\numberline{\csname the#1\endcsname}{#2}}%
\addcontentsline}% <replace>
{}{}% <success><failure>
\makeatother
\begin{document}
\listoffloat
\begin{table}
\centering
\caption[Short title]{A table \label{table:a_label}}
\begin{tabular}{c|c}
1 & 2 \\
3 & 4
\end{tabular}
\end{table}
As in \ref{table:a_label}.
\end{document}
您甚至可以在每次发送应该放入或的内容时,适应将\addcontentsline
内容插入到文件中:.flt
.lof
.lot
\documentclass{article}
\usepackage{tocloft}
\renewcommand{\thetable}{T\arabic{table}}
\renewcommand{\thefigure}{F\arabic{figure}}
\newcommand{\listfloatname}{\normalsize Contents:}
\newlistof{float}{flt}{\listfloatname}
\let\oldaddcontentsline\addcontentsline
\renewcommand{\addcontentsline}[3]{%
\oldaddcontentsline{#1}{#2}{#3}% Default addition to contents file
\ifnum\pdfmatch{#1}{lot,lof}=1 % A table or figure...
\oldaddcontentsline{flt}{#2}{#3}% ...also add to .flt
\fi
}
\begin{document}
\listoffloat
\section{A section}
A table~\ref{tbl:table} and figure~\ref{fig:figure}.
\begin{table}
\centering
\caption[Short title]{A table \label{tbl:table}}
\end{table}
\begin{figure}
\centering
\caption[Short figure]{A Figure \label{fig:figure}}
\end{figure}
\end{document}