| 1 | + | %% |
| 2 | + | %% This is file `mcode.sty' |
| 3 | + | %% |
| 4 | + | %% It is supposed to help you easily include MATLAB source code |
| 5 | + | %% into LaTeX document, but have it nicely highlighted, using |
| 6 | + | %% the great listings package. |
| 7 | + | %% |
| 8 | + | %% PLEASE NOTE that this package does nothing but save you from |
| 9 | + | %% figuring out some configurations in setting up the LISTINGS |
| 10 | + | %% package. ALL the work is done by that package! Thus, please |
| 11 | + | %% refer your questions to the listings package documentation. |
| 12 | + | %% |
| 13 | + | %% Usage: You have three ways of including your MATLAB code. As |
| 14 | + | %% environment, as inline object and directly from an external |
| 15 | + | %% file. |
| 16 | + | %% |
| 17 | + | %% 1) Environment: |
| 18 | + | %% |
| 19 | + | %% \begin{lstlisting} |
| 20 | + | %% YOUR CODE HERE |
| 21 | + | %% \end{lstlisting} |
| 22 | + | %% |
| 23 | + | %% |
| 24 | + | %% 2) Inline object: |
| 25 | + | %% |
| 26 | + | %% Bla bla \mcode{CODEFRAGMENT} bla bla. |
| 27 | + | %% |
| 28 | + | %% |
| 29 | + | %% 3) Include external file (in environment form) |
| 30 | + | %% |
| 31 | + | %% \lstinputlisting{YOUR-FILE.m} |
| 32 | + | %% |
| 33 | + | %% |
| 34 | + | %% For your convenience this package has the following options: |
| 35 | + | %% |
| 36 | + | %% - bw if you intend to print the document (highlighting done |
| 37 | + | %% via text formatting (bold, italic) and shades of gray) |
| 38 | + | %% |
| 39 | + | %% - numbered if you want line numbers |
| 40 | + | %% |
| 41 | + | %% - autolinebreaks if you want the package to automatically |
| 42 | + | %% wrap your code. This is buggy as it may well break |
| 43 | + | %% break syntax and it doesn't work well with comments. |
| 44 | + | %% You REALLY should wrap your code manually. |
| 45 | + | %% |
| 46 | + | %% - useliterate if you want some characters / relations in |
| 47 | + | %% your code to be replace with something more readable. |
| 48 | + | %% Example: ~= becomes $\neq$, >= becomes $\geq$, delta |
| 49 | + | %% becomes $\delta$ and so on. |
| 50 | + | %% |
| 51 | + | %% - framed if you want a frame around the source code blocks |
| 52 | + | %% |
| 53 | + | %% - final if you have ``gloablly'' set the draft option, the |
| 54 | + | %% listings package will not output the code at all. to |
| 55 | + | %% force it to do so anyway, load this package with the |
| 56 | + | %% final option (passes the ``final'' on to listings). |
| 57 | + | %% |
| 58 | + | %% For example, you may use \usepackage[numbered,framed]{mcode} |
| 59 | + | %% in your document preamble. |
| 60 | + | %% |
| 61 | + | %% Note: Inside code blocks you can escape to LaTeX text mode |
| 62 | + | %% using §...§. For ex. §text and some math: $x^2$§, which is |
| 63 | + | %% especially useful in comments for putting nicely typeset |
| 64 | + | %% equations etc. To get the same colour/style as in the rest |
| 65 | + | %% of the comment use \mcommentfont, i.e. §\mcommentfont $x^2$§ |
| 66 | + | %% |
| 67 | + | %% To change the font used, edit the first line in the "custo- |
| 68 | + | %% mise below" section. And feel free to edit other things as |
| 69 | + | %% well. Refer to the documentation of the listings package to |
| 70 | + | %% see what else you could do. If an extra small font is re- |
| 71 | + | %% quired, use {\fontfamily{pcr}\fontsize{3}{4.6}\selectfont} |
| 72 | + | %% in the definition of \lstbasicfont. |
| 73 | + | %% |
| 74 | + | %% Author: |
| 75 | + | %% Florian Knorn | [email protected] | www.florian-knorn.com |
| 76 | + | %% |
| 77 | + | %% Version history: |
| 78 | + | %% 2.3 -- More keywords (thanks Dominik Wild!) |
| 79 | + | %% 2.2 -- Bugfix (thanks Willi Gerbig!) |
| 80 | + | %% 2.1 -- Finally automatic detection between end and end |
| 81 | + | %% 2.0 -- New options for line breaking and literate prog. |
| 82 | + | %% 1.8 -- Fixed typo in documentation regarding §...§ |
| 83 | + | %% 1.7 -- Added MATLAB block comment syntax %{ ...... %} |
| 84 | + | %% 1.6 -- Added some infos, dealing with keyword ``end'' |
| 85 | + | %% 1.5 -- Tweaked check to see wether textcomp is loaded |
| 86 | + | %% 1.4 -- Fixed misconfig (mathescape now set to false) |
| 87 | + | %% 1.3 -- Purely cosmetic (tabs replaced by spaces) |
| 88 | + | %% 1.2 -- Added \lstset{showstringspaces=false} |
| 89 | + | %% 1.1 -- Added \mcode command and [final] option |
| 90 | + | %% 1.0 -- Release |
| 91 | + | |
| 92 | + | |
| 93 | + | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 94 | + | % D O N ' T T O U C H T H I S % |
| 95 | + | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 96 | + | \def\fileversion{2.3} |
| 97 | + | \def\filedate{2012/08/31} |
| 98 | + | |
| 99 | + | \typeout{-- Package: `mcode' \fileversion\space <\filedate> --} |
| 100 | + | \NeedsTeXFormat{LaTeX2e} |
| 101 | + | \ProvidesPackage{mcode}[\filedate\space\fileversion] |
| 102 | + | |
| 103 | + | % for bw-option |
| 104 | + | \newif\ifbw |
| 105 | + | \DeclareOption{bw}{\bwtrue} |
| 106 | + | |
| 107 | + | % numbered option |
| 108 | + | \newif\ifnumbered |
| 109 | + | \DeclareOption{numbered}{\numberedtrue} |
| 110 | + | |
| 111 | + | % final option |
| 112 | + | \newif\iffinal |
| 113 | + | \DeclareOption{final}{\finaltrue} |
| 114 | + | |
| 115 | + | % autolinebreaks option |
| 116 | + | \newif\ifautolinebreaks |
| 117 | + | \DeclareOption{autolinebreaks}{\autolinebreakstrue} |
| 118 | + | |
| 119 | + | % literate programming (replace certain characters/relations |
| 120 | + | \newif\ifuseliterate |
| 121 | + | \DeclareOption{useliterate}{\useliteratetrue} |
| 122 | + | |
| 123 | + | % framed option |
| 124 | + | \newif\ifframed |
| 125 | + | \DeclareOption{framed}{\framedtrue} |
| 126 | + | |
| 127 | + | \DeclareOption*{% default |
| 128 | + | \PackageWarning{mcode}{Unknown option `\CurrentOption' !}% |
| 129 | + | } |
| 130 | + | \ProcessOptions |
| 131 | + | |
| 132 | + | \ifbw\typeout{ - settings optimized for printing (bw formating)} |
| 133 | + | \else\typeout{ - settings optimized for display (colour formating)}\fi |
| 134 | + | \ifnumbered\typeout{ - line numbering enabled}\else\fi |
| 135 | + | \ifuseliterate\typeout{ - literate programming (character replacements) enabled}\else\fi |
| 136 | + | \ifautolinebreaks\typeout{ - automatic line breaking enabled (careful, buggy!)}\else\fi |
| 137 | + | \ifframed\typeout{ - framed listings}\else\fi |
| 138 | + | |
| 139 | + | % This command allows you to typeset syntax highlighted Matlab |
| 140 | + | % code ``inline''. The font size \small seems to look best... |
| 141 | + | \newcommand{\mcode}[1]{\lstinline[basicstyle=\lstbasicfont\small]|#1|} |
| 142 | + | |
| 143 | + | % check if color command exists |
| 144 | + | \ifx\color\undefined% |
| 145 | + | \RequirePackage{xcolor}% |
| 146 | + | \fi |
| 147 | + | |
| 148 | + | % check if listings has been loaded |
| 149 | + | \ifx\lstset\undefined% |
| 150 | + | \iffinal |
| 151 | + | \RequirePackage[final]{listings} |
| 152 | + | \else |
| 153 | + | \RequirePackage{listings} |
| 154 | + | \fi |
| 155 | + | \fi |
| 156 | + | |
| 157 | + | % Check if textcomp has been loaded (this package is needed for |
| 158 | + | % upright quotes '' (instead of typographic ones `´)... |
| 159 | + | \ifx\textquotesingle\undefined% |
| 160 | + | \RequirePackage{textcomp}% |
| 161 | + | \fi |
| 162 | + | |
| 163 | + | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 164 | + | % C U S T O M I S E B E L O W % |
| 165 | + | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 166 | + | |
| 167 | + | % --------------------------------------------------------------------------------- |
| 168 | + | % default font |
| 169 | + | \def\lstbasicfont{\fontfamily{pcr}\selectfont\footnotesize} |
| 170 | + | |
| 171 | + | % --------------------------------------------------------------------------------- |
| 172 | + | % matlat languate definition |
| 173 | + | \lstdefinelanguage{matlabfloz}{% |
| 174 | + | alsoletter={...},% |
| 175 | + | morekeywords={% % keywords |
| 176 | + | break,case,catch,continue,elseif,else,end,% |
| 177 | + | for,function,global,if,otherwise,persistent,% |
| 178 | + | return,switch,try,while,methods,properties,% |
| 179 | + | events,classdef,...},% |
| 180 | + | comment=[l]\%, % comments |
| 181 | + | morecomment=[l]..., % comments |
| 182 | + | morecomment=[s]{\%\{}{\%\}}, % block comments |
| 183 | + | morestring=[m]' % strings |
| 184 | + | }[keywords,comments,strings]% |
| 185 | + | |
| 186 | + | % --------------------------------------------------------------------------------- |
| 187 | + | % general definitions |
| 188 | + | \lstset{% |
| 189 | + | basicstyle={\lstbasicfont}, % set font |
| 190 | + | showstringspaces=false, % do not emphasize spaces in strings |
| 191 | + | tabsize=4, % number of spaces of a TAB |
| 192 | + | mathescape=false,escapechar=§, % escape to latex with §...§ |
| 193 | + | upquote=true, % upright quotes |
| 194 | + | aboveskip={1.5\baselineskip}, % a bit of space above listings |
| 195 | + | columns=fixed % nice spacing |
| 196 | + | } |
| 197 | + | |
| 198 | + | % --------------------------------------------------------------------------------- |
| 199 | + | % define colours and styles |
| 200 | + | \ifbw % use font formating and gray 'colors' |
| 201 | + | \def\mcommentfont{\color[gray]{.75}\itshape} %comments light gray and italic |
| 202 | + | \lstset{language=matlabfloz, % use our version of highlighting |
| 203 | + | keywordstyle=\bfseries, % keywords in bold |
| 204 | + | commentstyle=\mcommentfont, % comments |
| 205 | + | stringstyle=\color[gray]{0.5} % strings darker gray |
| 206 | + | } |
| 207 | + | \else% notbw => use colors : ) |
| 208 | + | \def\mcommentfont{\color[rgb]{.133,.545,.133}} %comments in green |
| 209 | + | \lstset{language=matlabfloz, % use our version of highlighting |
| 210 | + | keywordstyle=\color[rgb]{0,0,1}, % keywords in blue |
| 211 | + | commentstyle=\mcommentfont, % comments |
| 212 | + | stringstyle=\color[rgb]{.627,.126,.941} % strings in purple |
| 213 | + | } |
| 214 | + | \fi%bw |
| 215 | + | |
| 216 | + | % --------------------------------------------------------------------------------- |
| 217 | + | % automatic line breaking --- warning, this is buggy and |
| 218 | + | % doesn't break comments correctly! |
| 219 | + | \ifautolinebreaks |
| 220 | + | \newsavebox{\lbreakdots}\sbox{\lbreakdots}{\lstbasicfont\mcommentfont...} |
| 221 | + | \lstset{breaklines=true,breakatwhitespace=true,prebreak=\usebox{\lbreakdots}} |
| 222 | + | \fi |
| 223 | + | |
| 224 | + | % --------------------------------------------------------------------------------- |
| 225 | + | % literate replacements |
| 226 | + | % the following is for replacing some matlab relations like >= or ~= |
| 227 | + | % by the corresponding LaTeX symbols, which are much easier to read ... |
| 228 | + | \ifuseliterate |
| 229 | + | \lstset{% |
| 230 | + | literate=% |
| 231 | + | {~}{{$\neg$}}1 % \neg |
| 232 | + | {<=}{{\tiny$\leq$}}1 % \leq |
| 233 | + | {>=}{{\tiny$\geq$}}1 % \geq |
| 234 | + | {~=}{{\tiny$\neq$}}1 % \neq |
| 235 | + | {delta}{{\tiny$\Delta$}}1 % \Delta |
| 236 | + | {(end)}{\lstbasicfont (end)}{5} % black ``end'' when indexing last vector element |
| 237 | + | {({ }end)}{\lstbasicfont ({ }end)}{6} |
| 238 | + | {(end{ })}{\lstbasicfont (end{ })}{6} |
| 239 | + | {({ }end{ })}{\lstbasicfont ({ }end{ })}{7} |
| 240 | + | {:end}{\lstbasicfont :end}{4} |
| 241 | + | {:{ }end}{\lstbasicfont :{ }end}{5} |
| 242 | + | {end:}{\lstbasicfont end:}{4} |
| 243 | + | {end{ }:}{\lstbasicfont end{ }:}{5} |
| 244 | + | {,end}{\lstbasicfont ,end}{4} |
| 245 | + | {,{ }end}{\lstbasicfont ,{ }end}{5} |
| 246 | + | } |
| 247 | + | \else |
| 248 | + | \lstset{% |
| 249 | + | literate=% |
| 250 | + | {(end)}{\lstbasicfont (end)}{5} % black ``end'' when indexing last vector element |
| 251 | + | {({ }end)}{\lstbasicfont ({ }end)}{6} |
| 252 | + | {(end{ })}{\lstbasicfont (end{ })}{6} |
| 253 | + | {({ }end{ })}{\lstbasicfont ({ }end{ })}{7} |
| 254 | + | {:end}{\lstbasicfont :end}{4} |
| 255 | + | {:{ }end}{\lstbasicfont :{ }end}{5} |
| 256 | + | {end:}{\lstbasicfont end:}{4} |
| 257 | + | {end{ }:}{\lstbasicfont end{ }:}{5} |
| 258 | + | {,end}{\lstbasicfont ,end}{4} |
| 259 | + | {,{ }end}{\lstbasicfont ,{ }end}{5} |
| 260 | + | } |
| 261 | + | \fi%literates |
| 262 | + | |
| 263 | + | % --------------------------------------------------------------------------------- |
| 264 | + | % line numbering |
| 265 | + | \ifnumbered% numbered option |
| 266 | + | \lstset{% |
| 267 | + | numbersep=3mm, numbers=left, numberstyle=\tiny, % number style |
| 268 | + | } |
| 269 | + | \fi |
| 270 | + | |
| 271 | + | \ifframed% framed option |
| 272 | + | \lstset{% |
| 273 | + | frame=single, % frame |
| 274 | + | } |
| 275 | + | \ifnumbered% |
| 276 | + | \lstset{% |
| 277 | + | framexleftmargin=6mm, xleftmargin=6mm % tweak margins |
| 278 | + | } |
| 279 | + | \fi |
| 280 | + | \fi |
| 281 | + | |
| 282 | + | \endinput |
| 283 | + | %% End of file `mcode.sty'. |