
질문
외부 문서에서 그림, 섹션 또는 방정식을 인용하면서 참조 스타일 설정 cleveref
방법에 관계없이 패키지를 통해 내 문서에서 사용하는 것과 동일한 서식을 사용하여 참조하려면 어떻게 해야 합니까?cleveref
예
내가 다른 문서에서 방정식(예를 들어 9번째 방정식)을 참조하고 싶다고 가정해 보겠습니다. 아래 텍스트에서 다음과 같은 것으로 해결되도록 cleveref
가 설정되었다고 가정합니다 . 이 경우 나는\Cref{eqn:myEqn}
Equation~(1)
~할 수 있었다Equation~(9)
아래와 같이 외부 방정식을 참조하는 데 사용하면 됩니다 .
\documentclass{report}
\usepackage{cleveref}
\begin{document}
My equation is:
\begin{equation} \label{eqn:myEqn}
f(x) = a x^2 + bx + c \text{.}
\end{equation}
My \Cref{eqn:myEqn} is equivalent to Equation~(9) of Smith et al. (2018).
\end{document}
문제는 cleveref
나중에 다르게 설정되어 , 또는 , 또는 (등) \Cref{eqn:myEqn}
로 해결 되면 하드 코딩한 모든 위치를 새 참조 스타일과 일치하도록 변경해야 한다는 것입니다.Eqn.~1
(1)
Equation~1
Equation~(9)
cleveref
이 작업을 수행할 수 있는 매크로가 있나요 ? 나는 그것이 어떤 종류의 참조(방정식, 그림, 섹션 등)인지, 그리고 그것을 참조하기 위해 어떤 숫자나 문자를 사용해야 하는지 말해야 한다고 생각합니다. 만약 있다면 아마도 다음과 같이 사용될 수 있을 것입니다:
\documentclass{report}
\usepackage{cleveref}
\begin{document}
My equation is:
\begin{equation} \label{eqn:myEqn}
f(x) = a x^2 + bx + c \text{.}
\end{equation}
My \Cref{eqn:myEqn} is equivalent to \CrefFormatExternal[eqn]{9} of Smith et al. (2018).
\end{document}
답변1
가능한 솔루션은 카운터(방정식)의 현재 값을 저장한 다음 카운터(방정식)를 1씩 감소된 두 번째 인수 값으로 설정하고 \refstepcounter
자동으로 생성된 레이블을 사용하는 것입니다. 정의되어 있는지 여부를 확인하여 참조합니다 r@X
. X
자동 생성된 라벨 이름입니다. 물론 이 접근 방식의 약점은 매크로 \CrefFormatExternal
매크로가 동일한 값에 대해 호출될 때마다 그러한 레이블을 생성한다는 것입니다.
별표 표시된 버전은 하이퍼링크를 억제하며 이 버전 \crefFormatExternal
도 사용할 수 있습니다.
\documentclass{report}
\usepackage{mathtools}
\usepackage{xparse}
\usepackage{hyperref}
\usepackage{cleveref}
\makeatletter
\newif\if@hyperrefpackageloaded
\AtBeginDocument{%
\@ifpackageloaded{hyperref}{%
\@hyperrefpackageloadedtrue
}{}
}
\newcounter{tmpcntr}
\newcounter{helpcntr}
\NewDocumentCommand{\CrefFormatExternal}{sO{equation}m}{%
\setcounter{tmpcntr}{\value{#2}}%
\stepcounter{helpcntr}%
\setcounter{#2}{\numexpr#3-1}%
\refstepcounter{#2}\label{cleverrefexternalref:\number\value{helpcntr}}%
\@ifundefined{r@cleverrefexternalref:\number\value{helpcntr}}{%
}{%
\IfBooleanTF{#1}{%
\Cref*{cleverrefexternalref:\number\value{helpcntr}}%
}{%
\Cref{cleverrefexternalref:\number\value{helpcntr}}%
}%
}%
\setcounter{#2}{\value{tmpcntr}}%
}
\NewDocumentCommand{\crefFormatExternal}{sO{equation}m}{%
\setcounter{tmpcntr}{\value{#2}}%
\stepcounter{helpcntr}%
\setcounter{#2}{\numexpr#3-1}%
\refstepcounter{#2}\label{cleverrefexternalref:\number\value{helpcntr}}%
\@ifundefined{r@cleverrefexternalref:\number\value{helpcntr}}{%
}{%
\IfBooleanTF{#1}{%
\cref*{cleverrefexternalref:\number\value{helpcntr}}%
}{%
\cref{cleverrefexternalref:\number\value{helpcntr}}%
}%
}%
\setcounter{#2}{\value{tmpcntr}}%
}
\makeatother
\begin{document}
My equation is:
\begin{equation} \label{eqn:myEqn}
f(x) = a x^{2} + bx + c \text{.}
\end{equation}
My \Cref{eqn:myEqn} is equivalent to \CrefFormatExternal*{9} of Smith et al. (2018).
\begin{equation} \label{eqn:otherEqn}
x_{1/2} = \dfrac{ - b\pm \sqrt{ b^{2} - 4 ac}}{2a}
\end{equation}
\end{document}
답변2
관련 카운터 없이 참조 형식을 지정하기 위해 Cleveref의 내부 명령을 사용하는 것이 가능합니다. 카운터 이름(예 : , 또는 ) cref@<type>@format
이 있는 형식의 명령은 우리가 원하는 대로 수행되는 것 같습니다.<type>
equation
section
figure
예를 들어, cref@figure@format{foo}{}{}
"fig.foo" 또는 \crefformat
그림에 대해 사용자 정의에 의해 생성되는 모든 항목을 생성합니다. 메모:
- 두 개의 빈 인수는 일반적으로 참조의 하이퍼링크 부분의 시작과 끝을 표시하는 데 사용되지만( 문서 참조
\crefformat
) 이 경우에는 하이퍼링크가 필요하지 않다고 가정합니다. - 이 내부 매크로는
@
이름에 포함되어 있으므로 다른 문서에서는 사용할 수 없습니다.~ 후에\makeatletter
.
다음 예에서는 \crefcustom
내부 명령을 호출하는 도우미 매크로를 정의합니다. \Crefcustom
대문자 버전입니다.
\documentclass{report}
\usepackage{cleveref}
\makeatletter
\newcommand {\crefcustom}[2]{\csname cref@#1@format\endcsname{#2}{}{}}
\newcommand {\Crefcustom}[2]{\csname Cref@#1@format\endcsname{#2}{}{}}
\makeatother
\begin{document}
My equation is:
\begin{equation} \label{eqn:myEqn}
f(x) = a x^2 + bx + c.
\end{equation}
My \cref{eqn:myEqn} is equivalent to \crefcustom{equation}{9} of Smith et al. (2018).
\end{document}