Implementieren Sie fixedlabel in Memoir

Implementieren Sie fixedlabel in Memoir

Ich möchte einen Befehl namens \newfixedlabelin Analogie zu Memoiren \newfixedcaptionso implementieren, dass:

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

cleverefErgibt mit (oder einem anderen Paket) dasselbe Ergebnis wie:

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

Der Grundgedanke hinter diesem Makro besteht darin, dass ich in jeder Phase des (Dokument-)Schreibvorgangs einen Float-Wert immer in einen Nicht-Float-Wert ändern kann, ohne die Beschriftungs- und Überschriftenfunktionen eines Float-Werts zu verlieren.

Ein vollständiges Beispiel könnte wie folgt aussehen:

\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}

Aus irgendeinem Grund \label{keyb}verweist der Befehl auf Abschnitt B statt auf My Float.

Ps. Ich weiß, wie man cleveref einrichtet \crefname, das Problem liegt in der Erstellung des \newfixedlabelMakros. Ich habe denselben Trick wie Memoir ausprobiert, um eine Float-Umgebung nachzuahmen, indem ich \@captype definiert habe, aber ohne Erfolg.

Antwort1

Bei der Nutzung \captioninnerhalb einer Gruppe bleiben bestimmte Updates nicht erhalten. Insbesondere \captionUpdates , \cref@currentlabeldie notwendig sind fürcleverefum den passenden Referenztyp zu erkennen.

Sie können Ihre Aktualisierung folgendermaßen durchführen \cftonlycaption, um speziell auf den Typ zu verweisen :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

Das abschließende \cref@currentlabelUpdatedraußendie Gruppe speichert den aktuellen Referenztyp als myfloat.

Wenn Ihnen dies zu starr erscheint, können Sie den Referenztyp auch lokal überschreiben mit

\label[myfloat]{keyb}

Nachfolgend sehen Sie ein Minimalbeispiel für den früheren Ansatz:

Bildbeschreibung hier eingeben

\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}

Beachten Sie, dass das Entleeren \@makecaption(über \let\@makecaption\@gobbletwo) dazu führt,hyperrefdie entsprechende Absprungstelle zu verfehlen.

verwandte Informationen