The aim is to define a new command \colorPlus[<color-when-printed>]{<color-in-PDF>}
that
- works exactly like the usual
\color{<color-in-PDF-and-print>}
when used without optional argument as in\colorPlus{<color-in-PDF}
; - does some
osgx2
-type magic when an optional argument is given. For example,\colorPlus[black]{red}
will make text show up red in the PDF but be black when printed.
According to some old comments from 2015 (1, 2) by Mariano Suárez-Álvarez and Herbert/user2478, this should be possible. The question is then how to do it.
You may change the syntax of \colorPlus
, or use some existing commands instead, if you believe to have a better syntax.
As a test, change \color{red}
in the following MWE to \colorPlus{black}{red}
to see if your solution is working.
\documentclass{book}
\usepackage{tocloft}
\usepackage{titlesec}
\usepackage{xcolor}
\usepackage[colorlinks=true, linkcolor=blue, hypertexnames=false, linktoc=all]{hyperref} % blue internal links for document; in TOC, entry name and page nr are links
\usepackage[ocgcolorlinks]{ocgx2}
%%% Definition of \colorPlus{}
%%%
\renewcommand{\cftsecpagefont}{\color{red}} % change "\color{red}" to \colorPlus[black]{red} as a test
\begin{document}
{\hypersetup{hidelinks}
\tableofcontents}
\chapter{Example chapter}
\section{Example section}
\subsection{Example subsection}
This is some text.
\end{document}
The MWE is essentially from my earlier question How to make red page numbers in table of contents while 'linktoc=all' and '\hypersetup{linkcolor=black}' are present? and samcarter_is_at_topanswers.xyz's answer. The section page number is red when viewed in PDF (but, in this case, also when printed).