[Tex/LaTex] Code-trolling/prank questions for TeX.SX

fun

I have a friend who uses me for LaTeX support. He isn't very tech savvy but likes LaTeX and so I help him out when something he needs to do requires more than the standard stock of macros and environments.

We like to play pranks on each other. I had the idea to prank him the next time he ask for LaTeX help by doing something relatively benign and easily reversible that would noticeably alter the PDF generated— or do something else noticeable in normal (La)TeX usage, like prolonging compilation —in an unwanted fashion where the source of the problem would be hard to detect (so it would be a case of code-trolling, I suppose). I'm not very good at LaTeX myself, though, and so thought about asking for creative suggestions from the TeXperts.

How can I give my friend a (La)TeX related hard time?

Best Answer

Add to their aux file the following code snippet, which is a quine. When the aux file is \input at the start of a LaTeX run, the code here will build a copy of itself and write it in the aux file for the next run. Additionally, it will run the code contained in \toks2 below. For instance, the code I chose increases the indent size at each paragraph, but only if TeX is run on an odd minute: results depend on when your friend compiles.

{%
  \toks@{%
    \ifx\@nodocument\relax\else
      \toks2{% Here you put whatever mean code you want
        \ifodd\time
          \everypar\expandafter{%
            \the\everypar
            \advance\parindent 2pt\relax
          }%
        \fi
      }% end of \toks2
      \edef\x{%
        \noexpand\AtBeginDocument{%
          \the\toks2\relax
          \toks@{\the\toks@}%
          \immediate\write\@auxout{%
            {%
              \toks@{\noexpand\the\toks@}%
              \noexpand\noexpand\noexpand\the\toks@
            }% end of brace group
          }% end of \immediate\write\@auxout
        }% end of \AtBeginDocument
      }% end of \edef
      \x
    \fi
  }%
  \the\toks@
}

After one run, the aux file will contain the following condensed version (in a single line)

{\toks@ {\ifx \@nodocument \relax \else \toks 2{\ifodd \time \everypar
\expandafter {\the \everypar \advance \parindent 2pt\relax }\fi }\edef
\x {\noexpand \AtBeginDocument {\the \toks 2\relax \toks@ {\the \toks@
}\immediate \write \@auxout {{\toks@ {\noexpand \the \toks@ }\noexpand
\noexpand \noexpand \the \toks@ }}}}\x \fi }\the \toks@ }

and subsequent runs will leave the same condensed version (again in one line).

Let's see what happens in detail: within a (simple) group, the token register \toks@ is set to some value, then its contents are used. What do those contents do? There is a test to check whether we are reading the aux file at the start or at the end of the run: \@nodocument is \relax in the second case and we do nothing. Then the toks register \toks2 is set to the code you actually want to perform. The following \edef\x{...}\x construction expands the ... to

\AtBeginDocument{%
  <contents of \toks2>\relax
  \toks@{<contents of \toks@>}%
  \immediate\write\@auxout{%
    {%
      \toks@{\the\toks@}%
      \noexpand\the\toks@
    }% end of brace group
  }% end of \immediate\write\@auxout
}% end of \AtBeginDocument

then performs that code. \AtBeginDocument will run its argument a bit later, once LaTeX is again ready to write in the aux file (currently it is reading it). So, once LaTeX is ready to write to the aux file, it performs your code (which was stored temporarily in \toks2), then stores the <contents of \toks@> back into \toks@ (this token register may have been used by other code in between), and writes the following to the aux file (remember that \write expands):

{%
  \toks@{<contents of \toks@>}%
  \the\toks@
}% end of brace group

This is precisely the original code, which thus ends up in the aux file for the next run of LaTeX.

Hopefully, the code I chose to put in \toks2 is easy enough to follow:

\ifodd\time
  \everypar\expandafter{%
    \the\everypar
    \advance\parindent 2pt\relax
  }%
\fi

if the time (in minutes since the beginning of the day) is an odd number, then at every paragraph, do whatever was already done at every paragraph, and also advance (increase) the paragraph indentation (\parindent) by 2 points. For instance, say you add the first or second code snippets above to the aux file generated by running pdflatex on the document below. Then the document will be normal if compiled on an even minute, and otherwise will have ever growing paragraph indentation.

\documentclass{article}
\usepackage{lipsum}
\begin{document}
\lipsum[1-10]
\end{document}