How do I keep the format of the MATLAB code I'm pasting to word?

matlab

Solution

m files are plain text files, the syntax highlighting you see is done by the MATLAB editor, and the color coding information is not saved as part of the m file itself. So there's no way to get simple copy-paste to retain syntax highlighting.

As for spacing, you may be able to get satisfactory results by setting the font in your Word file to a monospaced font, instead of a proportional one.

But your best option is to use the `publish` function to export the m file. It will even export to `doc` format directly, or you could export to `html` and then have Word import it.

Problem

I'm trying to copy and paste code I've written in MATLAB to word, but it automatically reformats the code into black and white and changes the tabbing/spacing. Is there any way to copy and paste it in the same format? I'm running MATLAB on a Virtual Machine through my University, so unfortunately I've found that many of the methods suggested on Google are not working. I figured this question would have already been asked, but it seems most of the copying/pasting questions have to do with graphs. I haven't seen anything about code format, but if there is a post I missed please point it out. Thanks!

Original source