質問

質問

質問

外部ドキュメントから図、セクション、または方程式を引用し、参照スタイルの設定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~1Equation~(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

私の考えられる解決策は、カウンター (式) の現在の値を保存し、カウンター (式) を 2 番目の引数値から 1 を減算した値に設定し、 と自動生成されたラベルを使用します。これは、 が定義されているかどうか\refstepcounterをチェックすることで参照されます。 は自動生成されたラベル名です。もちろん、このアプローチの弱点は、マクロ マクロが同じ値に対して呼び出されるたびに、そのようなラベルが生成されることです。r@XX\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>equationsectionfigure

たとえば、cref@figure@format{foo}{}{}「fig. foo」や、ユーザー定義の\crefformat図によって生成されるものを生成します。注:

  • 2 つの空の引数は通常、参照のハイパーリンク部分の開始と終了をマークするために使用されます (のドキュメントを参照\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}

関連情報