Conversion ToolsImport Matlab Code to Latex File

Information and discussion about output converters related to LaTeX (e.g. dvips, ps2pdf, ...)
Post Reply
rudrales
Posts: 16
Joined: Wed Mar 11, 2009 1:52 am

Import Matlab Code to Latex File

Post by rudrales »

Hello

I've many Matlab files and I wanna import their code into my latex file. I know it exists the function matlab2latex in Matlab but then I've to copy-paste one by one all the file code what it's a lot of work. Does a latex command exist which imports the Matlab code from a file into the Latex file?

Thanks for your help!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
ignasi
Posts: 20
Joined: Tue Jul 28, 2009 5:10 pm

Re: Import Matlab Code to Latex File

Post by ignasi »

I think you need listings package, it has an option for Matlab code and can import code from external files.

Ignasi
rudrales
Posts: 16
Joined: Wed Mar 11, 2009 1:52 am

Import Matlab Code to Latex File

Post by rudrales »

Thank you for the advice. Now, it works. I solved my problem with the following code.

Settings in the header:

Code: Select all

\usepackage{listings}
\lstset{ 
	language=Matlab,                		% choose the language of the code
%	basicstyle=10pt,       				% the size of the fonts that are used for the code
	numbers=left,                  			% where to put the line-numbers
	numberstyle=\footnotesize,      		% the size of the fonts that are used for the line-numbers
	stepnumber=1,                   			% the step between two line-numbers. If it's 1 each line will be numbered
	numbersep=5pt,                  		% how far the line-numbers are from the code
%	backgroundcolor=\color{white},  	% choose the background color. You must add \usepackage{color}
	showspaces=false,               		% show spaces adding particular underscores
	showstringspaces=false,         		% underline spaces within strings
	showtabs=false,                 			% show tabs within strings adding particular underscores
%	frame=single,	                			% adds a frame around the code
%	tabsize=2,                				% sets default tabsize to 2 spaces
%	captionpos=b,                   			% sets the caption-position to bottom
	breaklines=true,                			% sets automatic line breaking
	breakatwhitespace=false,        		% sets if automatic breaks should only happen at whitespace
	escapeinside={\%*}{*)}          		% if you want to add a comment within your code
}
Command to import the Matlab text in the LaTeX document:

Code: Select all

\lstinputlisting[language=Matlab]{file_name.m}
sveinn79
Posts: 1
Joined: Thu May 16, 2013 3:18 pm

Re: Import Matlab Code to Latex File

Post by sveinn79 »

I am currently trying to display an .m file in my report. I use the code displayed above. The content is displayed accordingly but I get an annoying error message:
inputenc: Keyboard character used is undefined(inputenc) in inputencoding `utf8'
Does anybody have a solution for this problem.
dicker2
Posts: 4
Joined: Wed May 29, 2013 11:29 am

Re: Import Matlab Code to Latex File

Post by dicker2 »

You can change the encoding of your m-file.
Open it in an editor (e.g. Notepad++) and then save the file anew, with a different encoding. ANSI should work.

Potential warning: I don't know, how Matlab will react, when it tries to use that m-file and it is not utf8 encoded
Post Reply