-
-
Save eyliu/120689 to your computer and use it in GitHub Desktop.
% LaTeX settings for MATLAB code listings | |
% based on Ted Pavlic's settings in http://links.tedpavlic.com/ascii/homework_new_tex.ascii | |
\usepackage{listings} | |
\usepackage[usenames,dvipsnames]{color} | |
% This is the color used for MATLAB comments below | |
\definecolor{MyDarkGreen}{rgb}{0.0,0.4,0.0} | |
% For faster processing, load Matlab syntax for listings | |
\lstloadlanguages{Matlab}% | |
\lstset{language=Matlab, % Use MATLAB | |
frame=single, % Single frame around code | |
basicstyle=\small\ttfamily, % Use small true type font | |
keywordstyle=[1]\color{Blue}\bfseries, % MATLAB functions bold and blue | |
keywordstyle=[2]\color{Purple}, % MATLAB function arguments purple | |
keywordstyle=[3]\color{Blue}\underbar, % User functions underlined and blue | |
identifierstyle=, % Nothing special about identifiers | |
% Comments small dark green courier | |
commentstyle=\usefont{T1}{pcr}{m}{sl}\color{MyDarkGreen}\small, | |
stringstyle=\color{Purple}, % Strings are purple | |
showstringspaces=false, % Don't put marks in string spaces | |
tabsize=5, % 5 spaces per tab | |
% | |
%%% Put standard MATLAB functions not included in the default | |
%%% language here | |
morekeywords={xlim,ylim,var,alpha,factorial,poissrnd,normpdf,normcdf}, | |
% | |
%%% Put MATLAB function parameters here | |
morekeywords=[2]{on, off, interp}, | |
% | |
%%% Put user defined functions here | |
morekeywords=[3]{FindESS, homework_example}, | |
% | |
morecomment=[l][\color{Blue}]{...}, % Line continuation (...) like blue comment | |
numbers=left, % Line numbers on left | |
firstnumber=1, % Line numbers start with line 1 | |
numberstyle=\tiny\color{Blue}, % Line numbers are blue | |
stepnumber=5 % Line numbers go in steps of 5 | |
} | |
% Includes a MATLAB script. | |
% The first parameter is the label, which also is the name of the script | |
% without the .m. | |
% The second parameter is the optional caption. | |
\newcommand{\matlabscript}[2] | |
{\begin{itemize}\item[]\lstinputlisting[caption=#2,label=#1]{#1.m}\end{itemize}} |
That's wonderful!! Thanks a lot!!
To help if someone need it:
Adding to tgiorgetti's reply, you should call \matlabscript{}{}
The first arg {code_file_name} without .m
The second arg {caption} that is optional, will show a caption above the code.
You can change que caption "listing" name:
\renewcommand{\lstlistingname}{Code}% Listing -> Code
and position (from line 46) adding ",captionpos=b":
{\begin{itemize}\item[]\lstinputlisting[caption=#2,label=#1,captionpos=b]{./codes/#1.m}\end{itemize}}
I hope it could be helpful (2).
Thank you!
I'm using it with the beamer class. I had an issue with the color package:
LaTeX Error: Option clash for package color. [...] The package color has already been loaded with options: []
Removing \usepackage[usenames,dvipsnames]{color}
from the preamble and setting the document option xcolor={usenames,dvipsnames}
fixed the error.
I report just in case somebody else runs into the same issue.
Thanks for the gist :)
Friend, you must copy/paste the 46 lines above in your LateX code, before the beginning of your "\begin{document}" statement.
Pay Attention on line 46. You must define de location of repository of MatLab scripts, so I made a little modification in a parameter for my necessity, changing "{#1.m}" for "{./MATLAB_code/#1.m}", to put all scripts in a organized place.
Inside the document, you can put the MatLab code like this:
\matlabscript{Name_Of_Your_MatLab_Script}
So, the "Name_Of_Your_MatLab_Script", is like this:
% --------- Initial definitions
clc
clear
And.. works with .m or .mlx But in the parameter of /matlabscript{} don't need the extension, only the name.
I hope it could be helpful.