在 LaTeX 下,我想在文本之间添加以下 R 代码。我正在寻找合适的字体。
我已经尝试过了:
\documentclass[notitlepage]{article}
\usepackage{amsmath, amssymb}
\usepackage{pdflscape}
\usepackage[left=0.3in,right=0.3in,top=0.3in,bottom=0.5in]{geometry}
\usepackage{colortbl}
\usepackage[margin=1cm]{caption}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{float}
\usepackage{graphicx}
\graphicspath{{figures/}}
\usepackage{longtable}
%\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{pdfpages}
\usepackage[ ruled,vlined]{algorithm2e}
\usepackage{ifoddpage}
\usepackage{blindtext}
\usepackage{authblk}
\usepackage{listings}
\begin{document}
\begin{lstlisting}
library("rootSolve")
grad_descent<-function(objFun ,iter = 100, alpha = 0.001 , start_init ){
# define the objective function f(x)
# iter is the number of iterations to try
# alpha is the step parameter
# define the gradient of f(x)
# Note we don't split up the gradient
init = start_init #initial point search
gradient_1 <- function(init , objFun) {
result <- gradient(objFun, init,pert = 1e-8) # vector of gradient / partial derivatives
return(result)
}
x <- init
# create a vector to contain all xs for all steps
x.All = numeric(iter)
# gradient descent method to find the minimum
for(i in seq_len(iter)){
# Guard against NaNs
tmp <- c(x) - alpha * gradient_1(x , objFun)
if ( !is.nan(suppressWarnings(objFun(tmp))) ) {
x <- tmp
}
print(c(i, x,objFun(x))) # we print the current iteration with corresponding objective function value
}
# print result and plot all xs for every iteration
print(paste("The minimum of f(x) is ", objFun(x), " at position x = ", x, sep = ""))
plot(x.All, type = "l")
}
}
\end{lstlisting}
\end{document}
我期待更好的主题/R 代码字体。
答案1
该问题通过以下解决方案得到解决:
\documentclass[notitlepage]{article}
\usepackage{amsmath, amssymb}
\usepackage{pdflscape}
\usepackage[left=0.3in,right=0.3in,top=0.3in,bottom=0.5in]{geometry}
\usepackage{colortbl}
\usepackage[margin=1cm]{caption}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{float}
\usepackage{graphicx}
\graphicspath{{figures/}}
\usepackage{longtable}
%\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{pdfpages}
\usepackage[ ruled,vlined]{algorithm2e}
\usepackage{ifoddpage}
\usepackage{blindtext}
\usepackage{authblk}
\usepackage{listings}
\usepackage{xcolor}
\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{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle}{
backgroundcolor=\color{backcolour},
commentstyle=\color{codegreen},
keywordstyle=\color{magenta},
numberstyle=\tiny\color{codegray},
stringstyle=\color{codepurple},
basicstyle=\ttfamily\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2
}
\begin{document}
\lstset{style=mystyle}
\begin{lstlisting}[language=R , caption=Example of used R vanilla GD code]
library("rootSolve")
grad_descent<-function(objFun ,iter = 100, alpha = 0.001 , start_init ){
# define the objective function f(x)
# iter is the number of iterations to try
# alpha is the step parameter
# define the gradient of f(x)
# Note we don't split up the gradient
init = start_init #initial point search
gradient_1 <- function(init , objFun) {
result <- gradient(objFun, init,pert = 1e-8) # vector of gradient / partial derivatives
return(result)
}
x <- init
# create a vector to contain all xs for all steps
x.All = numeric(iter)
# gradient descent method to find the minimum
for(i in seq_len(iter)){
# Guard against NaNs
tmp <- c(x) - alpha * gradient_1(x , objFun)
if ( !is.nan(suppressWarnings(objFun(tmp))) ) {
x <- tmp
}
print(c(i, x,objFun(x))) # we print the current iteration with corresponding objective function value
}
# print result and plot all xs for every iteration
print(paste("The minimum of f(x) is ", objFun(x), " at position x = ", x, sep = ""))
plot(x.All, type = "l")
}
}
\end{lstlisting}
\end{document}