knitr를 사용하여 클래스/패키지/테마 옵션을 R에 전달

knitr를 사용하여 클래스/패키지/테마 옵션을 R에 전달

beamer'template.Rnw'라는 덩어리가 있는 문서가 있는데 , 'PDF 컴파일'을 사용하여 R컴파일합니다.knitrRStudio. '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}

mytheme.sty

\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}

관련 정보