%%1%% This is file `mcode.sty'2%%3%% It is supposed to help you easily include MATLAB source code4%% into LaTeX document, but have it nicely highlighted, using5%% the great listings package.6%%7%% For a demo document, please see8%% http://www.knorn.org/misc_files/mcode_demo.zip9%%10%% PLEASE NOTE that this package does nothing but save you from11%% figuring out some configurations in setting up the listings12%% package. ALL the work is done by that package! Thus, refer13%% your questions to the listings package documentation.14%%15%% Usage: You have three ways of including your MATLAB code. As16%% environment, as inline object and directly from an external17%% file.18%%19%% 1) Environment:20%%21%% \begin{lstlisting}22%% YOUR CODE HERE23%% \end{lstlisting}24%%25%%26%% 2) Inline object:27%%28%% Bla bla \mcode{CODEFRAGMENT} bla bla.29%%30%%31%% 3) Include external file (in environment form)32%%33%% \lstinputlisting{YOUR-FILE.m}34%%35%%36%% For your convenience this package has the following options:37%%38%% - bw if you intend to print the document (highlighting done39%% via text formatting (bold, italic) and shades of gray)40%%41%% - numbered if you want line numbers42%%43%% - framed if you want a frame around the source code blocks44%%45%% - final if you have ``gloablly'' set the draft option, the46%% listings package will not output the code at all. to47%% force it to do so anyway, load this package with the48%% final option (passes the ``final'' on to listings).49%%50%% For example, you may use \usepackage[numbered,framed]{mcode}51%% in your document preamble.52%%53%% Note: Inside code blocks you can escape to LaTeX text mode54%% using §...§. For ex. §text and some math: $x^2$§, which is55%% especially useful in comments for putting nicely typeset56%% equations etc. To get the same colour/style as in the rest57%% of the comment use \mcommentfont, i.e. §\mcommentfont $x^2$§58%%59%% A nice feature of the listings package is that you can re-60%% place certain strings by LaTeX strings; this is used for61%% some relation symbols, see below...62%%63%% To change the font used, edit the first line in the "custo-64%% mise below" section. And feel free to edit other things as65%% well. Refer to the documentation of the listings package to66%% see what else you could do. If an extra small font is re-67%% quired, use {\fontfamily{pcr}\fontsize{3}{4.6}\selectfont}68%% in the definition of \lstbasicfont.69%%70%% Author: Florian Knorn | [email protected] | www.florian-knorn.com71%%72%% Version history:73%% 1.8 -- Fixed typo in documentation regarding §...§74%% 1.7 -- Added MATLAB block comment syntax %{ ...... %}75%% 1.6 -- Added some infos, dealing with keyword ``end''76%% 1.5 -- Tweaked check to see wether textcomp is loaded77%% 1.4 -- Fixed misconfig (mathescape now set to false)78%% 1.3 -- Purely cosmetic (tabs replaced by spaces)79%% 1.2 -- Added \lstset{showstringspaces=false}80%% 1.1 -- Added \mcode command and [final] option81%% 1.0 -- Release828384%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%85% D O N ' T T O U C H T H I S %86%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%87\def\fileversion{1.7}88\def\filedate{2010/02/01}8990\typeout{-- Package: `mcode' \fileversion\space <\filedate> --}91\NeedsTeXFormat{LaTeX2e}92\ProvidesPackage{mcode}[\filedate\space\fileversion]9394% for bw-option95\newif\ifbw96\DeclareOption{bw}{\bwtrue}9798% numbered option99\newif\ifnumbered100\DeclareOption{numbered}{\numberedtrue}101102% final option103\newif\iffinal104\DeclareOption{final}{\finaltrue}105106% for framed option107\newif\ifframed108\DeclareOption{framed}{\framedtrue}109110\DeclareOption*{% default111\PackageWarning{mcode}{Unknown option `\CurrentOption' !}%112}113\ProcessOptions114115\ifbw\typeout{ - settings optimized for printing (bw formating)}116\else\typeout{ - settings optimized for display (colour formating)}\fi117\ifnumbered\typeout{ - line numbering enabled}\else\fi118\ifframed\typeout{ - framed listings}\else\fi119120% This command allows you to typeset syntax highlighted Matlab121% code ``inline''. The font size \small seems to look best...122\newcommand{\mcode}[1]{\lstinline[basicstyle=\lstbasicfont\small]|#1|}123124% check if color command exists125\ifx\color\undefined%126\RequirePackage{xcolor}%127\fi128129% check if listings has been loaded130\ifx\lstset\undefined%131\iffinal132\RequirePackage[final]{listings}133\else134\RequirePackage{listings}135\fi136\fi137138% Check if textcomp has been loaded (this package is needed for139% upright quotes '' (instead of typographic ones `´)...140\ifx\textquotesingle\undefined%141\RequirePackage{textcomp}%142\fi143144%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%145% C U S T O M I S E B E L O W %146%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%147148% define the wanted font for all highlightings here149\def\lstbasicfont{\fontfamily{pcr}\selectfont\footnotesize}150151% now let's define our own version of matlab highlighting152\lstdefinelanguage{matlabfloz}{%153alsoletter={...},%154morekeywords={% % keywords155break,case,catch,continue,elseif,else,end,for,function,global,%156if,otherwise,persistent,return,switch,try,while,...},%157comment=[l]\%,% % comments158morecomment=[l]...,% % comments159morecomment=[s]{\%\{}{\%\}}, % block comments160morestring=[m]',% % strings161}[keywords,comments,strings]%162163% define the colours164165\ifbw % use font formating and gray 'colors'166\def\mcommentfont{\color[gray]{.75}\itshape} %comments light gray and italic167\lstset{language=matlabfloz, % use our version of highlighting168keywordstyle=\bfseries, % keywords in bold169commentstyle=\mcommentfont, % comments170stringstyle=\color[gray]{0.5} % strings darker gray171}172\else% notbw => use colors : )173\def\mcommentfont{\color[rgb]{.133,.545,.133}} %comments in green174\lstset{language=matlabfloz, % use our version of highlighting175keywordstyle=\color[rgb]{0,0,1}, % keywords in blue176commentstyle=\mcommentfont, % comments177stringstyle=\color[rgb]{.627,.126,.941} % strings in purple178}179\fi%bw180181\lstset{%182basicstyle={\lstbasicfont}, % set font183showstringspaces=false, % do not emphasize spaces in strings184tabsize=4, % number of spaces of a TAB185mathescape=false,escapechar=§, % escape to latex with §...§186upquote=true, % upright quotes187aboveskip={1.5\baselineskip}, % a bit of space above listings188columns=fixed, % nice spacing189%190% the following is for replacing some matlab relations like >= or ~=191% by the corresponding LaTeX symbols, which are much easier to read ...192literate=%193{~}{{$\neg$}}1 % \neg194{<=}{{\tiny$\leq$}}1 % \leq195{>=}{{\tiny$\geq$}}1 % \geq196{~=}{{\tiny$\neq$}}1 % \neq197{delta}{{\tiny$\Delta$}}1% \Delta198{iend}{{\fontfamily{pcr}\selectfont end}}3% end when indexing matrix elements199}200201\ifnumbered% numbered option202\lstset{%203numbersep=3mm, numbers=left, numberstyle=\tiny, % number style204}205\fi206207\ifframed% framed option208\lstset{%209frame=single, % frame210}211\ifnumbered%212\lstset{%213framexleftmargin=6mm, xleftmargin=6mm % tweak margins214}215\fi216\fi217218\endinput219%% End of file `mcode.sty'.220221