在回憶錄中實作固定標籤

在回憶錄中實作固定標籤

我想以這樣的方式實現一個\newfixedlabel類似回憶錄的 命令:\newfixedcaption

\begin{myfloat}
\caption{...}
\label{myfloat:a}
...
\end{myfloat}

使用(或任何其他套件)產生相同的結果cleveref

\myfloatfixedcaption{...}% setup via memoirs \newfixedcaption
\label{myfloat:a}
...

這個巨集背後的原因是,我總是可以在(文件)寫入過程的任何階段將浮點數更改為非浮點數,而不會丟失浮點數的標籤和標題功能。

一個完整的範例可能如下所示:

\documentclass{memoir}
\usepackage{etoolbox}
\usepackage{hyperref}
\usepackage{cleveref}

\makeatletter
\newcommand*{\cftonlycaption}[2][]%
{\begingroup
   \let\@makecaption\@gobbletwo
   \ifstrempty{#1}%
     {\caption{#2}}%
     {\caption[#1]{#2}}
 \endgroup}
\makeatother

\newfloat[chapter]{myfloat}{lmf}{My Float}
\newfixedcaption[\cftonlycaption]{\myfloatfixedcaption}{myfloat}

\crefname{myfloat}{My Float}{My Floats}

\begin{document}
\chapter{A}
\begin{myfloat}
\caption{A}% or \myfloatfixedcaption{A}
\label{keya}
\end{myfloat}
\section{B}
\myfloatfixedcaption{B}
\label{keyb}
\cref{keya}
\cref{keyb}
\end{document}

由於某種原因,該\label{keyb}命令指向 B 部分而不是 My Float。

詩。我知道如何設定 smartef \crefname,問題在於建立\newfixedlabel巨集。我嘗試了與回憶錄相同的技巧,透過定義 \@captype 來模擬浮動環境,但沒有成功。

答案1

在群組內使用時\caption,某些更新將無法保留。特別是,必要的\caption更新\cref@currentlabelcleveref以便檢測適當的引用類型。

您可以使用以下命令更新您的類型\cftonlycaption以專門引用該類型:myfloat

\makeatletter
\newcommand*{\cftonlycaption}[2][]%
{\begingroup
   \let\@makecaption\@gobbletwo
   \ifstrempty{#1}%
     {\caption{#2}}%
     {\caption[#1]{#2}}
 \endgroup
 \protected@edef\cref@currentlabel{%
      \expandafter\cref@override@label@type%
        \cref@currentlabel\@nil{myfloat}}}
\makeatother

結局\cref@currentlabel更新外部該群組會將目前引用類型儲存為myfloat.

或者,如果這看起來太嚴格,您始終可以使用本地覆蓋引用類型

\label[myfloat]{keyb}

下面是使用前一種方法的最小範例:

在此輸入影像描述

\documentclass{memoir}
\usepackage{etoolbox}
\usepackage{hyperref}
\usepackage{cleveref}

\makeatletter
\newcommand*{\cftonlycaption}[2][]%
{\begingroup
   \let\@makecaption\@gobbletwo
   \ifstrempty{#1}%
     {\caption{#2}}%
     {\caption[#1]{#2}}
 \endgroup
 \protected@edef\cref@currentlabel{%
      \expandafter\cref@override@label@type%
        \cref@currentlabel\@nil{myfloat}}}
\makeatother

\newfloat[chapter]{myfloat}{lmf}{My Float}
\newfixedcaption[\cftonlycaption]{\myfloatfixedcaption}{myfloat}

\crefname{myfloat}{My Float}{My Floats}

\begin{document}
\chapter{A}
\begin{myfloat}
\caption{A}% or \myfloatfixedcaption{A}
\label{keya}
\end{myfloat}
\section{B}
\myfloatfixedcaption{B}
\label{keyb}% or \label[myfloat]{keyb}
\cref{keya}
\cref{keyb}
\end{document}

請注意,排尿\@makecaption(通過\let\@makecaption\@gobbletwo)會導致hyperref錯過了合適的跳躍位置。

相關內容