I'm writing the documentation to a large piece of Matlab code. I would like include tags in my Matlab code and then read the lines of code between the tags into Latex and output them as verbatim (or use some other form of code representation such as the lstlisting package).

I noticed that the catchfile package can help me with reading code as verbatim (as described here) and that the catchfilebetweentags package does essentially the other part of the job (as described in the answers to this question, yet I'm still struggling to do both simultaneously. Any ideas? Thank you.


1 Answer 1


I don't know how to use catchfilebetweentags. I would write a script to extract each code section into its own file, then use the mcode package to include the file in the doc.

You might find mcode useful anyway, since it does the matlab syntax highlighting for you.

\lstinputlisting[firstline=6, lastline=15]{/SOME/PATH/FILENAME.M}
  • Yes, it's this plus the arbitrary line-range markers.
    – john
    Apr 10, 2011 at 16:08
  • You should have a look at the matlab-prettifier package; it improves upon mcode in many ways. See this answer, for instance.
    – jub0bs
    Apr 28, 2014 at 15:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .