2017-11-21 9 views
2

저는 현재 rmarkdown을 사용하여 보고서를 작성 중이므로 r 코드 청크 안에 섹션을 만들고 싶습니다. 나는 이것이 고양이()결과 = "asis"의 도움으로 가능하다는 것을 알아 냈습니다. 이 솔루션의 문제점은 내 R 코드 결과와 코드가 정상적으로 올바르게 표시되지 않는다는 것입니다.R 코드 덩어리 안에 R-markdown 섹션을 만드는 방법은 무엇입니까? 적절한 코드 표시

--- 
title: "test" 
output: pdf_document 
--- 

```{r, results='asis'} 
for (i in 1:10) { 
    cat("\\section{Part:", i, "}") 
    print(summary(lm(data=X, X1~X2)) 
    $\alpha = `r X[1,i]`$ 
} 
``` 

거의 예를 들어

트릭을 수행하지만 여기에 두 가지 문제가 아직 없습니다 : 나는 그것을 생각하기 때문에

  • 요약()에 대한 R 출력이 아주 이상한 표시됩니다은 `LaTeX 코드로 해석 됨
  • 이 환경에서는 LaTeX 수식을 사용할 수 없으므로 모든 섹션을 R 변수를 사용할 수있는 수식으로 끝내기를 원한다면 이것은 불가능합니다 e

누군가가이 문제에 대한 해결책을 알고 있습니까? 아니면 루프 내에서 섹션을 만들고이 섹션에 R 코드, R 출력 및 LaTeX 수식을 포함 할 수있는 해결 방법이 있습니까?

나는 덩어리 네 \와 코드를 탈출 조언 :

답변

1

인하를 사용하고 그대로 환경

당신은 verbatim 환경 R 출력을 둘러싸고 수있는 모든 종류의 매우 감사입니다.

```{r, results='asis'} 
for (i in 1:10) { 
    cat("\\section{Part:", i, "}") 
    cat("\\begin{verbatim}") 
    print(summary(lm(data=mtcars, mtcars[,1]~ mtcars[,i]))) 
    cat("\\end{verbatim}") 
    cat(paste0("$\\\\alpha$ = ", mtcars[1,i])) 
} 
``` 

enter image description here

뭔가 라텍스를 실행할 때, 내가, 내가 찾을 인하 (.Rmd)를 종료하고 (.Rnw)를 sweave을 사용하는 경향이 어떤 점에서 리스팅 패키지

를 사용하여 더 많은 공상 훨씬 더 쉽고, .tex 파일에서 무슨 일이 일어나고 있는지 확인하고 무엇이 잘못되었는지를 파악할 수 있습니다.

\documentclass[a4paper]{article} 
\usepackage{listings} 
\usepackage[usename,dvipsnames]{xcolor} 
% create a set of colors 
\definecolor{mygreen}{rgb}{0,0.6,0} 
\definecolor{mygray}{rgb}{0.5,0.5,0.5} 
\definecolor{mymauve}{rgb}{0.58,0,0.82} 
% create a listings environment suitable for R code 
\lstset{ % 
    backgroundcolor=\color{white}, % choose the background color; you must add \usepackage{color} or \usepackage{xcolor} 
    basicstyle=\footnotesize\ttfamily, % the size of the fonts that are used for the 
    % code 
    breakatwhitespace=false,   % sets if automatic breaks should only happen at whitespace 
    breaklines=true,     % sets automatic line breaking 
    captionpos=b,     % sets the caption-position to bottom 
    commentstyle=\color{mygreen},  % comment style 
    deletekeywords={...},   % if you want to delete keywords from the given language 
    escapeinside={\%*}{*)},   % if you want to add LaTeX within your code 
    extendedchars=true,    % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8 
    frame=single,     % adds a frame around the code 
    keepspaces=true,     % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible) 
    keywordstyle=\color{blue},  % keyword style 
    language=R,      % the language of the code 
    morekeywords={*,...},   % if you want to add more keywords to the set 
    numbers=left,     % where to put the line-numbers; possible values are (none, left, right) 
    numbersep=5pt,     % how far the line-numbers are from the code 
    numberstyle=\tiny\color{mygray}, % the style that is used for the line-numbers 
    rulecolor=\color{black},   % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here)) 
    showspaces=false,    % show spaces everywhere adding particular underscores; it overrides 'showstringspaces' 
    showstringspaces=false,   % underline spaces within strings only 
    showtabs=false,     % show tabs within strings adding particular underscores 
    stepnumber=2,     % the step between two line-numbers. If it is 1, each line will be numbered 
    stringstyle=\color{mymauve},  % string literal style 
    tabsize=2,      % sets default tabsize to 2 spaces 
    title=\lstname     % show the filename of files included with \lstinputlisting; also try caption instead of title 
} 
\title{test} 
\begin{document} 
\maketitle 

<<r, results='asis'>>= 
for (i in 1:10) { 
    cat("\\section{Part:", i, "}") 
    cat("\\begin{lstlisting}") 
    print(summary(lm(data=mtcars, mtcars[,1]~ mtcars[,i]))) 
    cat("\\end{lstlisting}") 
    cat(paste0("$\\\\alpha$ = ", mtcars[1,i])) 
} 
@ 

\end{document} 

enter image description here