Are comments safely hidden once the document is compiled?

This is kind of similar to some of the pdf to latex questions but from a different angle. I'm not looking for a complete LaTeX reconstruction method, I want to know if the comments are safely hidden.

My scenario is that I would like to distribute a sample test to my students which includes the real test or some variation of it commented out---that way I can keep the same file from year to year without having to manage multiple documents. I'm pretty sure that what's commented out is dropped entirely from the compiled document but I'd like to be absolutely certain before I distribute such a pdf. Is there any way whatsoever to extract commented lines from the final pdf? If it makes any difference, I'll probably be using XeLaTeX.

-
Comments are completly ignored during compilation and will in no way find their way in a pdf. –  Ulrike Fischer May 31 '12 at 7:31
\the\inputlineno will put something different into the PDF file if you add a comment line before it, so comments are not "completely ignored" when applying smartass criteria. But certainly well enough for the given application. Source specials also give away input location data, so if the length of a comment is relevant (like "is the shortest sequence matching the criteria less than 20 lines?")... –  user9588 May 31 '12 at 7:53
I'm submitting a journal paper today as PDF and LaTeX source and was happy to remember yesterday to remove all comments before that ;-) –  Martin Scharrer May 31 '12 at 9:55
Where's my tin foil hat??? :D –  Count Zero May 31 '12 at 11:50
You can attach your TeX file with the package attachfile and then everything is visible again. But this won't happen without explicitly requesting / including it. –  topskip Jun 12 '12 at 20:32

Yes, comments are ignored when compiling and the text of a comment is irrecoverable from the final document.

However, no. It is often possible to tell where / how long the comments in the source were. For instance, take the following senario:

Line 1
Line 2


vs.

Line 1%
Line 2


In the compiled output, the newline character after Line 1 in the first example is treated as whitespace and converted to a space, so the output reads Line 1 Line 2. However, in the second example, the comment character comments out the whitespace, so the final compiled output reads Line 1Line 2. While this example doesn't leak any information itself, it does show how easy it is for comments to modify the structure of a TeX document in ways that you wouldn't expect.

While I doubt this is relevant to your use case, it does make it possible to get some information about where comments were, and sometimes how long they were, so it might be good to think through your questions / answers to make sure you're not leaking any information in this manner (that being said, are your students likely to think it through this far?).

EDIT: Saw right after posting that someone beat me to it. Sorry for the redundant info.

-
Welcome to TeX-SX. It's preferred by far to have information in actual answers rather than comments so this isn't redundant at all. –  Loop Space Jun 12 '12 at 20:42
Ah, good, good. Long time idler on Stack exchange, but I don't post often, so this is good to know. –  Sam Whited Jun 12 '12 at 20:43

I'm sure the other answers here are correct, and it is always nice to have guarantees and standards and such, but thought I might add something.

If it were me and I very much wanted to be sure that the output of a process (the PDF) did not have some element of the input process (the TeX source) mixed with it, I would add canaries to the input and then search for them in the output.

The canary should be a searchable string which would only appear as a marker in sections which should be discarded.

For instance,

%The answer to this question is D. ImACanary
...
\begin{comment}
Explaning stuff, explaining stuff. ImACanary
\end{comment}


You can then use your favourite hexeditor to search the output PDF for the string "ImACanary". If you find the canary, leave the coal mine.

-
»If you find the canary, leave the coal mine.« Very nice idiom. For explanation look here: wisegeek.com/… –  Keks Dose Nov 21 '12 at 11:41
The PDF output from pdfTeX et.al. does not have to contain the string "ImACanary" - especially if you are using weird fonts (subsetting) or are producing PDF 1.5++ (object compression). Using a hex editor to search for text strings in PDFs is doomed to fail. –  Martin Schröder Nov 21 '12 at 12:18
Though it does not always fail, as the existence of pdfgrep, &c. suggests. Know your use-case and act accordingly. Granted, if the canary is enciphered in some odd format, perhaps that's sufficient. –  Richard Nov 21 '12 at 12:28