Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mcode.sty 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. %% * If you want to place some inline code in a footnote, use
  62. %% \mcodefn{} instead (this will reduce the font size a bit).
  63. %%
  64. %% Note: Inside code blocks you can escape to LaTeX text mode
  65. %% using §...§. For ex. §text and some math: $x^2$§, which is
  66. %% especially useful in comments for putting nicely typeset
  67. %% equations etc. To get the same colour/style as in the rest
  68. %% of the comment use \mcommentfont, i.e. §\mcommentfont $x^2$§
  69. %%
  70. %% To change the font used, edit the first line in the "custo-
  71. %% mise below" section. And feel free to edit other things as
  72. %% well. Refer to the documentation of the listings package to
  73. %% see what else you could do. If an extra small font is re-
  74. %% quired, use {\fontfamily{pcr}\fontsize{3}{4.6}\selectfont}
  75. %% in the definition of \lstbasicfont.
  76. %%
  77. %% Author:
  78. %% Florian Knorn | florian@knorn.org | www.florian-knorn.com
  79. %%
  80. %% Version history:
  81. %% 2.7 -- Bugfixes + keywords (thanks Hildo Guillardi Jr.)
  82. %% 2.6 -- Add support for µ, fix for math-minus problem
  83. %% 2.5 -- Renamed internal variables (thx S. Kranenbarg!)
  84. %% 2.4 -- Added \mcodefn{} command (thx Tony Almeida!)
  85. %% 2.3 -- More keywords (thx Dominik Wild!)
  86. %% 2.2 -- Bugfix (thx Willi Gerbig!)
  87. %% 2.1 -- Finally automatic detection between end and end
  88. %% 2.0 -- New options for line breaking and literate prog.
  89. %% 1.8 -- Fixed typo in documentation regarding §...§
  90. %% 1.7 -- Added MATLAB block comment syntax %{ ...... %}
  91. %% 1.6 -- Added some infos, dealing with keyword ``end''
  92. %% 1.5 -- Tweaked check to see wether textcomp is loaded
  93. %% 1.4 -- Fixed misconfig (mathescape now set to false)
  94. %% 1.3 -- Purely cosmetic (tabs replaced by spaces)
  95. %% 1.2 -- Added \lstset{showstringspaces=false}
  96. %% 1.1 -- Added \mcode command and [final] option
  97. %% 1.0 -- Release
  98. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  99. % D O N ' T T O U C H T H I S %
  100. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  101. \def\fileversion{2.7}
  102. \def\filedate{2015/11/11}
  103. \typeout{-- Package: `mcode' \fileversion\space <\filedate> --}
  104. \NeedsTeXFormat{LaTeX2e}
  105. \ProvidesPackage{mcode}[\filedate\space\fileversion]
  106. % for bw-option
  107. \newif\ifmcode@bw
  108. \DeclareOption{bw}{\mcode@bwtrue}
  109. % numbered option
  110. \newif\ifmcode@numbered
  111. \DeclareOption{numbered}{\mcode@numberedtrue}
  112. % final option
  113. \newif\ifmcode@final
  114. \DeclareOption{final}{\mcode@finaltrue}
  115. % autolinebreaks option
  116. \newif\ifmcode@autolinebreaks
  117. \DeclareOption{autolinebreaks}{\mcode@autolinebreakstrue}
  118. % literate programming (replace certain characters/relations
  119. \newif\ifmcode@useliterate
  120. \DeclareOption{useliterate}{\mcode@useliteratetrue}
  121. % framed option
  122. \newif\ifmcode@framed
  123. \DeclareOption{framed}{\mcode@framedtrue}
  124. \DeclareOption*{% default
  125. \PackageWarning{mcode}{Unknown option `\CurrentOption' !}%
  126. }
  127. \ProcessOptions
  128. \ifmcode@bw\typeout{ - settings optimized for printing (bw formating)}
  129. \else\typeout{ - settings optimized for display (colour formating)}\fi
  130. \ifmcode@numbered\typeout{ - line numbering enabled}\else\fi
  131. \ifmcode@useliterate\typeout{ - literate programming (character replacements) enabled}\else\fi
  132. \ifmcode@autolinebreaks\typeout{ - automatic line breaking enabled (careful, buggy!)}\else\fi
  133. \ifmcode@framed\typeout{ - framed listings}\else\fi
  134. % This command allows you to typeset syntax highlighted Matlab
  135. % code ``inline''. The font size \small seems to look best...
  136. \newcommand{\mcode}[1]{\lstinline[basicstyle=\lstbasicfont\small]|#1|}
  137. % Same, but for footnotes
  138. \newcommand{\mcodefn}[1]{\lstinline[basicstyle=\lstbasicfont\footnotesize]|#1|}
  139. % check if color command exists
  140. \ifx\color\undefined%
  141. \RequirePackage{xcolor}%
  142. \fi
  143. % check if listings has been loaded
  144. \ifx\lstset\undefined%
  145. \ifmcode@final
  146. \RequirePackage[final]{listings}
  147. \else
  148. \RequirePackage{listings}
  149. \fi
  150. \fi
  151. % Check if textcomp has been loaded (this package is needed for
  152. % upright quotes '' (instead of typographic ones `´)...
  153. \ifx\textquotesingle\undefined%
  154. \RequirePackage{textcomp}%
  155. \fi
  156. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  157. % C U S T O M I S E B E L O W %
  158. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  159. % ---------------------------------------------------------------------------------
  160. % default font
  161. \def\lstbasicfont{\fontfamily{pcr}\selectfont\footnotesize}
  162. % ---------------------------------------------------------------------------------
  163. % matlat languate definition
  164. \lstdefinelanguage{matlabfloz}{%
  165. alsoletter={...},%
  166. morekeywords={% % keywords
  167. break,case,catch,classdef,continue,else,
  168. elseif,end,for,function,global,if,
  169. otherwise,parfor,persistent,
  170. return,spmd,switch,try,while,...}, % Use the matlab "iskeyword" command to get those
  171. comment=[l]\%, % comments
  172. morecomment=[l]..., % comments
  173. morecomment=[s]{\%\{}{\%\}}, % block comments
  174. morestring=[m]' % strings
  175. }[keywords,comments,strings]%
  176. % ---------------------------------------------------------------------------------
  177. % general definitions
  178. \lstset{%
  179. basicstyle={\lstbasicfont}, % set font
  180. showstringspaces=false, % do not emphasize spaces in strings
  181. tabsize=4, % number of spaces of a TAB
  182. mathescape=false,escapechar=§, % escape to latex with §...§
  183. upquote=true, % upright quotes
  184. aboveskip={1.5\baselineskip}, % a bit of space above listings
  185. columns=fixed % nice spacing
  186. }
  187. % ---------------------------------------------------------------------------------
  188. % define colours and styles
  189. \ifmcode@bw % use font formating and gray 'colors'
  190. \def\mcommentfont{\color[gray]{.75}\itshape} %comments light gray and italic
  191. \lstset{language=matlabfloz, % use our version of highlighting
  192. keywordstyle=\bfseries, % keywords in bold
  193. commentstyle=\mcommentfont, % comments
  194. stringstyle=\color[gray]{0.5} % strings darker gray
  195. }
  196. \else% notbw => use colors : )
  197. \def\mcommentfont{\color[rgb]{.133,.545,.133}} %comments in green
  198. \lstset{language=matlabfloz, % use our version of highlighting
  199. keywordstyle=\color[rgb]{0,0,1}, % keywords in blue
  200. commentstyle=\mcommentfont, % comments
  201. stringstyle=\color[rgb]{.627,.126,.941} % strings in purple
  202. }
  203. \fi%bw
  204. % ---------------------------------------------------------------------------------
  205. % automatic line breaking --- warning, this is buggy and
  206. % doesn't break comments correctly!
  207. \ifmcode@autolinebreaks
  208. \newsavebox{\lbreakdots}\sbox{\lbreakdots}{\lstbasicfont\mcommentfont...}
  209. \lstset{breaklines=true,breakatwhitespace=true,prebreak=\usebox{\lbreakdots}}
  210. \fi
  211. % ---------------------------------------------------------------------------------
  212. % literate replacements
  213. % the following is for replacing some matlab relations like >= or ~=
  214. % by the corresponding LaTeX symbols, which are much easier to read ...
  215. \ifmcode@useliterate
  216. \lstset{%
  217. literate=%
  218. {~}{{$\neg$}}1 % \neg, logical not
  219. {<=}{{\tiny$\leq$}}1 % \leq
  220. {>=}{{\tiny$\geq$}}1 % \geq
  221. {~=}{{\tiny$\neq$}}1 % \neq, not equal
  222. {delta}{{\tiny$\Delta$}}1 % \Delta
  223. {µ}{{$\mu$}}1 % \mu
  224. {(end)}{\lstbasicfont (end)}{5} % black ``end'' when indexing last vector element
  225. {({ }end)}{\lstbasicfont ({ }end)}{6}
  226. {(end{ })}{\lstbasicfont (end{ })}{6}
  227. {({ }end{ })}{\lstbasicfont ({ }end{ })}{7}
  228. {:end}{\lstbasicfont :end}{4}
  229. {:{ }end}{\lstbasicfont :{ }end}{5}
  230. {end:}{\lstbasicfont end:}{4}
  231. {end{ }:}{\lstbasicfont end{ }:}{5}
  232. {,end}{\lstbasicfont ,end}{4}
  233. {,{ }end}{\lstbasicfont ,{ }end}{5}
  234. }
  235. \else
  236. \lstset{%
  237. literate=%
  238. {(end)}{\lstbasicfont (end)}{5} % black ``end'' when indexing last vector element
  239. {({ }end)}{\lstbasicfont ({ }end)}{6}
  240. {(end{ })}{\lstbasicfont (end{ })}{6}
  241. {({ }end{ })}{\lstbasicfont ({ }end{ })}{7}
  242. {:end}{\lstbasicfont :end}{4}
  243. {:{ }end}{\lstbasicfont :{ }end}{5}
  244. {end:}{\lstbasicfont end:}{4}
  245. {end{ }:}{\lstbasicfont end{ }:}{5}
  246. {,end}{\lstbasicfont ,end}{4}
  247. {,{ }end}{\lstbasicfont ,{ }end}{5}
  248. {µ}{$\mu$}1
  249. {~}{{\fontfamily{ptm}\selectfont\texttildelow}}1 % get a nicer tilde character
  250. }
  251. \fi%literates
  252. % ---------------------------------------------------------------------------------
  253. % line numbering
  254. \ifmcode@numbered% numbered option
  255. \lstset{%
  256. numbersep=3mm, numbers=left, numberstyle=\tiny, % number style
  257. }
  258. \fi
  259. \ifmcode@framed% framed option
  260. \lstset{%
  261. frame=single,rulecolor=\color{black} % frame
  262. }
  263. \ifmcode@numbered%
  264. \lstset{%
  265. framexleftmargin=6mm, xleftmargin=6mm % tweak margins
  266. }
  267. \fi
  268. \fi
  269. % fix for ``minus'' character issue, as suggested by Stefan Karlsson, thanks!
  270. \makeatletter
  271. \lst@CCPutMacro\lst@ProcessOther {"2D}{\lst@ttfamily{-{}}{-{}}}
  272. \@empty\z@\@empty
  273. \makeatother
  274. \endinput
  275. %% End of file `mcode.sty'.