# How can I insert a Matlab code in Lyx easily

I have a .m file with Matlab code and I want to paste it in Lyx. How can I do this easily?

I'd recommend using the matlab-prettifier package (see https://tex.stackexchange.com/a/158816/586), which is really just provides a style for the listings package. This will give you good highlighting of Matlab code, while still enabling you to use the built-in stuff in LyX related to code listings (if you so desire).

To use the package, take these two steps:

1. Document --> Settings --> LaTeX preamble: Add

\usepackage{matlab-prettifier}

2. Document --> Settings --> Listings: Add

style=Matlab-editor


Now, there are two different methods for getting code into LyX. Either input a file directly, or copy-paste code from Matlab:

### Input file

In your document, do Insert --> File --> Child Document. Choose Program listing as "Include type", click the Browse button, and find an .m that you want to include:

You can write caption and label right away, if you like, or do it later by right-clicking on the inset and choosing settings.

### Copy-pasting code

The key-thing if you do want to copy-paste code into a listing (made with Insert --> Program listing, or ERT), is that you need to use paste special, i.e. Ctrl + Shift + V or Edit --> Paste special --> Plain text. If you don't, line breaks in the code will not be preserved.

For the code environment, you can use Mark Pace's code in an ERT directly, or you can use Insert --> Program listing, which inserts a lstlisting environment, just as Mark's code. If you right-click in that inset and choose settings,

you can adjust some things. To adjust settings that are not available from the first tab of the settings dialog, go to the Advanced tab, and write them as key=value pairs in the right panel. This includes things like caption and label.

One caveat here: if you use this method to insert a listing with a caption, you will not be able to access that label in LyX's cross-reference system. That is, if you do Insert --> Cross-reference, the code listings will not show up in the list of labels. A child document-listing on the other hand, will show up. (You can circumvent that by adding a new label via Insert --> Label inside a LyX note, made with Insert --> Note --> LyX Note, but it's more of a hassle.)

• Thanks! I used the special paste with the code provided by Mark Pace and it worked perfectly! :D – Moltimor Aug 27 '17 at 15:55
• @Moltimor If you prefer to avoid ERTs for some reason, then use Insert -> Program listing instead, which does exactly the same, but you need to go into the advanced settings of the inset (via right click) to set the optional parameters (caption, label, etc.). – Torbjørn T. Aug 27 '17 at 17:38
• @Moltimor I extended my answer a little bit, by the way. – Torbjørn T. Aug 27 '17 at 18:01
• Is there a way to add a frame to program listing enviornment? – FreeZe Mar 23 at 22:48
• @FreeZe Add frame=single to Document -> Settings -> Listings, I think. – Torbjørn T. Mar 24 at 20:59

\usepackage{listings}
...
\lstinputlisting[language=Matlab]{//--PATH--//YourNameHere.m}


However I would rather use:

\begin{lstlisting}
[
language=Matlab
,caption={CAPTION}
,label=LABEL]
%%%%% COPY CODE HERE %%%%%
\end{lstlisting}

• Note that in LyX you can insert a listing with Insert > Program Listing – scottkosty Aug 26 '17 at 16:29
• Thanks. I tried both method, but in the first one I get some errors and in the second one (using "insert program listing" ) I don't get a text with proper format when compiling – Moltimor Aug 27 '17 at 15:11
• @Moltimor You can of course change the format of the listings (see tex.stackexchange.com/questions/53260/…), but what is "proper format"? – Torbjørn T. Aug 27 '17 at 15:27
• I copy de text from Matlab and paste it in LyX, but I don't get the right spacing bwetween lines and commentaries. It is pasted as plane text and it's illegible – Moltimor Aug 27 '17 at 15:29