# サンプル画像等を参照するパス
file_loc <- system.file("resources", package = "rmdja")
# サンプル引用文献リストを取得する
file.copy(file.path(file_loc, "examples/examples.bib"), to = "./")

knitr::opts_chunk$set(cache = T)

\textrm{Нужны новые формы. Новые формы нужны, а если их нет, то лучше ничего не нужно.}{=latex}

\textmc{新しいフォーマットが必要なんですよ. 新しいフォーマットが. それがないというなら, いっそ何もないほうがいい.}{=latex} r tufte::quote_footer('--- A. チェーホフ『かもめ』')

目次

\tableofcontents[hideallsubsections]

イントロダクション

このスライドは何?

想定される用途

先行事例の紹介

reveal.js じゃダメなの?

パワーポイントじゃダメなの?

使い方/用例

セットアップ

  1. パッケージのインストール
remotes::install_github(
  "Gedevan-Aleksizde/my_latex_templates",
  subdir = "rmdja")
  1. TeXLive (>= 2020)のインストール
  2. 分からなければTeX wiki のページを参考に
  3. Debian/Ubuntuのユーザはaptより公式ダウンローダの方が良いかも

基本 (1/2)

  1. yamlヘッダに以下を書く
  2. 詳しくはexample/のテンプレートを参考に
output: rmdja::beamer_presentation_ja
  1. RStudioのツールバーの"Knit"を押す
knitr::include_graphics(file.path(file_loc, "img/render.png"))

基本 (2/2)

knitr::include_graphics(file.path(file_loc, "img/readme-selection.png"))

最低限必要な設定

output:
  rmdja::beamer_presentation_ja:
    latex_engine: lualatex
mainfont: "`r if(Sys.info()['sysname'] == 'Linux'){'DejaVu Serif'} else {'Times New Roman'}`"
mainfontoptions:
sansfont: "`r if(Sys.info()['sysname'] == 'Linux'){'DejaVu Sans'} else {'Arial'}`"
monofont: 'Courier New'
jfontpreset: "`r switch(Sys.info()['sysname'], 'Linux' = 'noto', 'Darwin' = 'hiragino-pro', 'Windows' = 'ms')`"

基本構文 (1/2)

# 節見出し
## タイトル1
- **太字** **bold**
- _強調_ _emph_
- `タイプライタ体` `mono`
- ~~取り消し線~~

基本構文 (2/2)

ソースコードの表示

names(knitr::knit_engines$get())

BeamerやRMarkdown使用に役立つ資料

もう少しくわしいやつ

今回使うパッケージ

require(conflicted)    # パッケージの競合防止用
require(tidyverse)     # 全般
require(ggthemes)      # ggplot2のデザイン変更
require(ggdag)         # ネットワーク図の用例に
require(DiagrammeR)    # DOT言語でのネットワーク図例
require(kableExtra)    # 表の出力オプション
require(stargazer)     # 複雑なLaTeXの表を扱う例

ソースコードの表示: 基本事項

ソースコードの表示: 出力例

text <- '```r
require(conflicted) 
require(tidyverse)
require(ggthemes)
```'
cat(text)

```{R load-packages2, echo=T, class.source = "numberLines LineAnchors", eval=F} require(conflicted) require(tidyverse) require(ggthemes)

## カラー絵文字

* [`BXcoloremoji`](https://github.com/zr-tex8r/BXcoloremoji)をインストール
  + `\coloremoji{}` で絵文字表示: \ifdefined\coloremoji \coloremoji{🍣} \else (ここに絵文字) \fi
  + 実際には**画像に置き換えている**
* グラフ描画には特に設定必要なし
  + ソースコード上のものは文字化けする

```r
plot(1:10, pch = "🍣")

数式の挿入: 行内(インライン)

数式の挿入: 独立行

$$\begin{aligned}
& \sin^2(x) + \cos^2(x) = 1\\
& f(x) = \frac{1}{(2\pi)^2}\int_{\mathbb{R}^n}
\hat{f}(\omega)\exp(i\omega x)d\omega
\end{aligned}$$

$$\begin{aligned} & \sin^2(x) + \cos^2(x) = 1\ & f(x) = \frac{1}{(2\pi)^2}\int_{\mathbb{R}^n}\hat{f}(\omega)\exp(i\omega x)d\omega \end{aligned}$$

数式の挿入: bookdown パッケージのアドインで補完

  1. RStudioのツールバー "Addins"
  2. "Input LaTeX Math"
knitr::include_graphics(file.path(file_loc, "img/math-input.png"))

用例: 図表の挿入

図の挿入: 画像ファイル貼り付け

knitr::include_graphics(file.path(file_loc,
  c("img/tiger.eps", "img/tiger.pdf", "img/tiger.png")))

図の挿入: markdown構文で貼り付け

![The Tiger](img/tiger.pdf){ height=30% }

The Tiger{ height=30% }

図の挿入: ggplot2のグラフ

theme_set(
  theme_classic(base_size = 11) +
  theme(legend.position = "top",
        axis.title.y = element_text(angle = 90, vjust = .5), legend.title = element_blank()))
data(iris)
g <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() + labs(x = "萼長", y = "萼幅")
g

図の挿入: 文字の大きさをそろえるには

図の挿入: 画像サイズの基本ルール

図の挿入: 幅100%で出力

## 図の挿入: beamerサイズで保存, 幅100%で出力

* 相対的に文字が大きくなった
````r
g + labs(title = "Iris") + theme_classic(base_size =11) + theme(axis.text = element_text(size = 11))

図の挿入: 字の大きさをなるべく揃える

図の挿入: 再現可能なポンチ絵

図の挿入: ポンチ絵の例1

diag_yjseir <- dagify(
  Y ~ E,
  J ~ E,
  S ~ Y,
  E ~ S,
  I ~ E + Y,
  R ~ J + I,
  labels = c("Y" = "懐疑", "J" = "間接接触", "S" = "感受性", "E" = "潜伏", "I" = "感染", "R" = "回復"),
  coords = list(x = c(Y = 1.5, J = 2, S = 0, E = 1, I = 2, R =3),
                y = c(Y = .5, J = -.5, S = 0, E = 0, I = 0, R = 0)
                )
  ) %>% tidy_dagitty() %>% mutate(label_edge = c("beta", NA, NA, "gamma", NA, "lambda",  NA, NA, NA))
diag_yjseir %>% ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
  geom_dag_node(size = 9 * .pt) +
  geom_dag_edges(edge_width = 2, arrow_directed = grid::arrow(length = grid::unit(.05, "npc"), type = "closed")) +
  geom_dag_text(aes(label = label)) +
  geom_label(aes(x = (x + xend)/2, y = (y + yend)/2, label = label_edge), parse = T, size = 10) +
  theme_dag_blank() + coord_cartesian(ylim=c(-1, 1))

図の挿入: ポンチ絵の例2

require(ggforce)
df.venn <- data.frame(
  x = c(-.5, 0, .5), y = c(0, .8, 0), r = c(1, 1, 1), labels = c('コーディング', 'デザイン', 'タイポグラフィ'))
ggplot(df.venn, aes(x0 = x, y0 = y, r = r, fill = labels)) +
  geom_circle(alpha = .3, size = 1, colour = 'grey') +
  geom_label(aes(x = x, y = y, label = labels)) +
  annotate("text", x = 0, y = .2, label = "LaTeX", size = 11 / .pt) +
  coord_fixed() + scale_fill_pander() +
  theme_void(base_size = 11) + theme(legend.position = "none")

図の挿入: DiagrammeR でDOT言語で書く

grViz("
digraph boxes_and_circles {

  # a 'graph' statement
  graph [overlap = true, fontsize = 15]

  # several 'node' statements
  node [shape = box]
  A; B; C; D; E; F

  node [shape = circle,
        fixedsize = true,
        width = 0.9] // sets as circles
  1; 2; 3; 4; 5; 6; 7; 8

  # several 'edge' statements
  A->1 B->2 B->3 B->4 C->A
  1->D E->A 2->4 1->5 1->F
  E->6 4->6 5->7 6->7 3->8
}
")

図の挿入: R以外のデバイス

表の挿入: データフレーム

data(iris)
print(head(iris))

表の挿入: データフレームをkable()で表示

data(iris)
kable(head(iris[, 1:3]),
      caption="kable()による表示")

表の挿入: 外部の.texファイル

file.copy(file.path(file_loc, "examples/tab.tex"), "./")

表の挿入: .texで書かれた表を掲載

\input{tab.tex}

表の挿入: stargazer の表示

fit1  <- lm(Sepal.Length ~ Petal.Width, data = iris)
fit2 <- lm(Sepal.Length ~ Petal.Width + Petal.Length,
           data = iris)
stargazer(fit1, fit2,
          header = F, type = "latex",
          digits = 2, digits.extra = 0, align = T,
          ...

表の挿入: stargazer の出力結果

\scriptsize

fit1  <- lm(Sepal.Length ~ Petal.Width, data = iris)
fit2 <- lm(Sepal.Length ~ Petal.Width + Petal.Length, data = iris)
stargazer(fit1, fit2,
          header = F, type = "latex", 
          digits = 2, digits.extra = 0, align = T,
          omit.table.layout = "n",
          intercept.bottom = F,
          keep.stat = c("n", "adj.rsq", "f"), df = F,
          title            = "回帰分析の結果",
          covariate.labels = c("定数項", "花弁幅", "花弁長"),
          dep.var.caption  = "モデル1",
          dep.var.labels   = "萼長")

\normalsize

表の挿入: markdown構文

\scriptsize

Table: 得点一覧

  クラス 科目   平均
  ------ ----- -----
  A      算数   $90$
  B      算数   $95$
  ------ ----- -----

\normalsize

Table: 得点一覧

クラス 科目 平均


A 算数 $90$ B 算数 $95$


外部資料の引用方法

ハイパーリンクの挿入

文献引用の方法

[@R-tidyverse; @R-rmarkdown; @rmarkdown2018; @R-bookdown]
[@R-citr; @wickham2016Data; @Okumura2017LaTeX]

[@R-tidyverse; @R-rmarkdown; @rmarkdown2018; @R-bookdown] [@R-citr; @wickham2016Data; @Okumura2017LaTeX]

文献引用の補助: 引用子の補完

knitr::include_graphics(file.path(file_loc, "img/citr.png"))

文献引用の補助: 文献管理

基本的なカスタマイズ

フォント変更: 欧文 (1/2)

フォント変更: 欧文 (2/2)

mainfont: <欧文フォント>
  mainfontoptions:
    - Scale=1.1
    - Ligatures=TeX
sansfont: <欧文サンセリフ体フォント>
monofont: <等幅フォント>

フォント変更: 和文

jmainfont: <和文フォント>
jmainfontoptions: <オプション>
jsansfont: <和文ゴシックフォント>
jmonofont: <和文等幅フォント>

フォント変更: 和文プリセット

フォント変更: 和文プリセットの設定例

jfontpreset: noto-otf
jfontpresetoptions:
  - match
  - deluxe
  - no-math

フォント変更: 和文フォントプリセット

Table: 主なプリセット名対照表

|フォント |\XeLaTeX |\LuaLaTeX| |:--------|---------:|-------------:| |ヒラギノ |hiragino|hiragino-pro| |MS |ms |ms| |Noto |noto/noto-jp|noto-otf/noto-otc|

インラインでのフォント変更

ここはいつものフォント.
`\textmc{ここだけ明朝 \textrm{Mincho}}`{=latex}

ここはいつものフォント.\textmc{ ここだけ明朝 \textrm{Mincho}.}{=latex}

スライドのテーマ変更

output:
  rmdja::beamer_presentation_ja:
    fonttheme: professionalfonts
    code_rownumber: true

シンタックスハイライトのテーマ変更

output:
  rmdja::beamer_presentation_ja:
  highlight: tango

ハイパーリンクの色の変更

linkcolor: blue
citecolor: green
urlcolor: red

アスペクト比の変更

output:
  rmdja::beamer_presentation_ja:
    fig_width: 6.29921
    fig_height: 3.54331
aspectratio: 169

引用形式の変更

引用形式の変更例

output:
  rmdja::beamer_presentation_ja:
    citation_package: natbib
    citation_options: authoryear

参考文献リストの変更

bibliography: examples.bib
biblio-style: jecon

「図」「表」の表示

output:
  rmdja::beamer_presentation_ja:
    figurename: Fig.
    tablename: Tab.
biblio-title: Further Readings

トラブルシューティング

Q 1: オプションが反映されない

?rmdja::beamer_presentation_ja

Q 2: エラーの原因がよくわからない

まとめ

結果どうなったか

改良・機能追加したいところ

補足: 細かい技術的な話

このセクションの想定読者

ハマりポイント

  1. \XeLaTeX で取り消し線を付ける場合の問題
  2. 和文に取り消し線を付けるとタイプセットエラーが発生した
  3. zxjatypeulemの競合と思われる
  4. xeCJKnftefを読み込むとなんか解決した
  5. 詳細: TeX フォーラム

  6. \LuaLaTeX と \XeLaTeX で文字サイズが変わってしまう

技術的に厄介だったところ

実装の特徴

日本語文献にどう対応しているか

謝辞



Gedevan-Aleksizde/rmdja documentation built on Sept. 28, 2021, 2:49 a.m.