0

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.

3
  • 1
    Did you try \begin{lstlisting}[language=R]? Or do you have any special requirements for the font and syntax highlighting, if so, which requirements?
    – Marijn
    Jan 30, 2021 at 11:44
  • 1
    By default 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})?
    – moewe
    Jan 30, 2021 at 11:53
  • @Marijin, the problem is solved for me now as I used this explanation :cs.overleaf.com/learn/latex/Code_listing
    – Tou Mou
    Jan 30, 2021 at 15:03

1 Answer 1

0

The problem is solved with this solution :

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .