Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96174
License: OTHER
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
%% For a demo document, please see
9
%% http://www.knorn.org/misc_files/mcode_demo.zip
10
%%
11
%% PLEASE NOTE that this package does nothing but save you from
12
%% figuring out some configurations in setting up the listings
13
%% package. ALL the work is done by that package! Thus, refer
14
%% your questions to the listings package documentation.
15
%%
16
%% Usage: You have three ways of including your MATLAB code. As
17
%% environment, as inline object and directly from an external
18
%% file.
19
%%
20
%% 1) Environment:
21
%%
22
%% \begin{lstlisting}
23
%% YOUR CODE HERE
24
%% \end{lstlisting}
25
%%
26
%%
27
%% 2) Inline object:
28
%%
29
%% Bla bla \mcode{CODEFRAGMENT} bla bla.
30
%%
31
%%
32
%% 3) Include external file (in environment form)
33
%%
34
%% \lstinputlisting{YOUR-FILE.m}
35
%%
36
%%
37
%% For your convenience this package has the following options:
38
%%
39
%% - bw if you intend to print the document (highlighting done
40
%% via text formatting (bold, italic) and shades of gray)
41
%%
42
%% - numbered if you want line numbers
43
%%
44
%% - framed if you want a frame around the source code blocks
45
%%
46
%% - final if you have ``gloablly'' set the draft option, the
47
%% listings package will not output the code at all. to
48
%% force it to do so anyway, load this package with the
49
%% final option (passes the ``final'' on to listings).
50
%%
51
%% For example, you may use \usepackage[numbered,framed]{mcode}
52
%% in your document preamble.
53
%%
54
%% Note: Inside code blocks you can escape to LaTeX text mode
55
%% using §...§. For ex. §text and some math: $x^2$§, which is
56
%% especially useful in comments for putting nicely typeset
57
%% equations etc. To get the same colour/style as in the rest
58
%% of the comment use \mcommentfont, i.e. §\mcommentfont $x^2$§
59
%%
60
%% A nice feature of the listings package is that you can re-
61
%% place certain strings by LaTeX strings; this is used for
62
%% some relation symbols, see below...
63
%%
64
%% To change the font used, edit the first line in the "custo-
65
%% mise below" section. And feel free to edit other things as
66
%% well. Refer to the documentation of the listings package to
67
%% see what else you could do. If an extra small font is re-
68
%% quired, use {\fontfamily{pcr}\fontsize{3}{4.6}\selectfont}
69
%% in the definition of \lstbasicfont.
70
%%
71
%% Author: Florian Knorn | [email protected] | www.florian-knorn.com
72
%%
73
%% Version history:
74
%% 1.8 -- Fixed typo in documentation regarding §...§
75
%% 1.7 -- Added MATLAB block comment syntax %{ ...... %}
76
%% 1.6 -- Added some infos, dealing with keyword ``end''
77
%% 1.5 -- Tweaked check to see wether textcomp is loaded
78
%% 1.4 -- Fixed misconfig (mathescape now set to false)
79
%% 1.3 -- Purely cosmetic (tabs replaced by spaces)
80
%% 1.2 -- Added \lstset{showstringspaces=false}
81
%% 1.1 -- Added \mcode command and [final] option
82
%% 1.0 -- Release
83
84
85
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86
% D O N ' T T O U C H T H I S %
87
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88
\def\fileversion{1.7}
89
\def\filedate{2010/02/01}
90
91
\typeout{-- Package: `mcode' \fileversion\space <\filedate> --}
92
\NeedsTeXFormat{LaTeX2e}
93
\ProvidesPackage{mcode}[\filedate\space\fileversion]
94
95
% for bw-option
96
\newif\ifbw
97
\DeclareOption{bw}{\bwtrue}
98
99
% numbered option
100
\newif\ifnumbered
101
\DeclareOption{numbered}{\numberedtrue}
102
103
% final option
104
\newif\iffinal
105
\DeclareOption{final}{\finaltrue}
106
107
% for framed option
108
\newif\ifframed
109
\DeclareOption{framed}{\framedtrue}
110
111
\DeclareOption*{% default
112
\PackageWarning{mcode}{Unknown option `\CurrentOption' !}%
113
}
114
\ProcessOptions
115
116
\ifbw\typeout{ - settings optimized for printing (bw formating)}
117
\else\typeout{ - settings optimized for display (colour formating)}\fi
118
\ifnumbered\typeout{ - line numbering enabled}\else\fi
119
\ifframed\typeout{ - framed listings}\else\fi
120
121
% This command allows you to typeset syntax highlighted Matlab
122
% code ``inline''. The font size \small seems to look best...
123
\newcommand{\mcode}[1]{\lstinline[basicstyle=\lstbasicfont\small]|#1|}
124
125
% check if color command exists
126
\ifx\color\undefined%
127
\RequirePackage{xcolor}%
128
\fi
129
130
% check if listings has been loaded
131
\ifx\lstset\undefined%
132
\iffinal
133
\RequirePackage[final]{listings}
134
\else
135
\RequirePackage{listings}
136
\fi
137
\fi
138
139
% Check if textcomp has been loaded (this package is needed for
140
% upright quotes '' (instead of typographic ones `´)...
141
\ifx\textquotesingle\undefined%
142
\RequirePackage{textcomp}%
143
\fi
144
145
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146
% C U S T O M I S E B E L O W %
147
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148
149
% define the wanted font for all highlightings here
150
\def\lstbasicfont{\fontfamily{pcr}\selectfont\footnotesize}
151
152
% now let's define our own version of matlab highlighting
153
\lstdefinelanguage{matlabfloz}{%
154
alsoletter={...},%
155
morekeywords={% % keywords
156
break,case,catch,continue,elseif,else,end,for,function,global,%
157
if,otherwise,persistent,return,switch,try,while,...},%
158
comment=[l]\%,% % comments
159
morecomment=[l]...,% % comments
160
morecomment=[s]{\%\{}{\%\}}, % block comments
161
morestring=[m]',% % strings
162
}[keywords,comments,strings]%
163
164
% define the colours
165
166
\ifbw % use font formating and gray 'colors'
167
\def\mcommentfont{\color[gray]{.75}\itshape} %comments light gray and italic
168
\lstset{language=matlabfloz, % use our version of highlighting
169
keywordstyle=\bfseries, % keywords in bold
170
commentstyle=\mcommentfont, % comments
171
stringstyle=\color[gray]{0.5} % strings darker gray
172
}
173
\else% notbw => use colors : )
174
\def\mcommentfont{\color[rgb]{.133,.545,.133}} %comments in green
175
\lstset{language=matlabfloz, % use our version of highlighting
176
keywordstyle=\color[rgb]{0,0,1}, % keywords in blue
177
commentstyle=\mcommentfont, % comments
178
stringstyle=\color[rgb]{.627,.126,.941} % strings in purple
179
}
180
\fi%bw
181
182
\lstset{%
183
basicstyle={\lstbasicfont}, % set font
184
showstringspaces=false, % do not emphasize spaces in strings
185
tabsize=4, % number of spaces of a TAB
186
mathescape=false,escapechar=§, % escape to latex with §...§
187
upquote=true, % upright quotes
188
aboveskip={1.5\baselineskip}, % a bit of space above listings
189
columns=fixed, % nice spacing
190
%
191
% the following is for replacing some matlab relations like >= or ~=
192
% by the corresponding LaTeX symbols, which are much easier to read ...
193
literate=%
194
{~}{{$\neg$}}1 % \neg
195
{<=}{{\tiny$\leq$}}1 % \leq
196
{>=}{{\tiny$\geq$}}1 % \geq
197
{~=}{{\tiny$\neq$}}1 % \neq
198
{delta}{{\tiny$\Delta$}}1% \Delta
199
{iend}{{\fontfamily{pcr}\selectfont end}}3% end when indexing matrix elements
200
}
201
202
\ifnumbered% numbered option
203
\lstset{%
204
numbersep=3mm, numbers=left, numberstyle=\tiny, % number style
205
}
206
\fi
207
208
\ifframed% framed option
209
\lstset{%
210
frame=single, % frame
211
}
212
\ifnumbered%
213
\lstset{%
214
framexleftmargin=6mm, xleftmargin=6mm % tweak margins
215
}
216
\fi
217
\fi
218
219
\endinput
220
%% End of file `mcode.sty'.
221