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 ?

3

1 Answer 1

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

7
  • But im copy-pasting the code into my tex file, would this still work ?
    – Razor
    Apr 26, 2013 at 16:15
  • If you are copying just part of your code, wouldn't the verbatim environment be more useful to you? Apr 26, 2013 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, 2013 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. Apr 26, 2013 at 16:33
  • 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 Apr 26, 2013 at 18:24

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