Are comments safely hidden once the document is compiled?

The answer to your question is two fold:

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.


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.