[Tex/LaTex] Randomly color paragraphs in LaTeX document

colorparagraphs

I usually type up my notes for school in LaTeX, and I find it easier to focus on text where the color changes between paragraphs, which is easy to do with old fashioned pen and paper.

On LaTeX, however, is there a way where the compiler can automatically do this while rendering my pdf changing the color of random paragraphs?

Best Answer

You need no external package (except for xcolor) if you use pdfTeX or LuaTeX (not XeTeX) as engine.

\documentclass{article}
\usepackage{everyhook,xcolor}

\newcommand{\randomcolor}{%
  \definecolor{randomcolor}{RGB}
   {
    \pdfuniformdeviate 256,
    \pdfuniformdeviate 256,
    \pdfuniformdeviate 256
   }%
  \color{randomcolor}%
}

\PushPostHook{par}{\randomcolor}

\begin{document}

\section{First}

One

Two

Three

\section{Second}

Four

Five

Six

\end{document}

The primitive \pdfuniformdeviate must be followed by an integer and expands to a uniformly distributed random integer between 0 (included) and the given number (excluded).

Using everyhook is safer than playing directly with \everypar.

enter image description here