knit_hooks和knit_theme似乎没有很好地互相发挥

时间:2017-01-26 15:03:56

标签: r knitr

我正在尝试使用knitr来包装文本并使用语法高亮显示。我可以让一个或另一个工作,但不是两个。我该如何解决这个问题?

这是一个例子,两者之间的唯一区别是knit_hooks $ set的变化来自:

knit_hooks$set(par=function(before, options, envir){if (before) par(mar=c(4,4,.1,.1),cex.lab=.95,cex.axis=.9,mgp=c(2,.7,0),tcl=-.3)})

knit_hooks$set(source=listing, output=listing)

更新:据我所知,这是因为我正在使用\ begin {lstlisting}来处理代码块,因此我对knit_theme所做的任何更改都无效(它们只会更改背景颜色,而不是文本本身),我需要改为使用\ lstdefinestyle并手动设置颜色,这不是完全我正在寻找的解决方案。

语法突出显示

\documentclass{article}
\title{Combining knittheme with knithooks}
\author{author}

\usepackage{listings}
\usepackage{inconsolata}

<<echo=FALSE>>=
    library(knitr)
    thm <- knit_theme$get("solarized-dark")
    options(width=60)
    listing <- function(x, options) {
        paste("\\begin{lstlisting}[basicstyle=\\ttfamily,breaklines=true]\n", x, "\\end{lstlisting}\n", sep = "")
    }

    #knit_hooks$set(source=listing, output=listing)
    knit_hooks$set(par=function(before, options, envir){if (before) par(mar=c(4,4,.1,.1),cex.lab=.95,cex.axis=.9,mgp=c(2,.7,0),tcl=-.3)})

    knit_theme$set(thm)
@

\begin{document}
\maketitle
<<Test>>=
    years    = 1996:2010 #Check Syntax highlighting
    years[1]+5 #Check that code wraps to fit in between the page margins and doesn't go on and on until you can't read it anymore.
@
\end{document}

Syntax highlighting works but the text doesn't wrap.

包装文字

\documentclass{article}
\title{Combining knittheme with knithooks}
\author{author}

\usepackage{listings}
\usepackage{inconsolata}

<<echo=FALSE>>=
    library(knitr)
    thm <- knit_theme$get("solarized-dark")
    options(width=60)
    listing <- function(x, options) {
        paste("\\begin{lstlisting}[basicstyle=\\ttfamily,breaklines=true]\n", x, "\\end{lstlisting}\n", sep = "")
    }

    knit_hooks$set(source=listing, output=listing)
    #knit_hooks$set(par=function(before, options, envir){if (before) par(mar=c(4,4,.1,.1),cex.lab=.95,cex.axis=.9,mgp=c(2,.7,0),tcl=-.3)})

    knit_theme$set(thm)
@

\begin{document}
\maketitle
<<Test>>=
    years    = 1996:2010 #Check Syntax highlighting
    years[1]+5 #Check that code wraps to fit in between the page margins and doesn't go on and on until you can't read it anymore.
@
\end{document}

Text Wrapping works but syntax highlighting fails

0 个答案:

没有答案