Skip to content

PDFハイパーリンク

Daiki Matsunaga edited this page Aug 14, 2020 · 2 revisions

この記事では,PDFハイパーリンク機能の使い方について解説します.

文書作成者向け

クラスファイルとしてstdjastdjareportstdjabookを使っている場合には文書を変更せずともハイパーリンク機能の恩恵を受けられます.具体的には以下の箇所でハイパーリンクが有効となります.

  • 目次
  • \ref\ref-pageの番号

ということで,過去に書いたSATySFi文書があれば(SATySFiをアップデートしてから)再コンパイルしてみましょう!

クラスファイルとは独立にannotパッケージでもコマンドが提供されており,現在は\hrefコマンドのみ定義されています.

平成最後の\SATySFi;アドベントカレンダー \href(`https://adventar.org/calendars/3418`){開催中}

と書くと,

平成最後のSATySFiアドベントカレンダー開催中

となります.

コマンド・クラスファイル作成者向け

普通に文書を作成するだけならこれだけなのですが,自分でコマンドを定義したり,自作クラスファイルをハイパーリンク対応する際には専用のプリミティブを呼ぶ必要があります.

  • register-link-to-uri : string -> point -> length -> length-> length -> (length * color) option -> unit : URIと座標,幅,高さ,深さ,枠線を指定し,URIへのハイパーリンクを作成します.枠線の指定はオプションで,線の幅と色を指定できます.この枠線はPDFビューアでの閲覧時にのみ表示されるもので,印刷時には消えます.
  • register-link-to-location : string -> point -> length -> length-> length -> (length * color) option -> unit : キーと座標,幅,高さ,深さ,枠線を指定し,キーに対応するジャンプ先へのハイパーリンクを作成します.
  • register-destination : string -> point -> unit : キーと座標を指定し,ジャンプ先を登録します.

register-link-to-locationは文書内の移動に使用するもので,register-destinationで登録したキーに対応するジャンプ先へのハイパーリンクを作成します.

実際にはこれらのプリミティブを直接呼び出すことはせず,annotパッケージ内のlink-to-uri-framelink-to-location-frameregister-location-frameを使用します.

  • link-to-uri-frame : string -> (length * color) option -> deco-set : URIと枠線を指定します.

  • link-to-location-frame : string -> (length * color) option -> deco-set : キーと枠線を指定します.

  • register-location-frame : string -> deco-set : キーを指定し,囲み枠の左上の座標をジャンプ先として登録します.

それぞれ,いくつか引数を与えてやると,囲み枠(The SATySFibook 9.3節参照)に指定するdeco-setになります.これらを使用すると,囲み枠を指定するのと同じ方法でハイパーリンクの設定とジャンプ先の登録を行えます.

例1. Twitterアカウント名を挿入するインラインコマンド\twitter

let-inline ctx \twitter ?:borderopt user =
  let user-uri = `https://twitter.com/` ^ user in
  let it = read-inline ctx (embed-string (`@` ^ user)) in
    inline-frame-breakable (0pt, 0pt, 0pt, 0pt) (Annot.link-to-uri-frame user-uri borderopt) it

例2. TeXのhyperrefパッケージ風インラインコマンド\hypertarget\hyperlink

let-inline ctx \hypertarget name inner =                                    
  inline-frame-breakable (0pt, 0pt, 0pt, 0pt) (Annot.register-location-frame name) (read-inline ctx inner)                                               
  
let-inline ctx \hyperlink ?:borderopt name inner =                          
  inline-frame-breakable (0pt, 0pt, 0pt, 0pt) (Annot.link-to-location-frame name borderopt) (read-inline ctx inner)
Clone this wiki locally