TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
Have you considered the other way round? Creating a LaTeX document and extracting the Matlab code? This is called literate programming and there are several systems to support it. – Christian Lindig Apr 10 '11 at 8:48
Have a look at tex.stackexchange.com/questions/4889/input-only-part-of-a-file if you haven't already. – Harald Hanche-Olsen Apr 10 '11 at 12:44
You can use tags with listings see documentation 5.7 Arbitrary linerange markers – Ulrike Fischer Apr 10 '11 at 13:19
Perfect, the arbitrary linerange markers were exactly, what I was looking for. Thank you. – john Apr 10 '11 at 16:11
You might be interested in the matlab-prettifier package; see this answer. – Jubobs Apr 28 '14 at 15:42

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}
share|improve this answer
Yes, it's this plus the arbitrary line-range markers. – john Apr 10 '11 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. – Jubobs Apr 28 '14 at 15:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.