I'm using listings package and \lstinputstring command to insert code from a file. Is it possible to hide code comments in inserted code? I'm using a language in which # starts a comment and it lasts until the end of line. I tried to do this with escapeinside option, but I think I don't understand how it works.


I'd just cheat, and specify commentstyle=\color{white}. This is even used in the manual :-) It does have the defect that if you print on non-white paper you'll see it, but that's usually not happening. Of course, if you have a non-white background, just use the background color instead.

Edit: For block comments, you can also use morecomment=[is]{/*}{*/}, where the i makes the comment completely invisible; however, this doesn't work for single-line comments, instead deleting all of the source code after the first single-line comment it finds.

  • If I have lines with only comments it leaves blank lines=( I tried negative \vspace in commentstyle but I guess it doesn't work in verbatim environment. – Marek Sapota Nov 19 '10 at 3:41
  • Hmm, that'll be harder. What Will said might be best—preprocess your file first. That said, I'll think on it and see if I can come up with any hacks. – Antal Spector-Zabusky Nov 19 '10 at 10:35
  • 1
    Having poked around in the manual: if you specify morecomment=[is]{/*}{*/}, the i makes the comment completely invisible, but I can't get this to work right with line comments (it just deletes the whole rest of the text after the first comment, not even reserving space for it). – Antal Spector-Zabusky Nov 19 '10 at 10:58
  • 2
    I got away with morecomment=[is]{\#\%}{\#:}, this required only minor changes to source files and looks well. Thanks for the i tip. – Marek Sapota Nov 19 '10 at 15:21
  • 1
    @maarons: Glad that worked out for you—I added it to the answer, for completeness's sake. – Antal Spector-Zabusky Nov 19 '10 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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