
\ref
を として再定義したいのです\eqref
が、うまくいきません。が内部的に として定義されている\renewcommand{\ref}{\eqref}
ためにこのようなことが起きているのではないかと思いますが、よくわかりません。何かアドバイスはありますか?\eqref
\newcommand{\eqref}{(\ref)}
答え1
もし、あんたが本当にこれをやりたい場合、はamsmath.sty
次\eqref
のように定義されます
\renewcommand{\eqref}[1]{\textup{\tagform@{\ref{#1}}}}
だからこう言える
\documentclass{article}
\usepackage{amsmath}
\makeatletter
\let\origref\ref
\renewcommand{\ref}[1]{\textup{\tagform@{\origref{#1}}}}
\makeatother
\begin{document}
\section{Test}\label{a}
Section~\ref{a}? Equation~\ref{b}
\begin{equation}\label{b}
a=b
\end{equation}
\end{document}
再定義によって生じる不整合に注意してください。セクション を相互参照しています(1)
が、ドキュメントにはセクション があります1
。ドキュメント内のフロートやその他のオブジェクトでも同様の問題が発生します。
付録
おそらく、特定のタイプのオブジェクトの相互参照形式を再定義することに興味がある場合は、cleveref
パッケージはより良い選択肢かもしれません。相互参照図の書式を変更し、他のすべての相互参照は変更しない小さな例:
\documentclass{article}
\usepackage{amsmath}
\usepackage{cleveref}
\crefformat{figure}{figure~(#2#1#3)}
\Crefformat{figure}{Figure~(#2#1#3)}
\begin{document}
\section{Test}\label{a}
Section~\ref{a}. Equation~\ref{b}. \Cref{c}? or \cref{c}?
\begin{equation}\label{b}
a=b
\end{equation}
\begin{figure}
\centering
A
\caption{test figure}
\label{c}
\end{figure}
\end{document}
答え2
マクロは、\p@equation
の等式参照を変更するために使用できます\ref
。通常、\p@<counter>
はカウンタの参照にプレフィックスを追加するために使用され<counter>
、引数は取りません。ただし、 を追加すると\expandafter
、カウンタ値を単純な引数として取得できます。
\documentclass{article}
\usepackage{amsmath}
\makeatletter
\renewcommand*{\p@equation}{%
\expandafter\@p@equation
}
\newcommand*{\@p@equation}[1]{%
\textup{\tagform@{#1}}% see definition of `\eqref`
}
\makeatother
\begin{document}
\section{Hello}\label{sec:hello}
\begin{equation}
\label{eq:einstein}
E=mc^2
\end{equation}
References: Section \ref{sec:hello} and equation \ref{eq:einstein}.
\end{document}
欠点: 参照は\ref
内で使用しないでください\tag
。そうしないと、括弧が重複することになります。
答え3
\ref
パッケージを使用する形式を選択的に定義できますfncylab
。これは、Heiko の回答と同等ですが、よりパッケージ化されています。例:
\usepackage{fncylab}
\labelformat{equation}{(#1)}
\section{A section}\label{s:section}
\[ \label{eq:eqn} ... \]
\ref{s:section}\ref{eq:eqn}
括弧内に方程式番号を出力しますが、セクション番号は通常どおりに出力します。定義するための前文が常に存在します\labelformat{section}{Section~#1}
。