如何在 Latex 文件中编写 R 示例

问题描述 投票:0回答:1

我正在Rstudio中编写一个乳胶文件,需要为我自己开发的R包提供一个R示例。我注意到发表的论文通常具有如下所示的示例格式,但我不确定他们使用什么函数来生成这种样式,例如 R>...我们如何生成这样的东西?

enter image description here

r latex r-package
1个回答
0
投票

我不确定您使用的是哪个操作系统;这是一个帮助指导方法的示例。

[![\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{parskip}  % Added to remove paragraph indentation
\usepackage{hyperref}  % Added for hyperlinks

% Define colors
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{linkcolor}{rgb}{0,0,0.8}  % Dark blue for links

% Hyperref setup
\hypersetup{
    colorlinks=true,
    linkcolor=linkcolor,
    urlcolor=linkcolor,
    pdftitle={Formatting Solution for 79010592},
    pdfauthor={Your Name}
}

% Console-style R code
\lstdefinestyle{RConsoleStyle}{
    language=R,
    basicstyle=\ttfamily\small,
    backgroundcolor=\color{white},
    commentstyle=\color{codegreen},
    keywordstyle=\color{blue},
    stringstyle=\color{codepurple},
    breaklines=true,
    breakatwhitespace=true,
    showstringspaces=false,
    numbers=none,
    stepnumber=1,
    numberstyle=\tiny\color{codegray},
    tabsize=2,
    keepspaces=true,
    escapeinside={(*@}{@*)},
    literate=*{>}{{\textcolor{codegray}{>}}}{1}
}

\title{Formatting Solution for \href{https://stackoverflow.com/questions/79010592/how-to-write-an-r-example-in-latex-file}{79010592}}
\author{Technophobe01}
\date{\today}

\begin{document}

\maketitle

\noindent Here is an example of R code formatted in a console-style using the listings package:

\begin{lstlisting}\[style=RConsoleStyle\]
(*@\textcolor{codegray}{R>}@*) library("simexaft")
\end{lstlisting}

Next, load the data that are properly organized with the variable names specified. In the example here, the data set named as BHS is included by issuing

\begin{lstlisting}\[style=RConsoleStyle\]
(*@\textcolor{codegray}{R>}@*) data("BHS")
(*@\textcolor{codegray}{R>}@*) dataset <- BHS
(*@\textcolor{codegray}{R>}@*) dataset$SBP <- log(dataset$SBP - 50)
\end{lstlisting}

\end{document}][1]][1]

这是输出:

© www.soinside.com 2019 - 2024. All rights reserved.