knitr 中的 R 代码换行和代码突出显示

knitr 中的 R 代码换行和代码突出显示

我使用了@Sharpie 在 2012 年 1 月 19 日给出的精彩答案让 Sweave 代码块停留在页边距内?在 R 代码中获取换行符,但如何才能使代码突出显示(颜色)呢?

\documentclass[a4paper]{article}

\usepackage{listings}
\usepackage{inconsolata}

<<echo=FALSE>>=
  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)
@

\title{Function listings with linebreaks and code highlighting}

\begin{document}
\maketitle

Two ways of printing the code.

<<tidy=TRUE,highlight=FALSE>>=
theFunction <- function(x) {
  tmp <- "A really long string that should be line-broken but it would be nice to also see code highlighting colors. The function is in the real code sourced, but for the sake of easier reproducibility, it is written here instead."
}

theFunction
@

\end{document}

答案1

您可以直接设置您的listings文件中的设置.Rnw。这里我定义了一种新样式Rsetings

\lstdefinestyle{Rsettings}{
  basicstyle=\ttfamily,
  breaklines=true,
  showstringspaces=false,
  keywords={if, else, function, theFunction, tmp}, % Write as many keywords 
  otherkeywords={},
  commentstyle=\itshape\color{Rcommentcolor},
  keywordstyle=\color{keywordcolor},
  moredelim=[s][\color{delimcolor}]{"}{"},
}

其中颜色定义如下。

\definecolor{keywordcolor}{rgb}{0,0.6,0.6}
\definecolor{delimcolor}{rgb}{0.461,0.039,0.102}
\definecolor{Rcommentcolor}{rgb}{0.101,0.043,0.432}

关于中的键值列表的几句话\lstset

  • 之间的所有字符(包括 )"都排版为\color{delimcolor}
  • 您可以设置otherkeywordsmorekeywords如果您在实际文档中使用更多这些。(抱歉,我还不太熟悉R因为我才刚刚开始。)
  • 点击这里了解有关该包的更多信息,或者您可以在终端中listings输入。texdoc listings

有了这些,你现在可以将文件重写.Rnw

\documentclass[a4paper]{article}
\usepackage{listings}
\usepackage{inconsolata}

%-----------------
%Define the colors you want to use

\definecolor{keywordcolor}{rgb}{0,0.6,0.6}
\definecolor{delimcolor}{rgb}{0.461,0.039,0.102}
\definecolor{Rcommentcolor}{rgb}{0.101,0.043,0.432}
%-----------------
%Set up your listings. You can type `texdoc listings` in your terminal

\lstdefinestyle{Rsettings}{
  language=R,
  basicstyle=\ttfamily,
  breaklines=true,
  showstringspaces=false,
  keywords={if, else, function, theFunction, tmp},
  otherkeywords={},
  commentstyle=\itshape\color{Rcommentcolor},
  keywordstyle=\color{keywordcolor},
  moredelim=[s][\color{delimcolor}]{"}{"},
}

\title{Function listings with linebreaks and code highlighting}

\begin{document}
\maketitle

Two ways of printing the code.

<<echo=FALSE>>=
  options(width=60)

  listing <- function(x, options) {
    paste("\\begin{lstlisting}[style=Rsettings]\n",
      x, "\\end{lstlisting}\n", sep = "")
  }
  knit_hooks$set(source=listing, output=listing)
@


<<tidy=TRUE,highlight=FALSE>>=
theFunction <- function(x) {
  tmp <- "A really long string that should be line-broken but it would be nice to also see code highlighting colors. The function is in the real code sourced, but for the sake of easier reproducibility, it is written here instead."
}

theFunction
@

\end{document}

这是输出。

在此处输入图片描述

答案2

另一个选择是使用开发人员预定义的knitr-listings和/或knitr-themes内置的样式。一些示例可以在knitrknitr演示站点

这里适用于您的示例。

\documentclass[a4paper]{article}

\usepackage{listings}
\usepackage{inconsolata}

\lstset{breaklines=true,showstringspaces=false}

<<setup, include=FALSE, cache=FALSE>>=
opts_chunk$set(fig.path = 'figure/listings-')
options(replace.assign = TRUE, width=60)
render_listings()
@

\title{Function listings with linebreaks and code highlighting}

\begin{document}
\maketitle

Two ways of printing the code.

<<tidy=TRUE,highlight=FALSE>>=
theFunction <- function(x) {
  tmp <- "A really long string that should be line-broken but it would be nice to also see code highlighting colors. The function is in the real code sourced, but for the sake of easier reproducibility, it is written here instead."
}

theFunction
@

\end{document}

在此处输入图片描述

相关内容