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 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.

share|improve this question
From your comment below you say that you want lines with only comments to be ignored. In this case I'd recommend just pre-processing the file. – Will Robertson Nov 19 '10 at 4:02
up vote 7 down vote accepted

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.

share|improve this answer
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
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
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
@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 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.