首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >listings包中R代码块的颜色

listings包中R代码块的颜色
EN

Stack Overflow用户
提问于 2014-01-28 17:52:56
回答 1查看 15.3K关注 0票数 20

我希望在LaTeX文档中插入R代码块。listings包的默认设置也会更改字体,但不会更改颜色,这是我需要的。

虽然我知道我可以使用lstset函数设置颜色,但我不是很确定哪个组合看起来会很好。因此,任何人都可以分享他们的listings包的颜色R语法的lstset设置吗?这样,每个看到这篇文章的人都可以使用相同的设置!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-31 06:08:56

代码语言:javascript
复制
\documentclass[12pt]{article}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{color}    

\lstset{ 
  language=R,                     % the language of the code
  basicstyle=\tiny\ttfamily, % the size of the fonts that are used for the code
  numbers=left,                   % where to put the line-numbers
  numberstyle=\tiny\color{Blue},  % 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{white},  % 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{black},        % 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{RoyalBlue},      % keyword style
  commentstyle=\color{YellowGreen},   % comment style
  stringstyle=\color{ForestGreen}      % string literal style
} 


\begin{document}
\begin{lstlisting}
library(foreign)

foo <- rnorm(100)
# writing a function
bar <- apply(foo, 1, function(x){
  y <- sqrt(x)
  cat(paste('The result is ', x )))
})
bar

str(bar)

foo + bar
\end{lstlisting}

\end{document}

票数 26
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21402157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档