使用列表、minted 或其他颜色 R 代码匹配 knitr 主题

使用列表、minted 或其他颜色 R 代码匹配 knitr 主题

包,以及这个答案这个答案,我可以R在我的 LaTeX 文档中包含类似于以下内容的代码knitr本地生產。

但是,由于我正在将 LaTeX 文档 (.tex) 与由knitr(.Rnw 文档) 制作的文档结合起来,两者都包含一些 R 代码,所以我希望完全匹配的knitr样式。

有办法吗?就像 adoptknitr的代码定义一样?可能使用还是其他包?下面是使用

以下是我的风格手动knitr模拟,

我有什么#5

下面是用 编写的相同代码knitr

针织#2

我的问题是,是否有一种自动的方式来调整我的\lstset{}定义以匹配knitr 确切地或者我是否可以以某种方式提取风格knitr

以下是代码的使用我的风格

\documentclass[11pt, oneside]{article}   
\usepackage{geometry}
\geometry{verbose,tmargin=2.5cm,bmargin=2.5cm,lmargin=2.5cm,rmargin=2.5cm}
\usepackage{listings} % to inlude R code with \begin{lstlisting}[language=R] etc.
\usepackage[usenames,dvipsnames]{xcolor}
\definecolor{backgroundCol}{rgb}{.97, .97, .97}
\definecolor{commentstyleCol}{rgb}{0.678,0.584,0.686}
\definecolor{keywordstyleCol}{rgb}{0.737,0.353,0.396}
\definecolor{stringstyleCol}{rgb}{0.192,0.494,0.8}
\definecolor{NumCol}{rgb}{0.686,0.059,0.569}
\definecolor{basicstyleCol}{rgb}{0.345, 0.345, 0.345}       
\lstset{ 
  language=R,                     % the language of the code
  basicstyle=\small  \ttfamily \color{basicstyleCol}, % the size of the fonts that are used for the code
  %numbers=left,                   % where to put the line-numbers
%  numberstyle=\color{green},  % the style that is used for the line-numbers
  stepnumber=1,                   % the step between two line-numbers. If it is 1, each line
                                  % will be numbered
  numbersep=5pt,                  % how far the line-numbers are from the code
  backgroundcolor=\color{backgroundCol},  % choose the background color. You must add \usepackage{color}
  showspaces=false,               % show spaces adding particular underscores
  showstringspaces=false,         % underline spaces within strings
  showtabs=false,                 % show tabs within strings adding particular underscores
  %frame=single,                   % adds a frame around the code
  %rulecolor=\color{white},        % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. commens (green here))
  tabsize=2,                      % sets default tabsize to 2 spaces
  captionpos=b,                   % sets the caption-position to bottom
  breaklines=true,                % sets automatic line breaking
  breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
  keywordstyle=\color{keywordstyleCol},      % keyword style
  commentstyle=\color{commentstyleCol},   % comment style
  stringstyle=\color{stringstyleCol},      % string literal style
  literate=%
   *{0}{{{\color{NumCol}0}}}1
    {1}{{{\color{NumCol}1}}}1
    {2}{{{\color{NumCol}2}}}1
    {3}{{{\color{NumCol}3}}}1
    {4}{{{\color{NumCol}4}}}1
    {5}{{{\color{NumCol}5}}}1
    {6}{{{\color{NumCol}6}}}1
    {7}{{{\color{NumCol}7}}}1
    {8}{{{\color{NumCol}8}}}1
    {9}{{{\color{NumCol}9}}}1
} 

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursus elementum massa, vitae fermentum dui blandit in.
\begin{lstlisting}[language=R]
require(graphics)

## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## Page 9: Plant Weight Data.
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
lm.D90 <- lm(weight ~ group - 1) # omitting intercept
\end{lstlisting}
Morbi ipsum neque, auctor sit amet malesuada a, malesuada sit amet odio. Proin imperdiet ipsum ligula, at aliquet ante pretium eu. 
\end{document}  

这是文档的代码knitr.Rnw

\documentclass{article}
\usepackage[sc]{mathpazo}
\usepackage[T1]{fontenc}
\usepackage{geometry}
\geometry{verbose,tmargin=2.5cm,bmargin=2.5cm,lmargin=2.5cm,rmargin=2.5cm}
\setcounter{secnumdepth}{2}
\setcounter{tocdepth}{2}
\usepackage{url}
\usepackage[unicode=true,pdfusetitle,
 bookmarks=true,bookmarksnumbered=true,bookmarksopen=true,bookmarksopenlevel=2,
 breaklinks=false,pdfborder={0 0 1},backref=false,colorlinks=false]
 {hyperref}
\hypersetup{
 pdfstartview={XYZ null null 1}}
\usepackage{breakurl}
\begin{document}
<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
# set global chunk options
opts_chunk$set(fig.path='figure/minimal-', fig.align='center', fig.show='hold')
options(formatR.arrow=TRUE,width=90)
@
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursus elementum massa, vitae fermentum dui blandit in.
<<boring-random, eval= FALSE>>=
require(graphics)

## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## Page 9: Plant Weight Data.
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
lm.D90 <- lm(weight ~ group - 1) # omitting intercept
@
Morbi ipsum neque, auctor sit amet malesuada a, malesuada sit amet odio. Proin imperdiet ipsum ligula, at aliquet ante pretium eu. 
\end{document}

答案1

我创建了一个包,可以在 github 上找到: https://github.com/DanielBonnery/SweaveLst

SweaveLst 函数将编织您的文档并用 lstlistings 环境替换编织块。

相关内容