[r] Rstudio rmarkdown : 하나의 PDF에서 세로 및 가로 레이아웃 모두

rmarkdown동일한 문서에서 세로 및 가로 레이아웃이 모두있는 pdf를 생성 하는 데 사용하는 방법이 궁금합니다 . rmarkdown라텍스를 사용하는 것보다 더 나은 순수한 옵션 이 있다면 .

다음은 재현 가능한 작은 예입니다. 먼저 .RmdRStudio에서 렌더링 ( PDF Knit 버튼 누르기)하면 모든 페이지가 가로 레이아웃으로 된 pdf가 생성됩니다.

---
title: "All pages landscape"
output: pdf_document
classoption: landscape
---

```{r}
summary(cars)
```

\newpage
```{r}
summary(cars)
```

그런 다음 세로 및 가로 레이아웃이 혼합 된 문서를 만들려고합니다. 의 기본 설정은 여기YAML 의 ‘포함’섹션에 따라 수행 됩니다 . in_header파일 ‘header.tex’는 포함 \usepackage{lscape}, 권장 패키지 knitr가로 레이아웃 여기를 . .tex파일과 같은 디렉토리에 .Rmd파일.

---
title: "Mixing portrait and landscape"
output:
    pdf_document:
        includes:
            in_header: header.tex
---

Portrait:
```{r}
summary(cars)
```

\newpage
\begin{landscape}
Landscape:
```{r}
summary(cars)
```
\end{landscape}

\newpage
More portrait:
```{r}
summary(cars)
```

그러나이 코드는 오류를 발생시킵니다.

# ! You can't use `macro parameter character #' in horizontal mode.
# l.116 #

# pandoc.exe: Error producing PDF from TeX source
# Error: pandoc document conversion failed with error 43

어떤 도움이라도 대단히 감사합니다.



답변

따라서 라텍스 환경의 내용을 구문 분석 pandoc 하지 않지만 파일 의 명령재정 의하여 속일 수 header.tex있습니다.

\usepackage{lscape}
\newcommand{\blandscape}{\begin{landscape}}
\newcommand{\elandscape}{\end{landscape}}

따라서 여기 \begin{landscape}에서 \blandscape, 및 \end{landscape}로 재정의 됩니다 \elandscape. .Rmd파일 에서 새로 정의 된 명령을 사용하면 작동하는 것 같습니다.

---
title: "Mixing portrait and landscape"
output:
    pdf_document:
        includes:
            in_header: header.tex
---

Portrait
```{r}
summary(cars)
```

\newpage
\blandscape
Landscape
```{r}
summary(cars)
```
\elandscape

\newpage
More portrait
```{r}
summary(cars)
```


답변

이전 솔루션을 기반으로하는 다음 솔루션에는 보조 header.tex파일이 필요하지 않습니다 . 모든 내용이 .Rmd파일에 포함되어 있습니다. LaTeX 명령은 대신 header-includesYAML 헤더 의 블록에 정의됩니다 . 자세한 정보는 여기 에서 찾을 수 있습니다 .

또한 lscapeLaTeX 패키지 를 사용하면 페이지 내용이 회전하지만 PDF 페이지 자체는 회전하지 않습니다. 이것은 pdflscape패키지를 사용하여 해결 됩니다.

---
title: "Mixing portrait and landscape WITHOUT a header.tex file"
header-includes:
- \usepackage{pdflscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
output: pdf_document
---

Portrait
```{r}
summary(cars)
```

\newpage
\blandscape
Landscape
```{r}
summary(cars)
```
\elandscape

\newpage
More portrait
```{r}
summary(cars)
```


답변

가장 일반적인 경우.

3 가지 조건이 있습니다.

  1. 모든 것이 세로 모드입니다.
  2. 모든 것이 가로 모드입니다.
  3. 세로 및 가로 모드의 혼합.

각 조건으로 좁혀 보겠습니다.

  1. 첫 번째는 아래 코드로 시작하는 마크 다운 문서가 있다고 가정합니다. 그리고 이것은 rmd 파일을 만들 때 Rstudio의 기본 설정입니다. 뜨다 할 때. 의심의 여지없이 세로 모드라고 자동으로 가정합니다.

    title: "Landscape and Portrait"
        author: "Jung-Han Wang"
        date: "Thursday, March 19, 2015"
        output: pdf_document
    
  2. PDF 파일을 가로 모드로 짜고 싶을 때 추가해야하는 유일한 것은 classoption : landscape

        title: "Landscape and Portrait"
        author: "Jung-Han Wang"
        date: "Thursday, March 19, 2015"
        output: pdf_document
        classoption: landscape
    
  3. 두 가지를 혼합하려면 YAML에 .tex 파일을 추가해야합니다. 위에서 언급 한 링크를 참조하여. 여기에서 .tex 코드를 다운로드 할 수 있습니다. http://goo.gl/cptOqg 또는 단순히 코드를 복사하여 header.tex로 저장합니다. 그런 다음 편하게 사용할 수 있도록이 .tex 파일을 편성 할 rmd 파일과 함께 넣습니다. 다음 두 가지 작업을 수행했는지 확인하십시오. tex 파일을 복사하고 rmd 파일과 함께 이동하십시오. rmd의 시작을 다음과 같이 변경하십시오.

     title: "Landscape and Portrait"
        author: "Jung-Han Wang"
        date: "Thursday, March 19, 2015"
        output:
          pdf_document:
            includes:
              in_header: header.tex
    

이것은 내가이 문제를 가지고 놀았고 대부분 침례교의 대답으로부터 혜택을받은 후의 요약입니다.

내 블로거 내 블로거 에 몇 가지 스냅 샷과 예제를 포함했습니다 .

도움이 되었기를 바랍니다. 행운을 빕니다.


답변

Baptiste가 언급했듯이 LaTeX 환경 내에 R 명령을 포함하면 pandoc은 명령을 구문 분석하지 않고 생성 된 LaTeX에있는 그대로 배치합니다. 이것이 오류의 원인입니다. Baptiste의 멋지고 간단한 수정 외에도 xtableR 패키지를 사용하여 R 출력에서 ​​더 섹시한 LaTeX 테이블을 생성 할 수 있습니다 . 다음 예제가 작동 \usepackage{rotating}하려면 header.tex파일 에 추가해야 합니다.

---
title: "Mixing portrait and landscape"
output:
    pdf_document:
        keep_tex: true
        includes:
            in_header: header.tex
---
```{r, echo=FALSE}
library(xtable)
```

Portrait
```{r, results='asis', echo=FALSE}
print(xtable(summary(cars), caption="Landscape table"), comment=FALSE)
```

Landscape:
```{r, results='asis', echo=FALSE}
print(xtable(summary(cars), caption="Landscape table"),
      floating.environment="sidewaystable", comment=FALSE)
```

두 번째 표는 sidewaystable평소가 아닌 환경 내에서 인쇄 table되므로 별도의 페이지에 가로 모드로 인쇄됩니다. lscape패키지 또는 sideways환경 에 의해 가로 모드로 배치되는 표와 그림 은 항상 별도의 페이지에 배치됩니다.이 매우 중요한 문서의 91 페이지를 참조하십시오.

http://www.tex.ac.uk/tex-archive/info/epslatex/english/epslatex.pdf

이 작업이 약간 짜증나 기 때문에 동일한 페이지에 세로 및 가로 테이블을 모두 보관할 수있는 방법을 찾았습니다 (프로세스에서 오후 전체를 낭비 함).

---
title: "Mixing portrait and landscape"
output:
    pdf_document:
        keep_tex: true
        includes:
            in_header: header.tex
---
```{r, echo=FALSE}
library(xtable)
```

Portrait:
```{r, results='asis', echo=FALSE}
print(xtable(summary(cars), caption="Portrait table."), comment=FALSE)
```

Landscape:
```{r, results='asis', echo=FALSE}
cat(paste0(
    "\\begin{table}[ht]\\centering\\rotatebox{90}{",
    paste0(capture.output(
      print(xtable(summary(cars)), floating=FALSE, comment=FALSE)),
      collapse="\n"),
    "}\\caption{Landscape table.}\\end{table}"))
```

가로 테이블의 \rotatebox경우 여기에 제공된 제안을 사용했습니다 .

http://en.wikibooks.org/wiki/LaTeX/Rotations

이 작업을 수행하려면 tabular부품이있는 테이블 의 일부만 생성 print(xtable(...한 다음 출력을 캡처하고 tablerotatebox명령 으로 “수동으로”둘러싸고 모든 것을 문자열 R 출력으로 변환하여 pandoc가 보지 않도록해야합니다. LaTeX 환경으로. 순수한 rmarkdown 솔루션을 위해 … 행운을 빕니다!


답변