0

As mentioned by the title, I need to change font color for only the current page. After searching the internet, the afterpage package seems to be a good choice, as well as \colorlet{saved}{.} from the xcolor package. Hence the following code.

\documentclass{article}
\usepackage{xcolor}
\usepackage{afterpage}
\usepackage{blindtext}
\begin{document}
\blindtext
\colorlet{saved}{.}
\color{yellow}
\afterpage{\color{saved}}

\blindtext
\clearpage
\blindtext
\end{document}

However, color of font in the second page remains unchanged.

Could you please figure why the color stays unchanged, and could you please help me how to solve the problem?

  • Does your real use case have a forced page break? if so the answer is easy just put \color{saved} after \clearpage or do you want the colour to change at an automatic-page break, possibly mid-paragrarah? That is harder and in general not always possible automatically. – David Carlisle May 14 '17 at 11:49
  • This could be an XY question, using \afterpage could be the wrong thing to do from the beginning. What is your greater goal? – Johannes_B May 14 '17 at 11:53
  • @DavidCarlisle It depends. Sometimes a \clearpage will be added and thus I could be able to declare color clearly. However, it's hard for me to add such color declaration in automatically process, for example in the toc output process. What if the fact is ensured that no float processing will try to break our color restore mechanism? Or, maybe we could have a try to handle this after the ltx2e layout codes have decided not to build a float page (column)? – Ch'en Meng May 14 '17 at 12:00
  • @Johannes_B, Thanks for the reply. The original problem is that I need to change the background color of specific area in the chapter title page. In the very page, a light color of font is therefore needed, and in the following pages, black should be recovered. Do you have any clues? – Ch'en Meng May 14 '17 at 12:02
  • Use only the chapter title on a page of its own. – Johannes_B May 14 '17 at 12:04
4

\afterpage adds a group. This here works as expected (but I wouldn't rely on it in all cases):

\documentclass{article}
\usepackage{xcolor}
\usepackage{afterpage}
\usepackage{blindtext}
\begin{document}
\blindtext
\colorlet{saved}{.}
\color{yellow}
\newcommand\restorecolor{\color{saved}}
\afterpage{\aftergroup\restorecolor}

\blindtext
\blindtext
\blindtext
\blindtext
\blindtext\par
\end{document}

enter image description here

  • Fantastic! It's a shame that I didn't know \aftergroup before. However, I guess this could be broke by the floating process? – Ch'en Meng May 14 '17 at 12:05
  • 2
    @Ch'enMeng this could be broken by anything :-) please read the warnings in the afterpage documentation. I wouldn't use it in any production setting. Changing a colour at a page change is such an odd thing to do, is there really no structural change in the document at that point which corresponds to the colour change? – David Carlisle May 14 '17 at 12:08
  • @Ch'enMeng: As I said: I wouldn't rely on it. I use afterpage only very seldom and very carefully. I only wanted to show why it didn't work in your (simple) example. Like David I find your requirement rather odd and imho there should be something more logical (as you mentioned the titlepage: I would simply use grouping there). – Ulrike Fischer May 14 '17 at 12:19
  • @DavidCarlisle Well, let me think twice about this. Thank you for the help. : ) – Ch'en Meng May 14 '17 at 12:21
  • @Ch'enMeng try replacing that \par by \blindtext ..... – David Carlisle May 14 '17 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.