3

I want to define an environment redout, that causes all text defined inside to be typeset in a changed color. However, I can't find a way to consistently recolor floats.

  • If recoloring is performed with just \color{red}, it doesn't affect floats and footnotes, and depending on the document class, may not affect headings either.
  • When adding \let\default@color\current@color, it affects floats and footnotes wrongly based on when they are typeset, rather than when they are defined.

I can imagine making this work by redefining all footnote/float commands, but that is error-prone. Is there any more elegant version to achieve coloring based on the position in the source-code?

                      enter image description here

\documentclass{article}
\usepackage[paperwidth=8cm,paperheight=3cm,scale=0.9]{geometry}
\usepackage{xcolor}

\begin{document}

This text is black.
\footnote{This footnote should be black, but is red.}

\begingroup
\color{red}
\makeatletter
\let\default@color\current@color % required to affect footnotes.
This text is red.
\footnote{This footnote is red, as intended.}

\clearpage
This text is still red.
\footnote{This footnote should be red, but being \emph{typeset} after the \texttt{\string\endgroup}, it isn't.}
\endgroup

This text is correctly black again.
\end{document}

2 Answers 2

2

As you show redefining \default@color does more harm than good, I think if you want to set footnotes in the current colour it is simpler just to set the colour at the start of the footnote either explicitly as here or by redefining \footnotetext macro to add the colour \color{.}.

enter image description here

\documentclass{article}
\usepackage[paperwidth=8cm,paperheight=3cm,scale=0.9]{geometry}
\usepackage{xcolor}

\begin{document}

This text is black.
\footnote{\color{.}This footnote should be black, but is red.}

\begingroup
\color{red}

This text is red.
\footnote{\color{.}This footnote is red, as intended.}

\clearpage
This text is still red.
\footnote{\color{.}This footnote should be red, but being \emph{typeset} after the \texttt{\string\endgroup}, it isn't.}
\endgroup

This text is correctly black again.
\end{document}
5
  • Thanks for the answer! Two questions though: (a) Why does \color{.} work? From my understanding of how footnotes are typeset, I would expect \color{.} to be expanded when the footnote is typeset, and thus sometimes after the current color has already changed. (b) Can this be extended, such that the color affects the footnote number too? This was one of my motivations for using \let\default@color over patching many commands.
    – kdb
    Commented Sep 7, 2018 at 12:37
  • Also, does this imply that there simply is no possibility to change the color for all floating elements and headings, without redefining the commands? (In scrartcl headings are not affected by the \color command in the text, so they too would need to be patched with \color{.}).
    – kdb
    Commented Sep 7, 2018 at 12:41
  • Experiments with saveboxes indicate that coloring is done independently of formatting. OTOH, I have seen \normalcolor used in LaTeX2e source. Commented Sep 7, 2018 at 15:17
  • @kdb footnotes are typeset when they appear in the source, the box is re-used after the page breaking but no macros are expanded at that time Commented Sep 7, 2018 at 15:21
  • 1
    @kdb by design floats are taken out of the text flow and so latex ensures that they do not inherit the current line width (eg a float in an indented list is still full width) or the current font or current colour. So yes if you want to change that design you will have to redefine something to change latex's behaviour. Commented Sep 7, 2018 at 15:23
0

With lualatex or xelatex you could use colored fonts. That's quite reliable, but math must be reset at the end of the group:

\documentclass{scrartcl}
\usepackage[paperwidth=8cm,paperheight=3cm,scale=0.9]{geometry}
\usepackage{fontspec,unicode-math,xcolor}

\setmainfont{Latin Modern Roman}
\setsansfont{Latin Modern Sans}

\begin{document}

This text is black.
\footnote{This footnote should be black}
\[a=\int x\]

\begingroup
\setmainfont{Latin Modern Roman}[Color=FF0000]
\setsansfont{Latin Modern Sans}[Color=FF0000]
\setmonofont{Latin Modern Mono}[Color=FF0000]
\setmathfont{Latin Modern Math}[Color=FF0000]
\section{should be red}
This text is red. \[a=\int x\]
\footnote{This footnote is red, as intended.}

\clearpage
This text is still red. \textcolor{green}{even with color green ...} unless you use
{\addfontfeature{Colour=green} addfontfeature}
\footnote{This footnote should be red, even after the \texttt{\string\endgroup} $a=\int x$.}

\begin{figure}[t]
This float should be red
\[a=\int x\]
\end{figure}
\endgroup \setmathfont{Latin Modern Math}

This text is correctly black again. \[a=\int x\]
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .