2013-02-05 8 views
6

저는 LaTeX에서 멋지게 표시하고자하는 코드 조각에 선형 모델을 가지고 있습니다. 모델 호출은 물결표가있는 표준 양식을 사용하여 LaTeX에서 조판됩니다.예쁜 틸드 ~ R 덩어리에서 knitr로?

\documentclass{article} 
\begin{document} 
<<>>= 
lm(Sepal.Width ~ Sepal.Length, data = iris) 
@ 
\end{document} 

코드는 knitr::knit(mwe.Rnw) 편성하고 PDFLaTeX을 통해 실행합니다.

LaTeX에서 좋은 물결표를 만드는 것은 매우 성가 시며 knitr을 사용하는 것이 훨씬 쉽고 쉬워 보이지 않습니다. knit에 의해 생성 된 .tex 파일을 검사하면 코드가 세 가지 환경에 놓여 있음을 알 수 있으며 그 중 \begin{alltt} ... \end{alltt}이 흥미 롭습니다. 그러나 패키지 alltt은 특수 문자의 특수한 조판에 대한 빠른 수정을 제공하지 않습니다.

답변

7

이 솔루션은 yihui's example on hooks, this post 및 내 친구 RJ로부터 영감을 받았습니다.

\documentclass{article} 
\usepackage{xspace} 
\newcommand{\mytilde}{\lower.80ex\hbox{\char`\~}\xspace} 
\begin{document} 
<<setup, include=FALSE>>= 
library(knitr) 
hook_source = knit_hooks$get('source') 
knit_hooks$set(source = function(x, options) { 
    txt = hook_source(x, options) 
    # extend the default source hook 
    gsub('~', '\\\\mytilde', txt) 
}) 
@ 
<<results = "hide">>= 
lm(Sepal.Width ~ Sepal.Length, data = iris) 
@ 
\end{document} 

는 또한 일반적으로 사용하기 위해 \mytilde 명령을 정의합니다. 예를 들어, R 코드의 인라인 예 : "in the form \texttt{response~\mytilde~predictors} ...".

패키지 xspace은 (newcommand에서 xspace을 제거하는 한) 엄격하게 필요한 것은 아니지만 명령을 더 멋지게 만듭니다.

+0

에코 코드에 적합하지만 물결표는 결과에 여전히 "못 생겼습니다". –