3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

autonum パッケージで相互参照する

Posted at

autonum パッケージで相互参照する

数式の式番号を付与するにはどれに付与するべきかという議題は議論の余地がある.
これには3つの相反するルールがあるようだ.

  1. Fisher's rule: すべての数式に式番号を付与する
  2. Occam's rule: 参照された数式のみに式番号を付与する
  3. Fisher-Occam rule: 参照される可能性のある数式に付与する(本当に不要なものには式番号を付与しない)

ここでは"2. Occam's rule" を採用していきたい.

数式環境に式番号を付与しないようにするには,* 付き環境を使うか\notag を手で決める必要がある.これを考えながら書くのはあまり生産的なことではない.

autonum パッケージを使用することでこれを実現することが出来る.

■ パッケージ読み込み

cf. The autonum Package, 3.1 Load Order

autonum パッケージが他のパッケージと干渉する場合があるので,順序には非常に気を遣う必要がある.(順序が正しくない場合,期待しない結果を得るかエラーを吐く)
また,自動的に読み込まれるパッケージはあからさまに読み込むようにしておこう.

例 :
amsmath, cleveref, autonum, hyperref を使用したい場合には,順序は必ず以下のようにする.

amsmath →hyperref →cleveref →autonum

より実践的に日本語文書を作成する場合には以下のようにすると良いだろう.

\documentclass[uplatex,dvipdfmx,12pt,a4paper]{jsarticle}
\usepackage{amsmath}
\usepackage{mathtools}    % あからさまに読み込む
\usepackage[hypertexnames=false]{hyperref}    % 必ずオプションを追加しておく
\usepackage{pxjahyper}
\usepackage{cleveref}
\usepackage{autonum}

正しい参照を取得するために,autonum パッケージを使用する際には複数回のコンパイルが必要になる.(目次を作成するような場合と同様)

また,1ページに多くの数式を含むような場合には以下のような対応をすべきのようだ.

  • autonum の使用を避ける
  • ほとんどの数式を参照する
  • 行番号をアクティブにする

どれくらいの数式が多いのかは分からないが,調子が悪い場合は上のようにすると良いだろう.

※ hyperref での問題点

clveref などとは無関係に以下のような問題が生じるようだ.これらの問題はすべてhyperref のオプション設定によって解決する.

より詳細に紹介しておいたほうが良いかと思われたが,筆者自身がよく分からなかったので,以下に原文のまま紹介しておきます.

cf. The autonum Package, 3.2 Hyperref

Problem description: Generally, the autonum package is careful not to break other packages, but with autonum there are non-starred mathematical environment which do not increase the equation counter if they are not referenced.
This leads to the following warning in hyperref when more than one equation is used:

"destination with the same identifier (nameequation.0.1) has been already used, duplicate ignored".

Additionally, the link anchors do not work correctly (with a reference followed by a labeled equation, there is a warning in tracing mode, too. Hyperref seems to increase $\rm\LaTeX$’s equation’s counter (called equation) only if a equation is referenced. This counter may not be modified to avoid problems with Hyperref, as this would change the displayed equation number (and it does not work anyway).
This problem is independent of cleveref. The problem might be solvable by modifying \begin{equation} or \end{equation}. Using gather instead of equation removes the warning, but hyperlinks still do not work.

So it’s a mess and probably not worth the time, as hypertexnames=false solves all these problems.

ともかく,hyperref オプションhypertexnames=false を有効にしておけば,この問題は解決されるようだ.

○ ラベルの付け方

cf. The autonum Package, 2 Usage and Examples

対応している数式環境を以下に列挙しておく.

  • equation, align, alignat, gather, flalign, multline 環境
  • equation+ 環境
  • \[~\] (※ amsmath から変更有)
  • split 環境
  • -ed 環境, cases 環境などの非独立な環境

パッケージガイドに書かれていない場合も含めてautonum パッケージを利用できない状況を挙げておきたい.

  • * 付き環境
  • empheq パッケージでoverload オプションを利用している場合1

■ equation, align, alignat, gather, flalign, multline 環境

\label の推奨される挿入方法

  • 各数式に\label を追加し,必要に応じて\ref を使用する
  • 数式と改行\\ の間に挿入する
\begin{<amsmath での数式環境>}
    <equation_1>  \label{eq: 1} \\
    <equation_2>  \label{eq: 2} \\
    <equation_3>  \label{eq: 3}
\end{<amsmath での数式環境>}

equation 環境では以下のようにして\label を置くことが出来るが,十分にはテストされていないようだ.

\begin{equation}  \label{eq: 1}
    <Equation>
\end{equation}

何度も書くようだが,* 付きの数式環境は使用できない.

■ equation+ 環境

相互参照が無くても式番号が常に表示される環境である

\begin{equation+}
    <Equation>  \label{eq :1}    % `\label` や参照なしでも式番号が付与される
\end{equation+}

autonum では相互参照のない数式には式番号が付与されないので,参照しないが式番号を付与したい場合には有用だろう.これは初めに示した"3. Fisher-Occam rule" にしたがうことができる.

ただし,equation 環境以外の+ 付き環境はないので注意が必要である.2

\[ ~\]

amsmath では,\[ ~\] はequation* 環境に定義されている.
しかし,autonum では* 付き環境は使用できないようになっているので,これをequation 環境と再定義している.

パッケージ 環境 式番号
amsmath equation* なし
autonum equation 相互参照のあるときのみ

autonum パッケージにおける\[ ~\] はequation 環境である

したがって,以下のようにすることももちろん可能である.

\[    % 式番号が付与される
    <Equation>  \label{eq: 1}
\]
\ref{eq: 1}    % \cref でも可

■ split 環境

split 環境を利用する場合には,\label の付ける位置を非常に気を付ける必要があるようだ.

\begin{align}
<Left Hand Side>
    = {} & <Right Hand Side 1>  \label{eq: RHS1}  \\
%
%
    \label{eq: RHS2 split}    % ここに置くことが推奨されている
    \begin{split}    % ここに置いても認識される
    = {} & <Right Hand Side 2-1>  \\    % \\ の前に置いても認識される
         & <Right Hand Side 2-2>    % ここに置いても認識される
    \end{split}    % ここに\label を付けるとエラーとなる
%
%
    = {} & <Right Hand Side 3>  \label{eq: RHS3}
\end{align}

現状としては\end{split} の後ではなければ\label が機能する.
しかし,以下の文言にあるようにsplit 環境内で\label を使用することが将来も使用できるかどうかは分からない(仕様変更に伴って使えなくなる可能性がある)ようだ.

Providing it inside of the split environment seems to work currently, but is not guaranteed to work in the future.

以下に実例コードを示しておきたい.

\begin{align}
\zeta(2)
    = {} & \sum_{n=1}^\infty \frac{1}{n^2}  \label{eq: zeta2}  \\
    \label{eq: zeta2expand}
    \begin{split}
    = {} & 1 + \frac{1}{4} + \frac{1}{9} + \frac{1}{16} + \frac{1}{25} + \frac{1}{36} + \frac{1}{49}  \\
         & + \frac{1}{64} + \frac{1}{81} + \frac{1}{100} + \frac{1}{121} + \cdots
    \end{split}  \\
    = {} & \frac{239437889}{153679680}
    \simeq 1.56  \label{eq: zeta2sum}
\end{align}

筆者としては,\begin{split} 前に\label を付けるのは非常に奇妙に感じる.
いっそのこと,新たなsplit 環境を定義してしまえば良いような気もする.

\newenvironment{splitwl}[1]{% split env. with label
    \label{#1}
    \split
}{
    \endsplit
}

このように定義しておけば,上の実例に関しても以下のようにすることが出来る.

\begin{align}
\zeta(2)
    = {} & \sum_{n=1}^\infty \frac{1}{n^2}  \label{eq: zeta2}  \\
    \begin{splitwl}{eq: zeta2expand}
    = {} & 1 + \frac{1}{4} + \frac{1}{9} + \frac{1}{16} + \frac{1}{25} + \frac{1}{36} + \frac{1}{49}  \\
         & + \frac{1}{64} + \frac{1}{81} + \frac{1}{100} + \frac{1}{121} + \cdots
    \end{splitwl}  \\
    = {} & \frac{239437889}{153679680}
    \simeq 1.56  \label{eq: zeta2sum}
\end{align}

○ 相互参照

参照には\ref を使用することが出来る.また,cleveref によって提供されている\cref も使用することが出来る.
また,amsmath から提供されている\eqref\ref を再定義したものなので,これも利用することが出来る.3

cleveref の参照コマンドに関しては,\cref をサポートしているが逆にそれ以外はサポートしていないようだ.
\crefrange に関して以下のように書かれている.

Range commands like the \crefrange command are not supported at the moment.
It’s not completely clear, if this is a good or a bad thing.
Please contact the package author if you are interested in this feature.

cleveref で提供されている他の相互参照コマンドに関しては,以下のように取り扱うと使用することが出来る.

\Cref を使いたい場合にはプリアンブルで以下のようにする.

\makeatletter
\autonum@generatePatchedReferenceCSL{Cref}
\makeatother

○ mathtools に関して

mathtools にはshowonlyrefs オプションがある.これも数式の式番号を参照している数式のみに与えるオプションになっている.

これに関してはmathtools を読み込み,オプションを追加すれば良い.

\usepackage{mathtools}
\mathtoolsset{showonlyrefs = true}

として,\eqref で参照された数式に式番号を付与することが出来る.

このオプションに関してautonum パッケージガイドのIntroduction には以下のように述べられている.

The mathtools package's options shownlyrefs seems to be the solution for those who want to have the referenced equations numbered only.
Unfortunately, for amsmath users this also means a step backwards, as the numbering can overwrite parts of the equaotn according to its documentation.
Generally, this options seems to be qeuite unreliable as it is incompatible with the cleveref package.

まず,このオプションではcleveref との併用が出来ない.これは多くの記事で紹介されていることだろう.
cleveref は\ref とは独自に参照コマンドを定義しているので,\eqref とは相容れない.したがって,双方を同時に利用するとカウンタが別として動作するために,同じ式番号が出てきたりする.

2文目の"Unfortunately,--" に続く文に関して,mathtools の何がamsmath の目的に後退しているのかがあまりよく分からない.分かる方がいれば教えてほしい.

参考

余談

cleveref を利用することを目的にautonum を使うだろう.
しかし,autonum にはいくつかの問題をはらんでいるように思われる.

例えば,subequations 環境を使用する場合にはautonum はあまり有用に使用することが出来ないと思われる.
これは+ 付き環境がequation 環境にしかないということが問題になっている.

また,パッケージガイドには9つほど"Further idea" が提示されているが,パッケージの更新は2015年1月になされたきりされていないようだ.(どうなっている?)

cleveref は\ref ではtheorem \ref とする必要があるところを\cref だけでどのような環境で使用されているのかを自動的に判定させることで"theorem" 等を付与することのできるパッケージである.この他にも,連続する参照をコンパクトにまとめるなどいくつかの機能が備わっている.
このパッケージの使い方は以下に詳しい.

autonum パッケージを利用するとこれまでの数式環境に対していくつかの変更を必要とする上に,cleveref とのサポートも中途半端な感がある.
数式にはmathtools のshowonlyrefs を利用して,その他の定理環境などにはcleveref を利用するのがclever な気もする.あるいは,autonum に代わるcleveref の\cref もサポートする別のパッケージを利用するか……

amsmath が提供する\eqref にcleveref に似た機能を追加してくれるのがもっとも楽に思える.(他力本願)

  1. overload オプションを有効にしなければ使うことが出来る.

  2. 相互参照しない数式を個別に式番号を与えることが出来ないのは不便な場合もある気がする.mathtools では\noeqref を付けることで式番号を与えることが出来る.

  3. mathtools におけるshowonlyrefs を利用している状態からそのままautonum に移行することが可能である.

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?