Skip to content

Instantly share code, notes, and snippets.

@yarakos95
Last active January 4, 2022 10:40
Show Gist options
  • Save yarakos95/347cd77e63a605603708adfc4747b1b4 to your computer and use it in GitHub Desktop.
Save yarakos95/347cd77e63a605603708adfc4747b1b4 to your computer and use it in GitHub Desktop.
zref-clever を使ってみた

zref-clever を使ってみた

相互参照で\ref するときには次のようにする.

contentType~\ref{labelName}

このとき,contentType をいちいち打つのは面倒であり,これを変更する必要がある場合にはさらに面倒なことになる.これは自動化しておきたい. また,数式に関しては相互参照した数式のみに式番号を付与すべきである.

この2つを同時に実現するには次のようなcleveref とautonum を組み合わせた方法が良く知られている.

cleveref とautonum パッケージ

世の中にはすでにcleveref+autonum というような上手な相互参照の方法が普及している.

しかしながら,autonum は少々厄介なように思う. と言うのは,次のような制約があると思うからである.

  • autonum がサポートしている数式環境しか式番号を操作できない
  • 参照しないが式番号を付与したい数式はequation+ 環境のみ

ここで,autonum に似たmathtools から提供されているshowonlyrefs オプションを確認しておきたい.

ただし,このmathtools によるshowonlyrefs はcleveref と並列に利用できないことに注意しておきたい.

mathtools のshowonlyrefs オプション

参照した数式のみに式番号を付与したい場合にはmathtools のshowonlyrefs オプションを使用することもある.

\mathtoolsset{showonlyrefs}

\eqref\refeq で参照された数式に式番号を付与することが出来る. これに加えて,\noeqref を使うことで参照しない数式にも式番号を付与することも出来る.

showonlyrefs ではautonum での制約がないものとなっていると思われる.

zref-clever パッケージ

v0.1.1-alpha, released 2021-12-07

Caution: まだリリースされたばかりであり,使用が変更される場合があることに注意が必要になる.

zref を利用したclever な相互参照を実現する.

次のようにプリアンブルで読み込めば良い. 日本語はデフォルトで対応していないので,japanese とエイリアスのja を定義しておいた.

本来はbabel やpolyglossia を利用することで自動的に対応されるようだが,ちょっとヨクワカラナイのでスルーしておく.(手動でも対応できるので尚更)

\usepackage[nameinlink,preposinlink,lang=ja]{zref-clever}
\zcDeclareLanguage[allcaps]{japanese}
\zcDeclareLanguageAlias{ja}{japanese}
\usepackage{hyperref}

今回は,オプションでnameinlink を与えている.これはcleveref と同じ仕様のものであり,ハイパーリンクを定理 などの参照のタイプ名を含めて埋め込むようにしている. また,preposinlink としておくことで,相互参照番号の前後の文字(丸括弧など) にもハイパーリンクを埋め込むようにしている.

\zcDeclareLanguage で日本語を宣言しており,allcaps オプションによって大文字小文字の違いがない(大文字のみ) の設定をしている.

zref-clever では\zcLanguageSetup から各タイプごとにEq. などを日本語に変更する. しかしながら,やたら長くなるためにプリアンブルが煩雑になってしまう.ちょっと面倒くさい. sty ファイルのあるディレクトリを漁ってみると,サポートされている言語ごとに.dict ファイルが作成されているようだ.また,試してみるとカレントディレクトリでも読んでくれるみたいだ.

したがって,次のようなファイルをカレントディレクトリにおいた. zref-clever-english.dict を改造したものなので,上手い翻訳が分からないものに関してはそのままとなっている.

この下にあるファイル: zref-clever-japanese.dict

Suffix Meaning
-sg 単数形
-pl 複数形
-ab 省略形

zref を利用しているため,通常のラベルには\zlabel を使う必要がある.また,これを参照するには\zcref を利用する. しかし,数式環境では少し勝手が異なり,\label を利用する.ここでも同じように参照は\zcref で行う.

showonlyrefs と並列する

zref-clever ではmathtools のshowonlyrefs と並列して利用することが出来る.

\usepackage{mathtools}
\mathtoolsset{showonlyrefs}

参照方法も\zcref のままで大丈夫である.

cleveref パッケージとの関連性

このパッケージの謝辞を見ると,cleveref を強く意識していることが分かる.

The name of the package makes no secret that a major inspiration for the kind of “feel” I strove to achieve has been Toby Cubitt’s cleveref. Indeed, I have been an user of cleveref for several years, and a happy one at that. But the role cleveref played in the development of zref-clever extends beyond the visible influence in the design of user facing functionality. Some technical solutions and, specially, the handling of support for other packages were a valuable reference. Hence, the accumulated experience of cleveref allowed for zref-clever to start on a more solid foundation than would otherwise be the case.

Quoted: The zref-clever package User manual (v0.1.1-alpha, released 2021-12-07) §14 Acknowledgments

Digression

もっと上手に使える人がいれば,いい感じに記事にしてください:pray:

mathtools のshowonlyrefs を使いたいけどcleveref が使えない人は是非に.

%%
%% This is file `zref-clever-japanese.dict',
namesep = {\nobreakspace} ,
pairsep = {~と\nobreakspace} ,
listsep = {,~} ,
lastsep = {~と\nobreakspace} ,
tpairsep = {~と\nobreakspace} ,
tlistsep = {,~} ,
tlastsep = {,~と\nobreakspace} ,
notesep = {~} ,
rangesep = {~から\nobreakspace} ,
type = book ,
Name-sg = Book ,
Name-pl = Books ,
type = part ,
Name-sg = Part ,
Name-pl = Parts ,
type = chapter ,
Name-sg = Chapter ,
Name-pl = Chapters ,
type = section ,
Name-sg = Section ,
Name-pl = Sections ,
type = paragraph ,
Name-sg = Paragraph ,
Name-pl = Paragraphs ,
type = appendix ,
Name-sg = 付録 ,
Name-pl = 付録 ,
type = page ,
Name-sg = 頁 ,
Name-pl = 頁 ,
name-sg-ab = p. ,
name-pl-ab = pp. ,
rangesep = {\textendash} ,
type = line ,
Name-sg = 行 ,
Name-pl = 行 ,
type = figure ,
Name-sg = 図 ,
Name-pl = 図 ,
type = table ,
Name-sg = 表 ,
Name-pl = 表 ,
type = item ,
Name-sg = Item ,
Name-pl = Items ,
type = footnote ,
Name-sg = 脚注 ,
Name-pl = 脚注 ,
type = endnote ,
Name-sg = 注釈 ,
Name-pl = 注釈 ,
type = note ,
Name-sg = ノート ,
Name-pl = ノート ,
type = equation ,
Name-sg = 式 ,
Name-pl = 式 ,
refpre = {(} ,
refpos = {)} ,
type = theorem ,
Name-sg = 定理 ,
Name-pl = 定理 ,
type = lemma ,
Name-sg = 補題 ,
Name-pl = 補題 ,
type = corollary ,
Name-sg = Corollary ,
Name-pl = Corollaries ,
type = proposition ,
Name-sg = 命題 ,
Name-pl = 命題 ,
type = definition ,
Name-sg = 定義 ,
Name-pl = 定義 ,
type = proof ,
Name-sg = 証明 ,
Name-pl = 証明 ,
type = result ,
Name-sg = 結果 ,
Name-pl = 結果 ,
type = remark ,
Name-sg = Remark ,
Name-pl = Remarks ,
type = example ,
Name-sg = 例 ,
Name-pl = 例 ,
type = algorithm ,
Name-sg = Algorithm ,
Name-pl = Algorithms ,
type = listing ,
Name-sg = コード ,
Name-pl = コード ,
type = exercise ,
Name-sg = 演習 ,
Name-pl = 演習 ,
type = solution ,
Name-sg = 解法 ,
Name-pl = 解法 ,
%%
%%
%% End of file `zref-clever-japanese.dict'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment