LaTeX forum ⇒ Conversion ToolsImport Matlab Code to Latex File

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

Import Matlab Code to Latex File

Postby rudrales » Wed Nov 11, 2009 3:53 pm


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!


Posts: 20
Joined: Tue Jul 28, 2009 5:10 pm

Postby ignasi » Wed Nov 11, 2009 5:18 pm

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


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

Postby rudrales » Wed Nov 11, 2009 7:04 pm

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

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

Command to import the Matlab text in the LaTeX document:
  1. \lstinputlisting[language=Matlab]{file_name.m}

Posts: 1
Joined: Thu May 16, 2013 3:18 pm

Postby sveinn79 » Thu May 16, 2013 3:31 pm

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.

Posts: 4
Joined: Wed May 29, 2013 11:29 am

Postby dicker2 » Wed May 29, 2013 11:38 am

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

Return to “Conversion Tools”

Who is online

Users browsing this forum: No registered users and 1 guest