
\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 파일도 정확해 보이지만 다음과 같은 오류 메시지가 나타납니다(5번, 호출당 한 번 \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
내 관점에서는 를 잡아서 레이블 이름을 in \label
에 저장한 다음 , 발행된 후에 실제 값을 의 끝으로 이동하는 것이 더 좋습니다.\storedlabel
\caption
\label
\@caption
\@makecaption
\label
윌 cleveref
의 스타일을 재정의하다 \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
\listoftables
그의 의견은 여전히 유효합니다. \label
선택적 인수를 사용하면 실패합니다. 그러나 나는 그것이 필요하지 않습니다.