Sequentially color paragraphs in LaTeX document

This question is similar to Randomly color paragraphs in LaTeX document , but I want to color the paragraphs with unique color (color list), is there any way to do this? (I want to comment on the original question's answer, but I don't have enough reputations.

There sure is room for improvement (e.g. one could define the color list as a comma-separated list), but you can use the following code, shamelessly based on egreg's answer to the question you linked.

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

\newcounter{cyclecolor}
\newcommand*{\cyclecolor}{%
\stepcounter{cyclecolor}%
\ifcase\value{cyclecolor}%
\or\color{red}%
\or\color{orange}%
\or\color{teal}%
\or\color{blue}%
\setcounter{cyclecolor}{0}% the last one in the list must reset the counter
\fi}

\PushPostHook{par}{\cyclecolor} % https://tex.stackexchange.com/a/157158/82917

\begin{document}

\section{First}

\lipsum[66]

\lipsum[75]

\lipsum[66]

\lipsum[75]

\lipsum[66]

\lipsum[75]

\end{document}


Alternatively, you can define two user macros\cyclecolor and \nocyclecolor which switch the behaviour mid-document

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

\newcounter{cyclecolor}

\makeatletter
\def\@cyclecolor{\relax}
\newcommand*{\cyclecolor}{\def\@cyclecolor{\@@cyclecolor}}
\newcommand*{\nocyclecolor}{\color{black}\def\@cyclecolor{\relax}}

\newcommand*{\@@cyclecolor}{%
\stepcounter{cyclecolor}%
\ifcase\value{cyclecolor}%
\or\color{red}%
\or\color{orange}%
\or\color{teal}%
\or\color{blue}%
\setcounter{cyclecolor}{0}%
\fi}

\PushPostHook{par}{\@cyclecolor}
\makeatother

\begin{document}
\lipsum[66]

\cyclecolor % cycles through colors
\lipsum[75]

\lipsum[66]

\lipsum[75]

\lipsum[66]

\lipsum[75]

\lipsum[75]

\nocyclecolor % restores normal black
\lipsum[66]

\lipsum[75]

\end{document}


• Thanks, I figured out to apply color with the help of counter also. Can I change the background color of each paragraphs in this way? (Keep the text and background the same color, so as to segment different paragraphs.) Actually, my original target is to located different text by bounding box which seems hard (tex.stackexchange.com/questions/519455/…), so I changed to this colored way. If you know any other way, that will be more appreciated! Thanks. – bluebird Dec 9 '19 at 9:28

Not fully tested but ...

\documentclass{article}
\usepackage{lipsum}
\usepackage{xcolor}
\definecolorseries{parrafo}{rgb}{last}{blue}{red}
\resetcolorseries[3]{parrafo}
\let\oldpar\par
\def\par{\oldpar\color{parrafo!!+}}
\begin{document}
\section{Test}
\lipsum[1][1-5]\par
\lipsum[2][1-5]\par
\lipsum[3][1-5]\par
\lipsum[4][1-5]\par
\lipsum[3][1-5]\par
\lipsum[4][1-5]\par
\section{More test}
\lipsum[1][1-5]\par
\lipsum[2][1-5]\par
\lipsum[3][1-5]\par
\lipsum[4][1-5]\par
\lipsum[3][1-5]\par
\lipsum[4][1-5]\par
\end{document}

• Thanks a lot. Can you help on this: How can I replace the several start-ing and end-ing characters of each paragraph? [What I really want is to locate each text line in final pdf, but couldn't find the direct way. So tried so many other ways but not success 😂 – bluebird Dec 9 '19 at 12:58
• The begin and end of paragraphs are always enough clear except when both \parskip and \parindent are not nearly  0pt, and this is inusual (and wrong), so it is not still clear for me what you really need. Make a new question, describing not the problem to solve the solutions that you are thinking (highlight with color, replace characters or whatever) but your true problem as clearly as possible. That mean using a minimal working example (MWE) where you can explaining what is problematic exactly. – Fran Dec 9 '19 at 22:15
• Usually the macro used as a fallback of \par is \endgraf, no need to do a \let\oldpar\par. – Skillmon Dec 9 '19 at 23:02
• OK, thanks a lot. I have clarified what I really ned in the new question tex.stackexchange.com/questions/519906/… – bluebird Dec 10 '19 at 2:38