错误在哪里?#1 在 \caption 里面

错误在哪里?#1 在 \caption 里面

我正在尝试自动转换\caption{...\label{...}...}\caption{...}\label{...}。这是我尝试过的:

\documentclass{article}
\usepackage{showlabels}
\newcommand{\storelabel}[1]{\gdef\storedlabel{#1}}
\let\oldcaption\caption
\renewcommand{\caption}[1]{%
    \let\storedlabel\undefined
    \oldcaption{%
        \def\label##1{%
            \protect\storelabel{##1}%
        }%
        #1%
    }%
    \ifdefined\storedlabel%
        \label{\storedlabel}%
    \else%
    \fi%
}
\begin{document}
    \begin{table}
        \caption{no label}
        foo
    \end{table}
    \begin{table}
        \caption{\label{mylabel}label inside caption (front)}
        foo
    \end{table}
    \begin{table}
        \caption{label inside caption (back) \label{mylabel}}
        foo
    \end{table}
    \begin{table}
        \caption{label directly after caption}
        \label{mylabel}
        foo
    \end{table}
    \begin{table}
        \caption{label at end of table (I'm OK with that one)}
        foo
        \label{mylabel}
    \end{table}
\end{document}

pdf 文件看起来也正确,但我收到此错误消息(五次,每次调用一次\caption):

! Illegal parameter number in definition of \reserved@a.
<to be read again> 
                   1
l.20        \caption{no label}

You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I'm going to assume that you meant ##.

如果我\storelabel{##1}用替换\storelabel{mylabel},pdf 文件看起来一样,错误消息也消失了。但这显然不是我想要的。

更新:这个 MWE 很好地显示了同样的错误,\label虽然这与我最初搬出\caption并因此移动showlabels标签的目标无关

\documentclass{article}
\begin{document}
\begin{table}
    \caption{\def\foo[#1]{#1}}
\end{table}
\end{document}

答案1

在我看来,最好是捕获\label并让其将标签名称存储\storedlabel在中,然后在发出后将\caption实际移至\label的末尾。\@caption\@makecaption

重新定义\labelwill breakcleveref\label[...]{foo}风格!

\documentclass{article}
\usepackage{showlabels}
\usepackage{xparse}
\usepackage{letltxmacro}

\usepackage{xpatch}

\usepackage{cleveref} % Just for testing!

\makeatletter

\LetLtxMacro\latex@@origlabel\label

\xpatchcmd{%
  \caption
}{%
  \refstepcounter\@captype%
}{%
  \let\storedlabel\undefined%
  \refstepcounter\@captype
  \begingroup
  \renewcommand{\label}[1]{%
     \gdef\storedlabel{##1}%  Catch the  the label but doing nothing except of storing it!
   }
   \endgroup
 }{\typeout{Successfully patched caption}}{\typeout{Failed in patching caption}}


\xpatchcmd{\@caption}{%
  \@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}\par
  \endgroup}{%
  \@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}\par
  \endgroup%
  \@ifundefined{storedlabel}{}{%  Transfer the label to this place
    \label@@origlabel{\storedlabel}}%
}{\typeout{Success}}{\typeout{Failure!}}
\makeatother

\begin{document}
  \begin{table}
        \caption{no label}
        foo
    \end{table}
    \begin{table}
        \caption{\label{othermylabel}label inside caption (front)}
        foo
    \end{table}
    \begin{table}
        \caption{label inside caption (back) \label{mylabel}}
        foo
    \end{table}
    \begin{table}
        \caption{label directly after caption}
        \label{mylabelfoo}
        foo
    \end{table}
    \begin{table}
      \caption{label at end of table (I'm OK with that one)}
      foo
      \label{mylabelfoobar}
    \end{table}

Now some referencing: \cref{othermylabel} and \ref{mylabel} and \ref{mylabelfoo} and \ref{mylabelfoobar}
\end{document}

在此处输入图片描述

答案2

这有效:

\documentclass{article}
%\usepackage{caption}
\usepackage{showlabels}
\usepackage{xpatch}
\def\storelabel#1{\gdef\storedlabel{#1}}
\makeatletter
\patchcmd{\@caption}{#3}{\global\let\storedlabel\undefined\let\label\storelabel#3}{}{err}
\apptocmd{\@caption}{\ifdefined\storedlabel\label{\storedlabel}\else\fi}{}{err}
\makeatother
\begin{document}
    \listoftables
    \clearpage
    \begin{table}
        \caption{no label}
        foo
    \end{table}
    \begin{table}
        \caption{\label{mylabel}label inside caption (front)}
        foo
    \end{table}
    \begin{table}
        \caption{label inside caption (back) \label{mylabel}}
        foo
    \end{table}
    \begin{table}
        \caption{label directly after caption}
        \label{mylabel}
        foo
    \end{table}
    \begin{table}
        \caption{label at end of table (I'm OK with that one)}
        foo
        \label{mylabel}
    \end{table}
\end{document}

主要区别在于

  • \def\label##1{...}-> ,避免\let\label\storelabel使用##1\caption
  • 受到@ChristianHupfer 的启发并感谢他,使用 来xpatch选择性修补\@caption而不是重新定义\caption。它甚至可以与caption包和\listoftablesnow 一起使用。

他的评论仍然有效:\label使用可选参数失败。但我不需要那个。

相关内容