3

I have bench oh Python and R code which I like to insert into my LaTeX article. How can I include .py or .r code file ?? I did this before with Matlab like this:

\begin{center}
\begin{figure} 
\caption{come caption. \label{fig:cod1}}
\lstinputlisting{code/test.m}
\end{figure}
\end{center}

However, this time I have a two column article and I would like to break line.

1
  • I'm pretty sure you had something at the top of your file that said "listings package should use matlab". It would be helpful to have that here. You can look through the listings documentation; it explains how to pass arguments to \lstinputlisting to switch the type of syntax highlighting.
    – Teepeemm
    Commented Dec 22, 2016 at 22:54

1 Answer 1

1

Here is a sample code run. I have used packages 'listings', 'filecontents', and 'multicol'

\documentclass[12pt]{article}
\usepackage{filecontents}
\usepackage{listings}
\usepackage[margin=1in]{geometry}
\usepackage{multicol}

\begin{document}
\begin{multicols}{2}
\begin{filecontents*}{matlabcode.tex}
while (~feof(fileID))                               

   fprintf('Block: %s\n', num2str(Block))           
   InputText = textscan(fileID,'%s',2,
      'delimiter','\n');  
   HeaderLines{Block,1} = InputText{1};
   disp(HeaderLines{Block});                        

   InputText = textscan(fileID,'Num SNR = %f');     

   NumCols = InputText{1};                             

   FormatString = repmat('%f',1,NumCols);           

   InputText = textscan(fileID,FormatString, ...    
      'delimiter',',');

   Data{Block,1} = cell2mat(InputText);              
   [NumRows,NumCols] = size(Data{Block});           
   disp(cellstr(['Table data size: ' ...
      num2str(NumRows) ' x ' num2str(NumCols)]));
   disp(' ');                                          

   eob = textscan(fileID,'%s',1,
      'delimiter','\n');  
   Block = Block+1;                                
end
\end{filecontents*}

\begin{filecontents*}{pythoncode.tex}
prices = {'apple': 0.40, 'banana': 0.50}
my_purchase = {
    'apple': 1,
    'banana': 6}
grocery_bill = sum(prices[fruit] 
                 * my_purchase[fruit]
                 for fruit in my_purchase)
print 'I owe the grocer $%.2f'  grocery_bill
\end{filecontents*}%$

\begin{filecontents*}{'rcode.R'}
#utility functions

readinteger <- function()
{ 
  n <- readline(prompt="Enter an integer: ")
  if(!grepl("^[0-9]+$",n)) #$
  {
    return(readinteger())
  }
  return(as.integer(n))
}

# real program start here

num <- round(runif(1) * 100, digits = 0)
guess <- -1

cat("Guess a number between 0 and 100.\n")

while(guess != num)
{ 
  guess <- readinteger()
  if (guess == num)
  {
    cat("Congratulations,", num, "is right.\n")
  }
  else if (guess < num)
  {
    cat("It's bigger!\n")
  }
  else if(guess > num)
  {
    cat("It's smaller!\n")
  }
}
\end{filecontents*}

{\Huge matlab}
\tiny
\lstinputlisting[language=matlab]{matlabcode.tex}
{\Huge python}
\tiny
\lstinputlisting[language=Python]{pythoncode.tex}
{\Huge R}
\tiny

\lstinputlisting[language=R]{rcode.tex}

\end{multicols}
\end{document}

With the output of:

enter image description here

You must log in to answer this question.

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