gpt4 book ai didi

r - 列表包中 R 代码块的颜色

转载 作者:行者123 更新时间:2023-12-03 13:31:18 24 4
gpt4 key购买 nike

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

虽然我知道我可以使用 lstset 设置颜色功能,我不太确定看起来不错的组合。因此,任何人都可以分享他们的lstset listings 的设置包为 R 语法着色?这样,看到这篇文章的每个人都可以使用相同的设置!

最佳答案

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

enter image description here

关于r - 列表包中 R 代码块的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21402157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com