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.
-
1From 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
-
related: How to strip a listing of all (line and block) comments without leaving white space? – jub0bs Mar 5 '14 at 11:09
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
-
1Having poked around in the manual: if you specify
morecomment=[is]{/*}{*/}
, thei
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 -
2I got away with
morecomment=[is]{\#\%}{\#:}
, this required only minor changes to source files and looks well. Thanks for thei
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