Search:   Help

Navigation

Groups

LyX documentation

Edit

Shared groups

Links

MatlabToLyx

Categories: tools
<< | Page list | >>

Goal--Insert Matlab code into LyX

Insert Matlab code into LyX with keywords, comments, etc. in different colors/fonts

Solution

The attached zip file contains a function called matlab2tex.m Execute this function in Matlab giving the full path to an m-file (of course, use the actual path on your system) matlab2tex('C:\MatlabFiles\Lyx\matlab2tex.m'); this creates a TeX file, C:\MatlabFiles\Lyx\matlab2tex.tex

In your LyX document, paste the following into the Preamble
   \usepackage{alltt}
   \usepackage{color}
   \definecolor{string}{rgb}{0.7,0.0,0.0}
   \definecolor{comment}{rgb}{0.13,0.54,0.13}
   \definecolor{keyword}{rgb}{0.0,0.0,1.0}

In LyX, open an ERT box

 Click in the box and do Insert|file|plain TeX and browse to the TeX file 

This will insert the TeX code for the Matlab source

Files

download the zip file at http://lyx.475766.n2.nabble.com/file/n2865240/matlab2tex.zip matlab2tex.zip (or at ftp.lyx.org).

This contains the Matlab code and a sample LyX document and a ps of the result.

Installation

Extract the files in the zip and extract the included highlight.zip into a location on your Matlab path

Example

See the example in the zip file.

See Also

The function uses highlight.m from the Mathlab file exchange

History

Comments, Bugs

Leave this field for future bugs or comments for your script

Hi! When i try this tool i get this result:

>> matlab2tex('my_file.m')
??? Error using ==> fprintf
Invalid file identifier.  Use fopen to generate a valid file
identifier.

Error in ==> highlight>write_highlighted_code at 126
	fprintf(outfid,out_format.pre_start);

Error in ==> highlight at 110
write_highlighted_code(mfid,outfid,opt)

Error in ==> matlab2tex at 57
highlight(mfile,opt,fid);

Any ideas what this could mean? I'm using Matlab R2010a for linux on Ubuntu 10.10!!!Copyright


Hi

I am using Windows 7 under VirtualBox on debian testing and I found the same problem. In my case, when the function fileparts called by matlab2tex was executed, it returned a void string for pathstr and the variable texname which points to the output file becomes '/my_file.tex' instead of 'my_file.tex'.

To fix it, I replaced the lines :

pathfull = [pathstr filesep];

mfile_name_with_path = [pathfull name]; % no extension

mfile_name = [name ext];

    % create a file pointer as input to highlight

texname = [pathfull name '.tex'];

by the line :

texname = fullfile(pathstr,[name '.tex' versn]);


Open source. Use for any application.

tools


Select Insert > Program Listing. Then type the program. To get colors and control many aspects of the listing format, go to Document > Settings... > LaTeX Preamble and copy there something like:

\usepackage{color}
\definecolor{hellgelb}{rgb}{1,1,0.85}
\definecolor{colKeys}{rgb}{0,0,1}
\definecolor{colIdentifier}{rgb}{0,0,0}
\definecolor{colComments}{rgb}{1,0,0}
\definecolor{colString}{rgb}{0,0.5,0}
\lstset{%
    language=Matlab,%
    float=hbp,%
    basicstyle=\footnotesize\ttfamily,%
    identifierstyle=\color{colIdentifier},%
    keywordstyle=\color{colKeys},%
    stringstyle=\color{colString},%
    commentstyle=\itshape\color{colComments},%
    columns=fixed,
    tabsize=4,%
    frame=single,%
    framerule=1pt,
    extendedchars=true,%
    showspaces=false,%
    showstringspaces=false,%
    numbers=left,%
    numberstyle=\tiny\ttfamily,%
    numbersep=1em,%
    breaklines=true,%
    breakindent=10pt,%
    backgroundcolor=\color{hellgelb},%
    breakautoindent=true,%
    captionpos=t,%
    xleftmargin=1em,%
    xrightmargin=\fboxsep%

}

Next go in Lyx to Insert --> File --> Subdocument. Type:Program Listing You're done.

Edit - History - Print - Recent Changes - All Recent Changes - Search
Page last modified on 2013-06-18 21:44 UTC