4

I am currently trying to implement my Matlab-Code into TeX. Weird enough, when programming a minimum working example, everything works pretty fine. However, when I use my real document, I only get errors. Here the MWE:

\documentclass{article}
\usepackage[numbered]{mcode}
\definecolor{lightgray}{rgb}{0.9,0.9,0.9}
\renewcommand{\lstlistingname}{Quellcode}
\lstset{backgroundcolor=\color{lightgray},
        linewidth=15.5cm,}
\begin{document}
\lstinputlisting[label={hans}, caption=Funktion {\ttfamily setminInt}]
                {"E:/Program Files/MATLAB/projects/master/classes/Customer.m"}
\end{document}

Here's the preamble of the original document (although heavily shortened):

\documentclass[a4paper,12pt,parskip,bibtotoc,liststotoc]{article}

\usepackage{fontenc}
\usepackage[ngerman, english]{babel}   
\usepackage{setspace}      %fr Zeilenabstand
\usepackage[notindex,nottoc]{tocbibind}  %Inhaltsverzeichnisse erstellen

%zustzliche bentigte Pakete
\usepackage{graphicx}      %Graphik
\usepackage{amsmath,amssymb}  %Mathematik
\usepackage{natbib}       %Zitate
\usepackage{marvosym}      %enthlt Symbole wie das Eurozeichen
\usepackage{eurosym}
%\setcounter{secnumdepth}{3}
%\setcounter{tocdepth}{3}

\usepackage[numbered]{mcode}
\definecolor{lightgray}{rgb}{0.9,0.9,0.9}
\renewcommand{\lstlistingname}{Source Code}
\lstset{backgroundcolor=\color{lightgray},
        linewidth=15.5cm,}

\setlength{\parskip}{3pt} % 1ex plus 0.5ex minus 0.2ex}


\usepackage{mdwlist} 
\usepackage[labelsep=space,justification=centering]{caption}

%\usepackage{hyperref} %erlaubt Links innerhalb des pdf-Dokuments zu erzeugen

\setlength{\parindent}{0pt}   %Verhinderung des horizontalen Einrckens zu Beginn eines Absatzes

%Seitenlayout
\topmargin -0.9cm    %Vertikaler Abstand der Kopfzeile von der Bezugslinie
\textheight 25cm    %Abstand der Grundlinie der Kopfzeile zum Haupttext
\textwidth 16.5cm    %Breite des Haupttexts
\footskip 1cm      %Abstand der Grundlinien der letzten Textzeile und der Fuzeile
\voffset -0.5cm     %Vertikale Bezugspunktposition
\hoffset -1.2cm     %Horizontale Bezugspunktposition

\onehalfspacing     %anderthalbzeiliger Abstand

\newcommand{\url}{\;}  %URL im Literaturverzeichnis

\begin{document}
\lstinputlisting[caption=Funktion {\ttfamily setminInt}]
                    {"E:/Program Files/MATLAB/projects/master/classes/Customer.m"}
\end{document}

I always get the error: ("E:\Program Files (x86)\MiKTeX 2.9\tex\latex\psnfss\ot1pcr.fd")) ! Incomplete \iffalse; all text was ignored after line 560.

So miktex ignores everything that's written in the customer class (the code starts at line 560)

  • This is not an answer to your question, but you should have a look at the matlab-prettifier package; it improves upon mcode in many ways. See this answer, for instance. Also, contrary to mcode, it's available in MiKTeX. – jub0bs Sep 6 '14 at 14:32
3

The problem is the ngerman option to babel as this turns on shorthands. The simplest solution is to turn them off via:

\usepackage[shorthands=off,ngerman,english]{babel}

Otherwise you can turn off shorthands around the listing input command via:

\begingroup
\shorthandoff{"}
\lstinputlisting[caption=Funktion {\ttfamily setminInt}] {"file.m"}
\endgroup

In your full example with external file.m containing

nth

this gives:

Sample output

\documentclass[a4paper,12pt,parskip,bibtotoc,liststotoc]{article}

\usepackage{fontenc}
\usepackage[main=ngerman,english]{babel}   
\usepackage{setspace}      %fr Zeilenabstand
\usepackage[notindex,nottoc]{tocbibind}  %Inhaltsverzeichnisse erstellen

%zustzliche bentigte Pakete
\usepackage{graphicx}      %Graphik
\usepackage{amsmath,amssymb}  %Mathematik
\usepackage{natbib}       %Zitate
\usepackage{marvosym}      %enthlt Symbole wie das Eurozeichen
\usepackage{eurosym}
%\setcounter{secnumdepth}{3}
%\setcounter{tocdepth}{3}

\usepackage[numbered]{mcode}
\definecolor{lightgray}{rgb}{0.9,0.9,0.9}
\renewcommand{\lstlistingname}{Source Code}
\lstset{backgroundcolor=\color{lightgray},
        linewidth=15.5cm,}

\setlength{\parskip}{3pt} % 1ex plus 0.5ex minus 0.2ex}


\usepackage{mdwlist} 
\usepackage[labelsep=space,justification=centering]{caption}

%\usepackage{hyperref} %erlaubt Links innerhalb des pdf-Dokuments zu erzeugen

\setlength{\parindent}{0pt}   %Verhinderung des horizontalen Einrckens zu Beginn eines Absatzes

%Seitenlayout
\topmargin -0.9cm    %Vertikaler Abstand der Kopfzeile von der Bezugslinie
\textheight 25cm    %Abstand der Grundlinie der Kopfzeile zum Haupttext
\textwidth 16.5cm    %Breite des Haupttexts
\footskip 1cm      %Abstand der Grundlinien der letzten Textzeile und der Fuzeile
\voffset -0.5cm     %Vertikale Bezugspunktposition
\hoffset -1.2cm     %Horizontale Bezugspunktposition

\onehalfspacing     %anderthalbzeiliger Abstand

\newcommand{\url}{\;}  %URL im Literaturverzeichnis

\begin{document}
xx"ayy

\begingroup
\shorthandoff{"}
\lstinputlisting[caption=Funktion {\ttfamily setminInt}] {"file.m"}
\endgroup

xx"ayy
\end{document}
  • Oh my, it worked! You saved the day, thx :) – Atlan Sep 6 '14 at 14:48
  • Was about to, but I had a chat with my flatmate – Atlan Sep 6 '14 at 14:55
2

Just add somewhere after loading listings the magic code

\makeatletter
\g@addto@macro\lst@setcatcodes{\catcode`\"=12 }
\makeatother

and you won't need any special trick. This is a safe measure; unfortunately there's no interface for it and it would be nice if there was. However, this has the consequence that babel shorthands cannot be used in the optional argument. If you need them a different solution is required.

\documentclass[a4paper]{article}

\usepackage[T1]{fontenc}
\usepackage[main=ngerman,english]{babel}   

\usepackage[numbered]{mcode}
\definecolor{lightgray}{rgb}{0.9,0.9,0.9}
\renewcommand{\lstlistingname}{Source Code}
\lstset{backgroundcolor=\color{lightgray},
        linewidth=15.5cm,}

\usepackage{mdwlist} 
\usepackage[labelsep=space,justification=centering]{caption}

\makeatletter
\renewcommand{\lstinputlisting}{%
  \begingroup\lst@setcatcodes\Xlst@listinputlisting
}
\newcommand{\Xlst@listinputlisting}[1][]{%
  \endgroup\def\lst@set{#1}%
  % make " other just for absorbing the next argument
  \begingroup\@makeother\"\Xlst@listinputlisting@i
}
\newcommand{\Xlst@listinputlisting@i}[1]{%
  \endgroup
  \IfFileExists{\lst@inputpath#1}
    {\expandafter\lst@InputListing\expandafter{\lst@inputpath#1}}%
    {\filename@parse{\lst@inputpath#1}%
     \edef\reserved@a{\noexpand\lst@MissingFileError{\filename@area\filename@base}%
       {\ifx\filename@ext\relax tex\else\filename@ext\fi}}%
     \reserved@a}%
  \lst@doendpe\@newlistfalse\ignorespaces
}
\makeatother

\begin{document}
xx"ayy

\lstinputlisting[caption=F\"unktion {\ttfamily setminInt}] {"juliaset.m"}

xx"ayy
\end{document}

The idea is to separate absorbing of the optional argument and the mandatory one. The " shorthand is removed just for grabbing the argument, just like \lst@setcatcodes is valid only when absorbing the optional argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.