5.0

5.0 | 2 ratings Rate this file 73 downloads (last 30 days) File Size: 14.49 KB File ID: #24515

m-code to LaTeX converter

by Uwe Lelke

 

22 Jun 2009 (Updated 29 Sep 2009)

Code covered by BSD License  

This m-file (with GUI) converts m-code into a tex-file for including in a LaTeX-document.

Download Now | Watch this File

File Information
Description

This file lets you very easy INCLUDE COLORED M-CODE in your LaTeX-file.

After you run this m-file "m2tex.m" and inlcude the output tex-file in your LaTeX document, you will experience NO DIFFERENCE IN THE APPEARANCE to the look of the m-code in the Matlab Editor. (except for the apostrophs)

There are SEVERAL WAYS TO EXECUTE the program.
1. Run the file by pressing F5 oder click the "Run" button in the Matlab Editor, while the m2tex.m file is open or type "m2tex" in the Command Window.
==> The program will open the GUI and the rest goes from there.
2. Call the program from the Command Window or another m-file with at least one option. The order of the options is not important. For example:

m2tex('num','C:\Programme\MATLAB\work\testfile.m','C:\latex_documents\testfile.tex')
m2tex('testfile.m','testoutputfile.tex', 'no_num','bwc')

There are ONLY TWO STEPS necessary to do to include your code:
1. Run "m2tex.m" (with the desired m-file to include).
2. Insert the code into your main LaTeX-document by adding the whole "filename.tex"-file with \include{filename} or \input{filename}.

For Example:
\usepackage{fancyvrb}
\usepackage{color}
...
\begin{singlespace} % only optional % you need \usepackage{setspace}
\footnotesize
\input{outputfile} % output from "m2tex.m", but without the extension ".tex"
\end{singlespace} % for changing your linespacing
\normalsize

Features:
 - recognizes all keywords, strings and comments
 - recognizes all indents and tabs
 - recognizes cell titles
 - writes all recognized objects correct in a tex-file
 - uses original Matlab colors
 - the font looks almost the same as in Matlab Editor
 - tex-file is saved using fontencoding "UTF-8", so that German Umlauts will be written correct
 - option for numbered code lines ('num' or 'no_num')
 - recognizes a linebreak, but only with a leading space character, i.e. " ..." and only once in a line
 - option for input- and output-file (*.m respectively *.tex-file)
 - execution via GUI or command line
-----------------------------------------------------------------
The conversion of the linebreak works only, if it is once in a line, because the command "regexprep" (code line: 1385) won't work, if you are looking for a string with three dots: '...'

--> If anyone knows why or a solution around the problem, please tell me! thx

MATLAB release MATLAB 7.5 (R2007b)
Other requirements LaTeX packages: - fancyvrb - color
Zip File Content  
Other Files license.txt,
m2tex.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
21 Jul 2009 David Gingras

Very useful and it works at the first time! I got a small error with an equation with 2 transposes in the same line (e.g. : G=P*C'/(C*P*C'+R)). The parser has interpreted the code between both transposes (') as a string. But it's minor. Good job, thanks!

30 Jul 2009 lenny

Well done. Really good work. Thanx!

A single problem I have come across:
"if, then, else" only recognized when they are in separate lines. But I'm not so sure what was the problem in my case. I changed it from
     if xx==1; do_this_fcn; else do_that_fcn; end
to
   if xx==1
      do_this_fcn;
   else
      do_that_fcn;
   end
and it worked.

For future versions:
additional input options for 'inputfilename', 'outputfilename' (which would include the directory) and which would help to automatize your script updating routines in a latex based protocol.

09 Aug 2009 Kristof

Nice work, but I still have some problems with it:
1. The transpose is still considered a string in the following case:
% .*(comp2) and .*(comp4) are colored as strings.
logical((comp1)’.*(~comp2)’.*(comp3)’.*(comp4)’.*(comp5)’);

2. When I use sprintf, then the everything after %s is colored as a string.
handles.(tag) = struct(...
’button’, eval(sprintf(’handles.%s’,tag)),...
’checkbox’, eval(sprintf(’handles.%s_check’,tag)),...
’time’, eval(sprintf(’handles.edit_%s_Time’,tag)),...
’initial’, eval(sprintf(’handles.edit_%s_Initial’,tag)),...
’final’, eval(sprintf(’handles.edit_%s_Final’,tag)),...
’gototag’, tag,...
’path’, path_steps_faults{i});

3. The gui doesn't seem to work. I don't know if anybody else had the same problem, but each button gives the same error:
>> m2tex
??? Error using ==> waitfor
Undefined function or method 'm2tex_gui_app' for input arguments of type 'struct'.
 
??? Error using ==> waitfor
Error while evaluating uicontrol Callback

I hope you can resolve these problems.

09 Aug 2009 Kristof

another problem that I've just encountered:
error(’The tag ’’%s’’ is not correct.’,boxtag);
% the ",box" is black while "tag);" is blue.

Please login to add a comment or rating.
Updates
26 Jun 2009

 (just minor things updated and a photo of the output)
more will follow...

26 Jun 2009

recognizes now a linebreak, but only with a leading space character, i.e. " ..." and only once in a line

20 Jul 2009

Corrected a minor error in respect of one single "second word" string.
Added a question dialog window for the option of numbered lines, because I always forgot to activate it in the command line.

21 Jul 2009

Upgraded the distinction between strings, e.g 'xyz' and apostrophs for transpose purpose, e.g. G=P*C'/(C*P*C'+R).

30 Jul 2009

Corrected a minor error with respect to lines, which contain only space characters cause of a kindly tip from an user.

31 Jul 2009

"if else end" can be handled now in one line, which the program was already supposed to do.
The requested input options will follow later.

06 Aug 2009

I upgraded the input options for specifying a source and target file, for example (instead of only a filename, you can write there the whole path):
m2tex('num','input.m','output.tex')

I have also added a GUI (my first one).

14 Aug 2009

I corrected all things in respect to the false strings.
But I couldn't solve the "waitfor" problem, there are now 2 m-files: the command-line tool and the GUI.
How do I put the two files together in one m-file?
Please let me to know.

29 Sep 2009

1. New option 'bwc' for code in black and white
2. The programm consists again only of one m-file

Tag Activity for this File
Tag Applied By Date/Time
latex Uwe Lelke 22 Jun 2009 12:27:09
mcode Uwe Lelke 22 Jun 2009 12:27:09
conversion Uwe Lelke 22 Jun 2009 12:27:09
tex Uwe Lelke 23 Jun 2009 08:42:03
publishing Uwe Lelke 23 Jun 2009 08:42:04
highlighting Uwe Lelke 23 Jun 2009 08:42:04
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com