\caption이나 \captionof 옆에 없어도 작동하도록 \label과 \ref 명령을 재정의하세요.

\caption이나 \captionof 옆에 없어도 작동하도록 \label과 \ref 명령을 재정의하세요.

나는 다음과 같이 or 이후에 \labeland 를 사용하려고 했습니다 .\ref\caption\captionof

Figure \Roman{chapter}.\arabic{figure}: The caption % A manually created caption
\label{fig:the_ref_name} % the ref that sould ref the \Roman{chapter}.\arabic{figure} number

\caption또는 뒤에 오지 않고도 이 명령 쌍을 사용할 수 있는 방법이 있습니까? \captionof하지만 방정식, 그림, 표 및 평소에 사용했던 모든 항목을 계속 참조하는 기능을 잃지 않도록 할 수 있습니까?

답변1

\label명령은 내부 매크로를 기반으로 레이블 값을 저장합니다 \@currentlabel. 이 매크로는 와 같은 명령 \section및 환경 에 의해 설정됩니다 \begin{figure}. 그러나 직접 설정할 수도 있으며, 그러면 다음 \label명령이 지정한 값을 사용합니다.

\@currentlabel은 기호가 포함된 내부 매크로이므로 해당 매크로를 사용하는 코드가 @필요합니다 \makeatletter. \makeatother다음 MWE는 (확장된 정의)를 사용하여 매크로에 값을 할당하여 \edef카운터가 실제 값으로 표시되는지 확인합니다.

\documentclass{report}
\begin{document}
\chapter{My chapter}
\setcounter{figure}{8} % set the figure counter to some number for illustration purposes
Figure \Roman{chapter}.\arabic{figure}: The caption % A manually created caption
\makeatletter\edef\@currentlabel{\Roman{chapter}.\arabic{figure}}\makeatother% set value for use by \label
\label{fig:the_ref_name} % assign the label
\section{New section}
As we have seen in Figure \ref{fig:the_ref_name}, figures can have captions.
\end{document}

결과(레이블을 보조 파일에 쓰고 다시 읽는 두 번의 컴파일 이후):

여기에 이미지 설명을 입력하세요

위의 코드는 내부 레이블 값을 설정한 후 이를 와 같은 레이블에 할당하는 원리를 설명하기 위한 것입니다 fig:the_ref_name. 그러나 이것은 물론 그다지 실용적이지 않습니다. 작은 매크로를 작성하여 캡션을 생성하고 동시에 레이블 값을 설정할 수 있습니다.

\documentclass{report}
\makeatletter
\newcommand{\customfigcaption}[1]{%
Figure \Roman{chapter}.\arabic{figure}: #1%
\edef\@currentlabel{\Roman{chapter}.\arabic{figure}}%
}
\makeatother

\begin{document}
\chapter{My chapter}
\setcounter{figure}{8}
\customfigcaption{The caption}
\label{fig:the_ref_name} % assign the label
\section{New section}
As we have seen in Figure \ref{fig:the_ref_name}, figures can have captions.
\end{document}

출력은 위와 동일합니다.

마지막 추가 사항은 다음에서 레이블을 참조할 수 있는 팬텀 섹션을 추가하는 것입니다 hyperref.

\documentclass{report}
\usepackage{hyperref}
\makeatletter
\newcommand{\customfigcaption}[1]{%
Figure \Roman{chapter}.\arabic{figure}: #1%
\csname phantomsection\endcsname%
\edef\@currentlabel{\Roman{chapter}.\arabic{figure}}%
}
\makeatother

\begin{document}
\chapter{My chapter}
\setcounter{figure}{5}
\customfigcaption{The caption}
\label{fig:the_ref_name} % assign the label
\section{New section}
As we have seen in Figure \ref{fig:the_ref_name}, figures can have captions.
\end{document}

여기에 이미지 설명을 입력하세요

관련 정보