\ref{}の周りに()を追加する

\ref{}の周りに()を追加する

次のようにするには、どのように[すればいいでしょうか:]\ref{myref}

[1.1] 

これは動作しません:

\documentclass{article}
\usepackage[colorlinks=true,allcolors=blue]{hyperref}
\let\oldref\ref
\renewcommand{\ref}[1]{[\oldref{#1}]}
\begin{document}
\section{Section}
Why cannot include square brackets around cross references? \ref{S}
\subsection{Subsection\label{S}}
This is a subsection.
\end{document}

回答: をコメントアウトする\usepackage[colorlinks=true,allcolors=blue]{hyperref}と動作します。しかし、 が必要ですhyperrefが、 は使いたくありません。amsmathまた、plastex の使用など、後で他のものと衝突する傾向があるため、パッケージを追加したくないのです。どうすればよいでしょうか?

答え1

hyperrefは の後のものを で再定義するので、 を次のように\begin{document}延期することができます。renewcommand\AfterBeginDocument

ここに画像の説明を入力してください

ノート:

  • これは、星付きのバリアントでは機能しません\ref*

コード:

\documentclass{article}
\usepackage{letltxmacro}
\usepackage[colorlinks=true,allcolors=blue]{hyperref}

\AtBeginDocument{%
    \LetLtxMacro\oldref{\ref}%
    \DeclareRobustCommand{\ref}[2][]{[\oldref#1{#2}]}%
}

\begin{document}
\section{Section}
Why cannot include square brackets around cross references? \ref{S}
\subsection{Subsection\label{S}}
This is a subsection.
\end{document}

答え2

私のコメントは回答のようですので、回答にアップグレードします。

を使用する代わりに\eqref、前文に以下を記述します。

\usepackage{fncylab}
\labelformat{equation}{(#1)}

\ref方程式のラベルに適用された は、通常は や のようにスタイル設定され、\eqrefhyperref使用されると、リンクは括弧を囲みます。

このテクニックは他の種類のラベルにも使用できますが、 にはバグがあるamsthmため、 の\newtheorem形式で を使用すると\newtheorem{<new theorem>}[<old theorem>]{...}<new theorem>には文字通りと同じカウンタ<old theorem>、およびfncylab(そして実際、cleverref)は、それらを同じようにラベル付けします。回避策は、定理を定義した後、それらのカウンタも複製する必要があることです。

\usepackage{aliascnt}
\newaliascnt{<new theorem>}{<old theorem>}
\labelformat{<new theorem>}{...}

これは期待どおりに動作します。私は、自分が書くものすべてにおいて、多数のカウンターでこれを実行します。

関連情報