You do not want to mess up, right? When writing a LaTeX document, you may once in a while want to include some Matlab codes and/or outputs (preferably typeset using typewriter font if you have the same taste as me) during the course of your writing. What I used to do was to copy and paste the Matlab codes into my LaTeX file, execute the codes in Matlab, then do another copy and paste to place the results in my LaTeX file, and finally decorate them in a verbatim block or something like that. Guess what, Matlab provides a command, called publish
, that helps you do all these in a simpler way.
In a nutshell, the way to use publish
is to first type in the texts (as your usually LaTeX editing), including Matlab codes, in a single .m
file. Let’s say the file name is example.m
. Then, in Matlab, you issue the command
publish('example.m', struct('format','latex','outputDir','ltx-src'));
It means that you want Matlab to process example.m
and output a LaTeX file example.tex
(that you can compile to get pdf) in the sub-directory ltx-src
. This is it. Instead of writing example.tex
, you write a file example.m
.
So, how should I write example.m
? It is best to give an example. See the following:
%% % <latex> % The eigenvalues of a circulant matrix can be % obtained by performing FFT on the first column % of the matrix. First, let us construct a % $5times5$ circulant matrix verb|C| whose first % column verb|c| is generated with random input: % </latex>
c = rand(5,1); % sad that Matlab does not provide a circulant() % command... C = toeplitz(c, c([1 end:-1:2]))
%% % <latex> % The eigenvalues of verb|C| are nothing but % </latex>
lambda = fft(c)
%% % <latex> % Check it out! The output is the same as using % the verb|eig| command: % </latex>
eig(C)
%% % Fun, isn't it?
It is nothing but a script file that Matlab can execute, right? The trick part is that all the texts and LaTeX markups are buried in comment blocks. How the Matlab command publish
makes a LaTeX output is that whenever it meets a whole block of comments starting with ‘%%
‘, it strips the comment signs and decorates the whole block using the pair begin{par}
and end{par}
. On the other hand, whenever it meets a block of codes that does not start with ‘%%
‘, Matlab knows that they are executable commands. Matlab uses begin{verbatim}
and end{verbatim}
to typeset these command texts, and automatically add the Matlab outputs of the commands, which are also decorated by the begin{verbatim}
and end{verbatim}
pair, in the LaTeX file. Something I am not satisfied is that Matlab does not recognize LaTeX commands such as verb||
. I have to put <latex></latex>
so that Matlab can do a verbatim copy of verb||
, instead of expand the text verb||
in some weird way, in the output LaTeX file.
It is time to try the above example yourself. Have fun.
Note: The copyright belongs to the blog author and the blog. For the license, please see the linked original source blog.
Leave a Reply
You must be logged in to post a comment.