0に立ち返ってのSweave,Tex,epub

0に立ち返ってのTex環境

  • Texを使うとは、「ある形式」で書いたテキストファイルを「解釈」して「絵(文字を含む)」にすること
  • このための道具が大きく分けて4段構えになっている
    • (1)日本語フォントなどを使わない簡単な文書のための道具
    • (2)日本語フォントを使う簡単な文書を書くための追加の道具
    • (4)コマンドラインから解放されるための道具
    • (3)素敵な構成にするための追加の道具
  • 必要なもののダウンロード
    • (1)と(2)までが、「日本語ユーザにとっての必須道具」として、以下のようなサイトから取れる。とても便利にできている
      • 2つの方法
    • (1) 「あべのり さん」を使う
        • proxyの設定などで「あべのり さん」版が使えないときは、次のを使う
      • (2) 「LabTex Installer」を使う
  • コマンドラインからの解放(ステップ(3))
    • Texエディタ・Tex文書処理アプリケーションというのは、hoge.texというファイルを読み込んで、ちゃんとしたファイルかどうかの判断をして、ちゃんとしていたら、『絵〜dvi,eps,pdf』にして、それを見せてくれるアプリ
    • 上記の諸段階の処理のすべてをやってくれるものもあれば、一部だけをやるものもある
    • この段階のトラブルは、文字コード問題・文字化け問題
    • 文字コード問題に対処する仕組み
      • 問題になりうるから、それぞれの段階で、それを制御する仕組みが存在し得る
      • Sweaveに読ませるRnw文書のレベルで制御したいなら、こちらの3.7 Encoding に書いてあるような対処法(『この文書のエンコーディングは○○』と宣言して、後は、それに従わせる)もあるようだが、これを使って、Sweave->texの日本語エンコーディング問題を具体的に説明した文書・サイトがすぐには見つからないので、これは保留
      • Sweave()関数の引数として指定する。これはうまく行くらしい
Sweave("hoge.Rnw",encoding="SJIS")
      • Texエディタで開く。Texエディタが自動判定してくれれば問題ないが、自動判定してくれない場合、判定に失敗する場合は、エンコーディングを変更して読み込み直しをすればよい。TexWorksというエディタの場合の例がこちら
      • texファイルからpdf(等)ファイルへの処理は、texエディタがきちんと扱っていれば、うまく行く(texエディタの表示は文字化けしていても、texファイル→pdfはうまくいうこともある(それはtexエディタが呼び出しているpdf(等)化のコマンドがうまくハンドリングしているから。Sweave()関数できちんと指定してあり、(1)(2)の環境整備がうまく行っていれば、texエディタ上で化けていても大丈夫なようだ…とは言え、気持ち悪いので、texエディタ上もきちんと見えるようにしておく方が良い)
  • スタイルを整える(4)
    • (1),(2)で入手した環境と、それを気楽に使うための(3)が整ったとする
    • この後は「カスタマイズ」になる
    • 「カスタマイズ」をするときの注意点は、「こうするとよい」という情報が得られたときに、「やれ」と言われていないことをはからずもやってしまわないこと
      • 「はからずも」を避けるための最大のコツは「わかっていないことに手を出さない」ことだろうか(これが難しい)。「わかっていないこと」は動作環境依存のことが多い。なので、「ファイルの置き場」とかは要注意
      • コンピュータ上の場所を制御する
        • Sweave,texの自作ファイルと、カスタマイズのために取ってきたファイル(styファイルと化)は、
          • 『すべて』一箇所にまとめておく
          • 邪魔でもなんでも、一つのフォルダにじかに置く
          • 場所を移動したくなったら、styファイルなどは、全部コピーして引っ越す
    • (4)は非常に多岐に渡りうるので、いろんな人がいろんなことをしている。
    • それは"hoge.sty"というようなファイルだったり、\usepackage{}という宣言だたったりするが、初めは『使おうとしない』ことにする。その上で、『決め打ちして、これだけ使う』として使うことにする(いつか、自分でスタイル調整できるようになるけれど、今は、そこに興味が強くなければ忘れよう)
    • この記事では、こちらを参考に、文書の形式としてjsbook と jsarticleの2つのみを使うことにする(jsbookとjsarticleのために複数のstyファイルが必要になる。こちらの説明に沿って、ファイル群をダウンロードし、全部を使えるようにする:『自分で決めた作業フォルダ』に全部、じかに置く)
  • 使ってみよう
    • ここまでできたら、jsbook,jsarticleが書けるはず
    • こちらtexファイルをそのまま使わせていただいて、実験。
    • まず、デスクトップに"asobiba"というフォルダを作り、こちらからとってきたjsbook,jsarticleのための諸ファイルをすべてコピーしておいて置く
    • そのフォルダに、"test.tex"という名前のファイル(以下)を保存
    • TexWorksで開いて、(文字化けしているけれど、そのまま)左上のpdfLaTeXの緑ボタンを押せば、フォルダ内に複数のファイルが生成され
    • PDFファイルがきちんと読める





\documentclass[11pt,a4paper]{jsarticle}
%
\usepackage{amsmath,amssymb}
\usepackage{bm}
\usepackage{graphicx}
\usepackage{ascmac}
%
\setlength{\textwidth}{\fullwidth}
\setlength{\textheight}{39\baselineskip}
\addtolength{\textheight}{\topskip}
\setlength{\voffset}{-0.5in}
\setlength{\headsep}{0.3in}
%
\newcommand{\divergence}{\mathrm{div}\,}  %ダイバージェンス
\newcommand{\grad}{\mathrm{grad}\,}  %グラディエント
\newcommand{\rot}{\mathrm{rot}\,}  %ローテーション
%
\pagestyle{myheadings}
\markright{\footnotesize \sf 物理のかぎしっぽ \ \texttt{ http://www12.plala.or.jp/ksp/}}
\begin{document}
%
%
\section*{◆タイトル◆}

%
%
\end{document}

0に立ち返ってのSweave

  • Sweave()とRnwファイル
    • Sweave()はhoge.Rnwという拡張子のファイルを読み込んでhoge.texというファイルを出力する
    • オオマカニtex文書であって、Rのソースを書きこみ、そのソースを実行した結果をプロット画像をtexの中に埋め込んでくれる仕組み
  • Sweave()を使ったtex文書は、Tex文書のスタイルの一つなので、styファイルが必要
    • Sweave.styとae.styの2つ
    • それぞれを「texの作業フォルダ」に「じかおき」する(初心者なので失敗しないため。作業場所を引っ越すときは、一緒にお引っ越し)
    • これ(Sweave,sty)とこれ(ae.sty)をコピーして、それぞれテキストファイルとして保存する(名前は"Sweave.sty","ae.sty")
  • Sweave()関数を使ってみる
    • ただのtexファイルをRのSweave()関数に処理させることもできる
      • 前の記事のtest.texを拡張子だけつけかえた"testSw.Rnw"としてみよう("test.Rnw"のままだと、Sweave()関数が"test.tex"を作ろうとしてバッティングするので名前をいじっただけ)
      • もちろんRの作業ディレクトリは、texファイル、Rnwファイル、styファイルのあるディレクトリにしておく
      • encoding対策としてSweave()関数の引数に注意
Sweave("testSw.Rnw",encoding="SJIS")
    • testSw.RnwとそれからできたtestSw.texとを以下に示す。1(2)カ所違う。
      • 1箇所目はtestSw.texの20行目に入った"\usepackage{Sweave}"の行
      • 2箇所目は最後に改行が入って行数が増えたこと(これはもとのRnwファイルの最後が改行されていれば変わらなかったのだろう)
      • このことからわかるように、Sweave()関数は、次の2つの仕事をする
        • Rnwという拡張子だが、基本はtex形式のファイルに"Sweave"パッケージを使うよ、という宣言を書きこむ
        • Rnwファイルに書かれたRのコードやその出力をしかるべきファイル名で作成しながら、texファイルに書き込んだり、ファイルの呼び込み行を書きこんだりする
    • "testSw.Rnw"ファイル
\documentclass[11pt,a4paper]{jsarticle}
%
\usepackage{amsmath,amssymb}
\usepackage{bm}
\usepackage{graphicx}
\usepackage{ascmac}
%
\setlength{\textwidth}{\fullwidth}
\setlength{\textheight}{39\baselineskip}
\addtolength{\textheight}{\topskip}
\setlength{\voffset}{-0.5in}
\setlength{\headsep}{0.3in}
%
\newcommand{\divergence}{\mathrm{div}\,}  %ダイバージェンス
\newcommand{\grad}{\mathrm{grad}\,}  %グラディエント
\newcommand{\rot}{\mathrm{rot}\,}  %ローテーション
%
\pagestyle{myheadings}
\markright{\footnotesize \sf 物理のかぎしっぽ \ \texttt{ http://www12.plala.or.jp/ksp/}}
\begin{document}
%
%
\section*{◆タイトル◆}

%
%
\end{document}
    • "testSw.tex"ファイル
\documentclass[11pt,a4paper]{jsarticle}
%
\usepackage{amsmath,amssymb}
\usepackage{bm}
\usepackage{graphicx}
\usepackage{ascmac}
%
\setlength{\textwidth}{\fullwidth}
\setlength{\textheight}{39\baselineskip}
\addtolength{\textheight}{\topskip}
\setlength{\voffset}{-0.5in}
\setlength{\headsep}{0.3in}
%
\newcommand{\divergence}{\mathrm{div}\,}  %ダイバージェンス
\newcommand{\grad}{\mathrm{grad}\,}  %グラディエント
\newcommand{\rot}{\mathrm{rot}\,}  %ローテーション
%
\pagestyle{myheadings}
\markright{\footnotesize \sf 物理のかぎしっぽ \ \texttt{ http://www12.plala.or.jp/ksp/}}
\usepackage{Sweave}
\begin{document}
%
%
\section*{◆タイトル◆}

%
%
\end{document}

0に立ち返ってのTex文書操作

  • jsbookで大規模な文書を作ってみよう
    • 大量に書きこむこともできるけれど、部分ごとに作って読み込ませる方式にする
    • 読み込ませる場合は、hoge.texというtexとしてちゃんとしたファイルのうち、以下のようにドキュメント本体として囲まれた内側だけのファイルにする
    • その部分はtexファイルとしての体裁をとっていないわけだけれど...
    • jsbookのファイルの中から、"useRtest_bare.tex"の"useRtest_bare"のみを指定して読み込ませる
  • 以下のように表紙から9ページの「本」ができていて、その第1章にuseRtest_bare.texの中身が入っている

\begin{document}
...
\end{document}
  • 抜き書きした"useRtest_bare.tex"ファイル
\lstset{%
 language={R},
 backgroundcolor={\color[gray]{.85}},%
 basicstyle={\small\tt },%
% basicstyle={\small },%
 identifierstyle={\small},%
 commentstyle={\small\itshape},%
 keywordstyle={\small\bfseries},%
 ndkeywordstyle={\small},%
 stringstyle={\small\ttfamily},
 frame={tb},
 breaklines=true,
 columns=[l]{fullflexible},%
 numbers=left,%
 xrightmargin=0zw,%
 xleftmargin=0zw,%
% xleftmargin=3zw,%
 numberstyle={\scriptsize},%
 stepnumber=1,
 numbersep=1zw,%
 lineskip=-0.5ex%
}

\section{Rを使ってみる}
Rの内側では日本語を使わない方が無難かも

\begin{Schunk}
\begin{Sinput}
R> data<-runif(10)
R> sort(data)
\end{Sinput}
\begin{Soutput}
 [1] 0.08061574 0.20693529 0.25479653 0.39116874 0.41828804 0.55967723 0.86937327 0.89790996
 [9] 0.90242920 0.95710237
\end{Soutput}
\begin{Sinput}
R> plot(sort(data))
\end{Sinput}
\end{Schunk}
\includegraphics{useRtest-001}


\begin{lstlisting}
data<-runif(10)
sort(data)
plot(sort(data))
\end{lstlisting}
  • jsbook本体のファイル "testBook.tex"
\documentclass{jsbook}
\author{
氏名
}

\title{本を作ってみる}

\usepackage{amsmath,amssymb}
\usepackage{bm}
%\usepackage{graphicx}
\usepackage{ascmac}
\usepackage[dvips]{graphicx}
\usepackage{graphicx}
\usepackage{bigdelim,multirow}
\usepackage{amsmath,amsthm,amssymb,cases}
\usepackage{ascmac}
\usepackage{url}
\usepackage{eclbkbox}
\usepackage{wrapfig}
\usepackage{listings, jlisting}
\usepackage[dvips,usenames]{color}
\usepackage{makeidx}
\renewcommand{\baselinestretch}{1}
\setlength{\textheight}{33\baselineskip}


\makeindex


%\setlength{\topmargin}{0mm}

\usepackage{Sweave}
\begin{document}
\maketitle
\tableofcontents

\mainmatter
\chapter{Rの基礎}
\include{useRtest_bare}

\chapter{DNAと遺伝子多型}

\chapter{分割表}

\end{document}

0に立ち返ってRコードをSweave()で使ってみる

  • tex文書にRのコードを書けばよい
  • 2通りを使うことに(限定)する
    • Rを実行して、その結果・出力プロットを埋め込む
    • Rのコードを背景色・行番号付きで表示させる
  • 以下のファイルは"useRtest.Rnw"と言うファイル
\begin{document}
    • までに
\usepackage
    • が続くが、(もしかしたら不要なものも含めて)ひとまず気にしないことにする
    • Rの実行によってできるファイルをeps形式で出力させたいために\SweaveOpts{eps=TRUE}という1行がある点は注意
  • Rの書き方は以下の2つ
    • (1)実行して、プロット画像を埋め込む場合
<<fig=TRUE>>=
data<-runif(10)
sort(data)
plot(sort(data))
@
    • (2)Rのソースを背景色つき書式にするための部分
\begin{lstlisting}
data<-runif(10)
sort(data)
plot(sort(data))
\end{lstlisting}
      • この書式を決めているのが、以下の部分で、これに関する説明はこちらを参照
\lstset{%
 language={R},
 backgroundcolor={\color[gray]{.85}},%
 basicstyle={\small\tt },%
% basicstyle={\small },%
 identifierstyle={\small},%
 commentstyle={\small\itshape},%
 keywordstyle={\small\bfseries},%
 ndkeywordstyle={\small},%
 stringstyle={\small\ttfamily},
 frame={tb},
 breaklines=true,
 columns=[l]{fullflexible},%
 numbers=left,%
 xrightmargin=0zw,%
 xleftmargin=0zw,%
% xleftmargin=3zw,%
 numberstyle={\scriptsize},%
 stepnumber=1,
 numbersep=1zw,%
 lineskip=-0.5ex%
}

  • コマンドは
Sweave("useRtest.Rnw",encoding="SJIS")
  • ファイル全体は以下の通り
\documentclass{jsarticle}

\usepackage{amsmath,amssymb}
\usepackage{bm}
%\usepackage{graphicx}
\usepackage{ascmac}
\usepackage[dvips]{graphicx}
\usepackage{graphicx}
\usepackage{bigdelim,multirow}
\usepackage{amsmath,amsthm,amssymb,cases}
\usepackage{ascmac}
\usepackage{url}
\usepackage{eclbkbox}
\usepackage{wrapfig}
\usepackage{listings, jlisting}
\usepackage[dvips,usenames]{color}
\usepackage{makeidx}
\SweaveOpts{eps=TRUE}
\begin{document}
\lstset{%
 language={R},
 backgroundcolor={\color[gray]{.85}},%
 basicstyle={\small\tt },%
% basicstyle={\small },%
 identifierstyle={\small},%
 commentstyle={\small\itshape},%
 keywordstyle={\small\bfseries},%
 ndkeywordstyle={\small},%
 stringstyle={\small\ttfamily},
 frame={tb},
 breaklines=true,
 columns=[l]{fullflexible},%
 numbers=left,%
 xrightmargin=0zw,%
 xleftmargin=0zw,%
% xleftmargin=3zw,%
 numberstyle={\scriptsize},%
 stepnumber=1,
 numbersep=1zw,%
 lineskip=-0.5ex%
}

\section{Rを使ってみる}
Rの内側では日本語を使わない方が無難かも

<<fig=TRUE>>=
data<-runif(10)
sort(data)
plot(sort(data))
@


\begin{lstlisting}
data<-runif(10)
sort(data)
plot(sort(data))
\end{lstlisting}

\end{document}

0に立ち返ってのTex文書操作/書籍内外と連携する

  • hyperrefパッケージを使うので、冒頭に
\usepackage[dvipdfmx,setpagesize=false]{hyperref}
  • と書いておきます。
  • jsarticleとの競合があるらしいので、特にsetpagesize=falseという宣言を入れておくらしいです
  • 使ってみましょう
    • こちらを参考に書いてみました。
  • Rnwで書いて、Sweave()で
Sweave("useHyperref.Rnw",encoding="SJIS")
  • tex化して、それをタイプセットx2回して、リンクが完成
  • Rnwとtexとpdfとを以下につけます
  • まずはuseHyperref.Rnwファイル
\documentclass{jsarticle}

\usepackage{amsmath,amssymb}
\usepackage{bm}
%\usepackage{graphicx}
\usepackage{ascmac}
\usepackage[dvips]{graphicx}
\usepackage{graphicx}
\usepackage{bigdelim,multirow}
\usepackage{amsmath,amsthm,amssymb,cases}
\usepackage{ascmac}
\usepackage{eclbkbox}
\usepackage{wrapfig}
\usepackage{listings, jlisting}
\usepackage{color}
\usepackage{makeidx}
\usepackage{url}
\usepackage[dvipdfmx,setpagesize=false]{hyperref}
\SweaveOpts{eps=TRUE}
\begin{document}
\section{hyperrefを使おう}\label{myD1S1}
\subsection{インターネット上のサイトへのリンクを張る}\label{myD1S1SS1}
\href{http://www.clas.kitasato-u.ac.jp/~fujiwara/infoScienceB/TeX/ref/labelAndRef.html}{文書内リンクの張り方のサイト}\\

この文字列『文書内リンクの張り方のサイト』はハイライトされていて、クリッカブルでウェブサイトにリンクが張られています。\\

このようなurlの埋め込みをするには、次のように書きます。\\

\verb|\href{http://www.clas.kitasato-u.ac.jp/~fujiwara/infoScienceB/TeX/ref/labelAndRef.html}{文書内リンクの張り方のサイト}|\\ 

第一の{}の中にurlを、第二の{}の中にハイライトされる文字列を書きます。

\subsection{ページ稼ぎのサブセクション}\label{myD1S1SS2}
Rでプロットをさせて少しページ稼ぎをしてみます。
<<fig=TRUE>>=
data<-runif(100)
plot(sort(data))
@
\section{文書内でリンクを張る}\label{myD1S2}
\subsection{セクションにジャンプする}\label{myD1S2SS1}
ここから文書の最初のセクション\ref{myD1S1}に戻ります。\\
最初のセクションのところは\\
\\
\verb|\section{hyperrefを使おう}\label{myD1S1}|\\
\\
と言うように\verb|\label{}|という仕組みを使ってありました。\\
飛ぶ先はこのように\verb|\label{}|で指定します。飛ぶ元は\\
\\
\verb|書の最初のセクション\ref{myD1S1}に|\\
\\
というように\verb|\ref{}|と書いておきます。\\
後ろにあるセクション3にもジャンプしてみます:セクション\ref{myD1S3}へ。\\

なお、リンクを作るときには、texがtex文書を前から後ろへ読みながら、内部リンクを探しますので、1回目のタイプセット(pdfpLaTeXとか)は、相互関係を見つけられないので、リンクがうまく張れずに「?」が現れます。もう一度タイプセットを実行すると問題は解消します。\\
\subsection{ラベルのあるページにジャンプする}\label{myD1S2SS2}
前のサブセクションのあるページの先頭にジャンプする(\pageref{myD1S2SS1})。\\
セクションに飛ぶのと同様に、ラベルの張ってある行があるページ(の先頭)に戻るには\\
\\
\verb|\pageref{myD1S2SS1}|\\
\\
のように\verb|\pageref|を使います。ここでは、一つ前のサブセクション「セクションにジャンプする」のページを指定してあります。
セクション3があるのは\pageref{myD1S3}ページです。そこへリンクしました。\\

\subsection{文書内の任意の場所にジャンプする}\label{myD1S2SS3}
第3セクションには英文が書いてあって、その中に「宝」と「千両箱」が埋まっています。そこへジャンプしてみます。\\
\hyperlink{t1}{宝へのリンク}\\
\hyperlink{t2}{千両箱へのリンク}\\

<<fig=TRUE>>=
data<-rnorm(100)
plot(sort(data))
@

\section{次のセクション}\label{myD1S3}
Wikipedia 英語版のEgyptの記事からの抜粋です。この中に、日本語の「宝」や「千両箱」を埋め込んでおき、そこにジャンプできるようにしておきましょう。\\
\verb|\hypertarget{t1}{宝}|,\verb|\hypertarget{t2}{千両箱}|のように埋め込んであります。\\

Egypt is one of the most populous countries in Africa and the Middle East, and the 15th most populated in the world. The great majority of its over 84 million people[4] live near the banks of the Nile River, an area of about 40,000 square kilometers (15,000 sq mi), where the only arable land is found. The large regions of the Sahara Desert, which constitute most of Egypt's territory, are sparsely inhabited. About half of Egypt's residents \textcolor{green}{\hypertarget{t1}{}} live in urban areas, with most spread across the densely populated centres of greater Cairo, Alexandria and other major cities in the Nile Delta.\\
Egypt has one of the longest histories of any modern state, having been continuously inhabited since the 10th millennium BC.[10] Its monuments, such as the Giza pyramid complex and its Great Sphinx, were constructed by its ancient civilization, which was one of the most advanced of its time. Its ancient ruins, such as those of Memphis, Thebes, Karnak, and the Valley of the Kings outside Luxor, are a significant focus of archaeological study and popular interest. Egypt's rich cultural legacy, as well as the attraction of its Red Sea Riviera, have made tourism a vital part of the economy, employing about 12% of the country's workforce.\\
The economy of Egypt is one of the most diversified in the Middle East, with sectors such as tourism, agriculture, industry and services at almost equal production levels. Egypt is considered to be a middle power,[11] with significant cultural, political, and military influence in North Africa\textcolor{blue}{\hypertarget{t2}{千両箱}}, the Middle East and the Muslim world.\\


\end{document}
  • ついでtexファイルuseHyperref.tex
\documentclass{jsarticle}

\usepackage{amsmath,amssymb}
\usepackage{bm}
%\usepackage{graphicx}
\usepackage{ascmac}
\usepackage[dvips]{graphicx}
\usepackage{graphicx}
\usepackage{bigdelim,multirow}
\usepackage{amsmath,amsthm,amssymb,cases}
\usepackage{ascmac}
\usepackage{eclbkbox}
\usepackage{wrapfig}
\usepackage{listings, jlisting}
\usepackage{color}
\usepackage{makeidx}
\usepackage{url}
\usepackage[dvipdfmx,setpagesize=false]{hyperref}

\usepackage{Sweave}
\begin{document}
\section{hyperrefを使おう}\label{myD1S1}
\subsection{インターネット上のサイトへのリンクを張る}\label{myD1S1SS1}
\href{http://www.clas.kitasato-u.ac.jp/~fujiwara/infoScienceB/TeX/ref/labelAndRef.html}{文書内リンクの張り方のサイト}\\

この文字列『文書内リンクの張り方のサイト』はハイライトされていて、クリッカブルでウェブサイトにリンクが張られています。\\

このようなurlの埋め込みをするには、次のように書きます。\\

\verb|\href{http://www.clas.kitasato-u.ac.jp/~fujiwara/infoScienceB/TeX/ref/labelAndRef.html}{文書内リンクの張り方のサイト}|\\ 

第一の{}の中にurlを、第二の{}の中にハイライトされる文字列を書きます。

\subsection{ページ稼ぎのサブセクション}\label{myD1S1SS2}
Rでプロットをさせて少しページ稼ぎをしてみます。
\begin{Schunk}
\begin{Sinput}
> data<-runif(100)
> plot(sort(data))
\end{Sinput}
\end{Schunk}
\includegraphics{useHyperref-001}
\section{文書内でリンクを張る}\label{myD1S2}
\subsection{セクションにジャンプする}\label{myD1S2SS1}
ここから文書の最初のセクション\ref{myD1S1}に戻ります。\\
最初のセクションのところは\\
\\
\verb|\section{hyperrefを使おう}\label{myD1S1}|\\
\\
と言うように\verb|\label{}|という仕組みを使ってありました。\\
飛ぶ先はこのように\verb|\label{}|で指定します。飛ぶ元は\\
\\
\verb|書の最初のセクション\ref{myD1S1}に|\\
\\
というように\verb|\ref{}|と書いておきます。\\
後ろにあるセクション3にもジャンプしてみます:セクション\ref{myD1S3}へ。\\

なお、リンクを作るときには、texがtex文書を前から後ろへ読みながら、内部リンクを探しますので、1回目のタイプセット(pdfpLaTeXとか)は、相互関係を見つけられないので、リンクがうまく張れずに「?」が現れます。もう一度タイプセットを実行すると問題は解消します。\\
\subsection{ラベルのあるページにジャンプする}\label{myD1S2SS2}
前のサブセクションのあるページの先頭にジャンプする(\pageref{myD1S2SS1})。\\
セクションに飛ぶのと同様に、ラベルの張ってある行があるページ(の先頭)に戻るには\\
\\
\verb|\pageref{myD1S2SS1}|\\
\\
のように\verb|\pageref|を使います。ここでは、一つ前のサブセクション「セクションにジャンプする」のページを指定してあります。
セクション3があるのは\pageref{myD1S3}ページです。そこへリンクしました。\\

\subsection{文書内の任意の場所にジャンプする}\label{myD1S2SS3}
第3セクションには英文が書いてあって、その中に「宝」と「千両箱」が埋まっています。そこへジャンプしてみます。\\
\hyperlink{t1}{宝へのリンク}\\
\hyperlink{t2}{千両箱へのリンク}\\

\begin{Schunk}
\begin{Sinput}
> data<-rnorm(100)
> plot(sort(data))
\end{Sinput}
\end{Schunk}
\includegraphics{useHyperref-002}

\section{次のセクション}\label{myD1S3}
Wikipedia 英語版のEgyptの記事からの抜粋です。この中に、日本語の「宝」や「千両箱」を埋め込んでおき、そこにジャンプできるようにしておきましょう。\\
\verb|\hypertarget{t1}{宝}|,\verb|\hypertarget{t2}{千両箱}|のように埋め込んであります。\\

Egypt is one of the most populous countries in Africa and the Middle East, and the 15th most populated in the world. The great majority of its over 84 million people[4] live near the banks of the Nile River, an area of about 40,000 square kilometers (15,000 sq mi), where the only arable land is found. The large regions of the Sahara Desert, which constitute most of Egypt's territory, are sparsely inhabited. About half of Egypt's residents \textcolor{green}{\hypertarget{t1}{}} live in urban areas, with most spread across the densely populated centres of greater Cairo, Alexandria and other major cities in the Nile Delta.\\
Egypt has one of the longest histories of any modern state, having been continuously inhabited since the 10th millennium BC.[10] Its monuments, such as the Giza pyramid complex and its Great Sphinx, were constructed by its ancient civilization, which was one of the most advanced of its time. Its ancient ruins, such as those of Memphis, Thebes, Karnak, and the Valley of the Kings outside Luxor, are a significant focus of archaeological study and popular interest. Egypt's rich cultural legacy, as well as the attraction of its Red Sea Riviera, have made tourism a vital part of the economy, employing about 12% of the country's workforce.\\
The economy of Egypt is one of the most diversified in the Middle East, with sectors such as tourism, agriculture, industry and services at almost equal production levels. Egypt is considered to be a middle power,[11] with significant cultural, political, and military influence in North Africa\textcolor{blue}{\hypertarget{t2}{千両箱}}, the Middle East and the Muslim world.\\


\end{document}
  • できたpdfファイルがこちら
    • ファイル内リンクは、ブラウザ上でもワークしていますが、ネット上のサイトへのリンクはブラウザ上では飛ばないようになるようです
    • ダウンロードしてやると飛べます
    • 不正なリンクの埋め込みとかがないように、というウイルスソフトとかブラウザの仕様が効いているのかと思います
  • 個々のRnw,tex文書については上記でOK
  • 多数のtex文書を束ねて「書籍」にするときも、各tex文書の冒頭に\hyperrefを埋め込めば、書籍全体で飛べるだろう
  • やってみる
  • texファイル testHyperrefBook.texを次のように書いて、useRtest_bare.texとuseHyperref_bare.texをそれぞれのRnw経由のtexファイルの\begin{document}と\end{document}の間の部分だけにしたものとして保存しておけば、読み込みリンクのトップに飛べる
\documentclass{jsbook}
\author{
氏名
}

\title{記事間連絡のある本を作ってみる}

\usepackage{amsmath,amssymb}
\usepackage{bm}
%\usepackage{graphicx}
\usepackage{ascmac}
\usepackage[dvips]{graphicx}
\usepackage{graphicx}
\usepackage{bigdelim,multirow}
\usepackage{amsmath,amsthm,amssymb,cases}
\usepackage{ascmac}
\usepackage{url}
\usepackage{eclbkbox}
\usepackage{wrapfig}
\usepackage{listings, jlisting}
\usepackage[dvips,usenames]{color}
\usepackage{makeidx}
\usepackage[dvipdfmx,setpagesize=false]{hyperref}
\renewcommand{\baselinestretch}{1}
\setlength{\textheight}{33\baselineskip}


\makeindex


%\setlength{\topmargin}{0mm}

\usepackage{Sweave}
\begin{document}
\maketitle
\tableofcontents
\mainmatter
\chapter{Rの基礎}
\include{useHyperref_bare}
\include{useRtest_bare}
2文書間の関係を書きたい\\
iseRtest bare文書は
useHyperref bareに関係しています。その冒頭に飛びたいなら、こちら\ref{myD1S1}\\
その中の「宝」へ飛びたいなら
\hyperlink{t1}{宝へのリンク}\\

\end{document}