Math & ScienceListings problem when including code

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
thorpn
Posts: 53
Joined: Mon Jul 30, 2012 7:30 pm

Listings problem when including code

Post by thorpn »

I am using the listings package to include R code in my report, however because i am doing a matrix multiplication, which in R is done with %*% i get 100 errors !

How can i handle this problem ?

Code: Select all

\begin{lstlisting}
fejl <- rnorm(antal_obs_total,0,1) + B %*% beta_endo
\end{lstlisting}
Minimal example

Code: Select all

\documentclass[12pt,a4paper,oneside]{report}
\usepackage[top=2.0cm,left=1.5 cm,right=3.5 cm,bottom=2.0cm,headheight=11.0 pt]{geometry}		%Sætter lille top, bund og venstreside. Samt større højreside(så læser kan indsætte noter i margin)
\usepackage[ansinew]{inputenc}
\usepackage{graphicx}
\usepackage{amssymb,amsmath}
\usepackage[compact]{titlesec}												%Bruges til at fjerne white space omkring overskrifter(chapters/sections etc.)
\usepackage{tabularx}
\usepackage{multicol}
\usepackage{amsmath, amsthm, amssymb}									%Stor matematik pakke
\usepackage{ulem} 																		%lettere adgang til over og underline
\usepackage{listings,relsize} 																%Gør kode pænere
\usepackage{textcomp} 																%gør at man kan skrive nye symboler, bla. '
\usepackage{wrapfig}																	%Gør at figure kan wrappes
\usepackage{color}																		%Gør at tekst osv. kan farves
\usepackage[bf]{caption}						 									%gGør navnet på captions(fx. "Figur 5.1"') til fed skrift
\usepackage{enumerate}																%Gør at man kan sætte tal/bogstaver som indeks på lister
\usepackage[nottoc,numbib]{tocbibind}									%Sørger for at bibliografien er med i table of kontents
%\usepackage{pslatex}																	%Sætter skrift typen til Times new roman (langt grimmere end standard LaTex skrift)
%\usepackage{harvard} 																%Tillader at citer som \citeasnoun hvilket af nogen af foretrukket over \cite
\usepackage{natbib}
\usepackage[all]{xy}																	%Kan bruges til at tegne figurer såsom flowcharts
\usepackage{verbatim}

\setcounter{tocdepth}{1}															%Dybde af indholdsfortegnelsen: 1: Chapters, 2: sections, 3: subsections osv.
%\input{diverse.settings.tex}													%Nogle forskellige settings og genvejs kommandier


%Listings indstillinger defineres
\lstset{ %
language=R,                % choose the language of the code
basicstyle=\footnotesize,       % the size of the fonts that are used for the code
numbers=left,                   % where to put the line-numbers
numberstyle=\footnotesize,      % the size of the fonts that are used for the line-numbers
stepnumber=1,                   % the step between two line-numbers. If it's 1 each line 
                                % will be numbered
numbersep=5pt,                  % how far the line-numbers are from the code
backgroundcolor=\color{white},  % choose the background color. You must add \usepackage{color}
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding particular underscores
frame=single,                   % adds a frame around the code
tabsize=2,                      % sets default tabsize to 2 spaces
captionpos=b,                   % sets the caption-position to bottom
breaklines=true,                % sets automatic line breaking
breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
title=\lstname,                 % show the filename of files included with \lstinputlisting;
                                % also try caption instead of title
escapeinside={\%*}{*)},         % if you want to add a comment within your code
morekeywords={*,...}            % if you want to add more keywords to the set
}

\begin{document}
\begin{lstlisting}
fejl <- rnorm(antal_obs_total,0,1) + B %*% beta_endo
\end{lstlisting}
\end{document}
Last edited by thedreamshaper on Sat May 28, 2011 2:03 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Listings problem when including code

Post by localghost »

You should badly learn how to build a minimal example. Nevertheless I have been able to cut down the problem to a special parameter in the settings for your R listings.

Code: Select all

escapeinside={\%*}{*)},         % if you want to add a comment within your code
Commenting this makes the sample code below work (and your example, too).

Code: Select all

\documentclass[12pt,a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[top=2.0cm,left=1.5cm,right=3.5cm,bottom=2.0cm,headheight=11.0pt]{geometry}
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}

\lstset{ %
language=R,                % choose the language of the code
basicstyle=\footnotesize,       % the size of the fonts that are used for the code
numbers=left,                   % where to put the line-numbers
numberstyle=\footnotesize,      % the size of the fonts that are used for the line-numbers
stepnumber=1,                   % the step between two line-numbers. If it's 1 each line
                                % will be numbered
numbersep=5pt,                  % how far the line-numbers are from the code
backgroundcolor=\color{white},  % choose the background color. You must add \usepackage{color}
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding particular underscores
frame=single,                   % adds a frame around the code
tabsize=2,                      % sets default tabsize to 2 spaces
captionpos=b,                   % sets the caption-position to bottom
breaklines=true,                % sets automatic line breaking
breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
title=\lstname,                 % show the filename of files included with \lstinputlisting;
                                % also try caption instead of title
%escapeinside={\%*}{*)},         % if you want to add a comment within your code
morekeywords={*,...}            % if you want to add more keywords to the set
}

\begin{document}
\begin{lstlisting}
fejl <- rnorm(antal_obs_total,0,1) + B %*% beta_endo
\end{lstlisting}
\end{document}

Thorsten
thorpn
Posts: 53
Joined: Mon Jul 30, 2012 7:30 pm

Re: Listings problem when including code

Post by thorpn »

Thanks works like a charm, i guess i deserved it for not propperly understanding my Listings settings :)
Post Reply