This article will show you how to add Matlab code to the Latex typesetting process.
Functional effects
Mainly has the following typesetting function:
- Syntax highlighting
- Automatically add borders
- Add line numbers automatically
First, let's feel the effect.
listings
Package
First make sure you can use the listings
package
Simple and quick way to use the following
\usepackage{listings}\lstset{ language=matlab}\begin{lstlisting}% Plot function Span class= "Hljs-title" >f (x ) = 2 *x ^3 -x -2 ezplot ( ' 2*x^3-x-2 ' , [0 , 2 ]) hold on plot ([0 , 2 ],[ 0 , 0 ], ' R ' ) \< Span class= "hljs-function" >end {lstlisting }
Let's take a look at the effect:
Next use mcode
style
mcode
Package
Mcode
There are three main methods of use
Inserting code blocks
\usepackage{listings}\usepackage[framed,numbered, autolinebreaks,useliterate]{mcode}\begin{lstlisting}% Plot function f (x ) = 2 *x ^3 -x -2 Ezplot ( Span class= "hljs-string" > ' 2*x^3-x-2 ' , [0 , 2 ]) hold on plot ([ 0 , 2 ],[0 , Span class= "Hljs-number" >0 ], ' R ' ) \end {lstlisting }
Let's take a look at the effect:
The comparison found just a simple addition to the following sentence code
\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}
Inserting Matlab statements in a paragraph
Simply insert the following statement in the paragraph or footer edit
\mcode{for i=1:3, disp(‘cool‘end;}in footnotes: \mcodefn{for i=1:3, disp(‘cool‘end;}}
Even with \mcode{}
The effect is as follows
Embed .m
files
See the references below for more details
Reference documents
Listings Help Documentation
Mcode Help Documentation
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Inserting Matlab code in Latex