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.
Asked
Active
Viewed 262 times
3
-
Related: https://tex.stackexchange.com/a/153281/11604 but change of color is very subtle line by line (the idea here was that the color changes wouldn't be noticed easily). – Fran Dec 09 '19 at 10:55
2 Answers
5
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}%
% add further colors here
\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}%
% add further colors here
\or\color{blue}%
\setcounter{cyclecolor}{0}%
\fi}
\PushPostHook{par}{\@cyclecolor}
\makeatother
\begin{document}
% start with normal black
\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}
campa
- 31,130
-
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 (https://tex.stackexchange.com/questions/519455/is-there-any-ways-to-show-the-bounding-boxes-of-a-tex-text), so I changed to this colored way. If you know any other way, that will be more appreciated! Thanks. – bluebird Dec 09 '19 at 09:28
3
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}
Fran
- 80,769
-
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 09 '19 at 12:58
-
The begin and end of paragraphs are always enough clear except when both
\parskipand\parindentare not nearly0pt, 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 09 '19 at 22:15 -
Usually the macro used as a fallback of
\paris\endgraf, no need to do a\let\oldpar\par. – Skillmon Dec 09 '19 at 23:02 -
OK, thanks a lot. I have clarified what I really ned in the new question https://tex.stackexchange.com/questions/519906/translate-english-tex-to-chinese-precisely – bluebird Dec 10 '19 at 02:38


