8

with the package, and the help of this answer and this answer, I can include R code in my LaTeX document that looks almost like what knitr produces natively.

However, as I am combining a LaTeX document (.tex) with a document made by knitr (.Rnw document), both with some R code, I would like to exactly match knitr's style.

Is there a way to do that? Like as in adopt knitr's code definitions exactly? Possibly using or some other package? Below is an illustration of the issue using .

Here's my style emulating knitr manually,

what I have#5

and here's how the same code looks made by knitr,

knitr #2

My question is if there's an automated way to twerk my \lstset{} definitions to match knitr exactly or if I can extract the style from knitr somehow?

Here's the code use for my style,

\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}  

and here's the code for the knitr document, the .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}
5
  • 1
    It’s a fine question but why not just use knitr to produce the code excerpts?
    – Hugh
    Mar 3, 2018 at 4:57
  • Thank you for the compliment. I understand who you ask! The .tex file is quite big and I don't want to run that through knitr whenever I compile. Second, people who can barely use LaTeX has to write to that file. Finally, I like the idea of being able to produce something identical and understand how knitr does it. Apparently it's not simply using the listings hooks as the Labels command gets colored and the objects lmd9 and lm.D90 does not. To me it's also about learning how these things work.
    – Eric Fail
    Mar 3, 2018 at 10:47
  • Still at a loss I'm afraid. Running a .tex file should be trivial to do through knitr -- the size won't matter. To understand how knitr does it, you can inspect the .tex output,
    – Hugh
    Mar 4, 2018 at 10:42
  • Sorry for having tried to help you, I never do it again.
    – CarLaTeX
    Mar 8, 2018 at 17:53
  • I believe knitr uses highlight May 16, 2018 at 18:08

1 Answer 1

1

I created a package, available on github here: https://github.com/DanielBonnery/SweaveLst

The SweaveLst function will knit your document and replace the knitr chunks by lstlistings environments.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .