私は「template.Rnw」というチャンクbeamer
を含む文書を持っていてR
、それをknitr
「PDFをコンパイル」を使用してコンパイルします。RStudio. テーマ「mytheme.sty」を定義しました。テーマにオプションを追加してFrench
、ドキュメントの先頭で次のように呼び出すようにします。
\usetheme[French]{mytheme}
$1,000,000.00
この「フランス語」テーマは、 として印刷されるなど、米国スタイルとは異なるグラフィックのラベル書式設定オプションを定義します1 000 000,00$
。
French == 1
「mytheme.sty」内にブール値を作成し、それをR
条件で使用するためにチャンクに渡すことができると考えましたif (French) { # set the French styles here
。
しかし、まず、LaTeX
カウンタ値(例えば)をR
チャンクに渡す方法がわかりません。また、おそらくもっと良い方法があるでしょう。もしそうなら、ぜひ知りたいです。以下は私のMWEです(警告:コンパイルされません!)
テンプレート.Rnw
\documentclass{beamer}
\usetheme{mytheme}
%\usetheme[French]{mytheme}
<<'setup', include=FALSE>>=
library(knitr)
library(ggplot2)
library(scales)
@
% I'd want to hide this chunk inside mytheme.sty or similar
<<'formats', include=FALSE>>=
# Create euro currency symbol in plot labels with library(scales)
euro <- function(x) {
paste0("€", format(x, big.mark = ",", decimal.mark = ".", trim = TRUE,
scientific = FALSE))
}
# French style formatting for euro labels
euroFrench <- function(x) {
paste0(format(x, big.mark = " ", decimal.mark = ",", trim = TRUE,
scientific = FALSE), " €")
}
# French style formatting for dollar labels
dollarFrench <- function(x) {
paste0(format(x, big.mark = " ", decimal.mark = ",", trim = TRUE,
scientific = FALSE), " $")
}
# Toggle On/Off to set formats to French or US
if (French) { # Here reads the boolean French set by the option [French]
euro <- euroFrench # set the custom 'euro' style to custom 'euroFrench'
dollar <- dollarFrench # overwrite the default 'dollar' styles of library(scales)
}
@
\begin{document}
\begin{frame}
\frametitle{One day I will be a dual-axis euro/dollar plot}
<<'plot', out.width='1\\linewidth'>>=
df <- data.frame(x = c(0, .0001, .0002, .0003), y = c(0, 1000000, 2000000, 3000000))
ggplot(data = df, aes(x = x, y = y)) + geom_line() + theme_classic(30) + scale_x_continuous(labels = dollar) + scale_y_continuous(labels = euro)
@
\end{frame}
\end{document}
マイテーマ
\newcounter{French}
\newif\if@themeFrench%
\@themeFrenchfalse%
\DeclareOption{French}{\@themeFrenchtrue}%
\ProcessOptions%
\if@themeFrench%
\setcounter{French}{1}
@
\fi
プロットR
クラス外 で取得しますbeamer
。
答え1
LaTeX 変数を R に渡す方法はありません (少なくとも簡単な方法はありません)。その逆、つまり R 変数を使用して LaTeX を記述する方がはるかに簡単です。
\documentclass{article}
<<include=FALSE>>=
French <- 1
@
<<results='asis'>>=
if (French == 1) cat('\\usepackage[French]{mytheme}')
@
\begin{document}
...
\end{document}