newfloat、自定义引用宏和 listof 的问题

newfloat、自定义引用宏和 listof 的问题

我已经编写了宏来为章节和部分等生成“名称空间”。这一切都很顺利,但 newfloat 的 listof 功能(或下属的 tocloft)似乎破坏了它:

\documentclass{report}

\usepackage{newfloat}

%section magic
\expandafter\providecommand\csname usr@chapterlabel\endcsname[0]{}
\newcommand{\locallabel}[2][]{\label{#1:\csname usr@chapterlabel\endcsname:#2}}
\newcommand{\localref}[2][]{\ref{#1:\csname usr@chapterlabel\endcsname:#2}}


\newcommand{\smartchapter}[2][]{%
\chapter{#2}%
\label{c:#1}%
\expandafter\renewcommand\csname usr@chapterlabel\endcsname[0]{#1}%
}

% Setup new float env
\DeclareFloatingEnvironment[name=Test,listname={List of Tests}]{testfloat}

\begin{document}
\tableofcontents
\listoftestfloats

\smartchapter[a]{A chapter using localref}
In this chapter we show content A (\localref[test]{a}) and content B (\localref[test]{b}) using localref.

\begin{testfloat}
Content A
\caption{Here is a caption for A}
\locallabel[test]{a}
\end{testfloat}

\begin{testfloat}
Content B
\caption{A caption with a localref to \localref[test]{a} which can be written as a normal ref like so \ref{test:a:a}}
\locallabel[test]{b}
\end{testfloat}
\end{document}

在我的主文档中,所有localref工作都已完成。然而,listoftestfloats内容 B 的条目排版如下:

A caption with a localref to ?? which can be written as a normal ref like so 1.1

标题中的正确排版如下:

A caption with a localref to 1.1 which can be written as a normal ref like so 1.1

我进一步收到警告:

(/compile/output.lotestfloat
LaTeX Font Info:    External font `cmex10' loaded for size
(Font)              <7> on input line 2.
LaTeX Font Info:    External font `cmex10' loaded for size
(Font)              <5> on input line 2.

LaTeX Warning: Reference `test::a' on page 2 undefined on input line 3.

)
\tf@lotestfloat=\write5
\openout5 = `output.lotestfloat'

因此,很明显,它试图localref在章节之外的某些上下文中评估(这很明显,因为当为空时test::a,结果会是这样的)。我还没能找到解决这个问题的方法。\localref[test]{a}usr@chapterlabel

相关内容