0

My MWE :

\documentclass[12pt,a4paper]{report}
\usepackage[lmargin=3.81cm,tmargin=2.54cm,rmargin=2.54cm,bmargin=2.52cm]{geometry}
\linespread{1.5}
\usepackage{listings}
\usepackage{color}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
  language=Matlab,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=left,
  numberstyle=\tiny\color{black},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true
  tabsize=3
}

\begin{document}

\begin{lstlisting}
 e=1;e2=1;e3=1 %%control variable for ON mode
\end{lstlisting}
\end{document}

I have a Python code that I want to add, but the language set currently is only for matlab. How can I define it for two languages ?

Razor
  • 2,691

1 Answers1

2

You can set this when you input the file, e.g.:

\lstinputlisting[language=Matlab]{matlabfile.m}

\lstinputlisting[language=python]{pythonfile.py}

For more information visit the package documentation

Mario S. E.
  • 18,609
  • But im copy-pasting the code into my tex file, would this still work ? – Razor Apr 26 '13 at 16:15
  • If you are copying just part of your code, wouldn't the verbatim environment be more useful to you? – Mario S. E. Apr 26 '13 at 16:17
  • Actually the method you proposed here is much better and more organised, i'll use this instead :D thanks ! – Razor Apr 26 '13 at 16:17
  • The main advantage of using this is that don't have to update the code in LaTeX if you make any changes to the file. The argument of the lstinputlistings command points directly to the file, so any change made to it will reflect in your pdf output. – Mario S. E. Apr 26 '13 at 16:33
  • Yeah, thanks alot for that ... Regarding the settings in preamble (following \lstset{frame=tb,), are these still needed though ? – Razor Apr 26 '13 at 18:18
  • 1
    well, the command \lstset is for the settings that are going to affect both files, so "frame" there is affecting both the matlab and the python file – Mario S. E. Apr 26 '13 at 18:24
  • Ahh now I understand this better. Thank you very much. – Razor Apr 26 '13 at 18:25