Do comments in a LaTeX file change the output?
The Endeavour 2024-02-08
When you add a comment to a LaTeX file, it makes no visible change to the output. The comment is ignored as far as the appearance of the file. But is that comment somehow included in the file anyway?
If you compile a LaTeX file to PDF, then edit it by throwing in a comment, and compile again, your two files will differ. As I wrote about earlier, the time that a file is created is embedded in a PDF. That time stamp is also included in two or three hashes, so the files will differ by more than just the bits in the time stamp.
But even if you compile two files at the same time (within the resolution of the time stamp, which is one second), the PDF files will still differ. Apparently some kind of hash of the source file is included in the PDF.
So suppose you have two files. The content of foo.tex
is
\documentclass{article} \begin{document} Hello world. \end{document}
and the content of bar.tex
is
\documentclass{article} \begin{document} Hello world. % comment \end{document}
then the output of running pdflatex
on both files will look the same.
Suppose you compile the files at the same time so that the time stamps are the same.
pdflatex foo.tex && pdflatex bar.tex
It’s possible that the two time stamps could be different, one file compiling a little before the tick of a new second and one compiling a little after. But if your computer is fast enough and you don’t get unlucky, the time stamps will be the same.
Then you can compare hex dumps of the two PDF files with
diff <(xxd foo.pdf) <(xxd bar.pdf)
This produces the following
< ... ./ID [<F12AF1442 < ... E03CC6B3AB64A5D9 < ... 8DEE2FE> <F12AF1 < ... 442E03CC6B3AB64A < ... 5D98DEE2FE>]./Le -- > ... ./ID [<4FAA0E9F1 > ... CC6EFCC5068F481E > ... 0419AD6> <4FAA0E > ... 9F1CC6EFCC5068F4 > ... 81E0419AD6>]./Le
You can’t recover the comment from the binary dump, but you can tell that the files differ.
I don’t know what hash is being used. My first guess was MD5, but that’s not it. It’s a 128-bit hash, so that rules out newer hashes like SHA256. I tried searching for it but didn’t find anything. If you know what hash pdflatex
uses, please let me know.
LaTeX will also let you add text at the end of the file, after the \end{document}
command. This also will change the hash code but will not change the appearance of the output.