Under LaTeX, I'm wanting to add the following R code between text. I'm searching for an appropriate font .
I already tried:
\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}
I'm looking forward a better theme / R code font.
\begin{lstlisting}[language=R]
? Or do you have any special requirements for the font and syntax highlighting, if so, which requirements?listings
does not use typewriter font to typeset code, which can make things look ... unusual. Do you maybe just want\lstset{basicstyle=\ttfamily}
(add that to your preamble somewhere after\usepackage{listings}
)?