GeneralConverting colored text to Black for printing

LaTeX specific issues not fitting into one of the other forums of this category.
curiouslearn
Posts: 105
Joined: Fri Nov 30, 2007 11:32 pm

Converting colored text to Black for printing

Post by curiouslearn »

Hi,

I have some text/expressions in my document for which I have used colored text. I have defined custom colors in some cases and used the standard "blue" color in others. The colored expressions do not print well on the black and white printer (they are lighter than the surrounding text). I was wondering whether there was a simple command that I could use or define to convert them to black. I could then use that copy for printing and then comment out that command to get the colors back.

I have used the following command to get the colored text. In the preamble I have defined:
\newcommand{\clr}[2]{{\color{#1}#2}}


In the body I use \clr{blue}{This is blue text}.

\clr{ored}{This is orange red text. Color ored has been defined using the definecolor command.}

Thanks very much.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Converting colored text to Black for printing

Post by gmedina »

You could use, after the definition of your \clr command, something like

Code: Select all

\renewcommand{\clr}[2]{{\color{black}#2}}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Converting colored text to Black for printing

Post by Juanjo »

To disable coloring, it suffices to load the color or xcolor packages with the monochrome option:

Code: Select all

\usepackage[monochrome]{color}
By the way, there already exists the \textcolor command with the same syntax and functionality of your \clr command. So, you can simply write, for example, \textcolor{blue}{This is blue text}. This saves you a command definition.
curiouslearn
Posts: 105
Joined: Fri Nov 30, 2007 11:32 pm

Re: Converting colored text to Black for printing

Post by curiouslearn »

Thanks very much gmedina and Juanjo. I will try both these suggestions in the morning tomorrow (my laptop on which I have the file is at school). Actually, I was thinking of trying the command that gmedina suggested...but it seemed wrong because {\clr}[2] says that \clr has two arguments but {{\color{black}#2}} does not have #1 in it. I thought this would result in an error. I may have even tried it; but most likely I did not because none of the commands that I tried worked.

Juanjo, I will also try your solution. Thanks for the information about the \textcolor. I could use it for text; but I used the \clr command also for some mathematical expressions.
curiouslearn
Posts: 105
Joined: Fri Nov 30, 2007 11:32 pm

Converting colored text to Black for printing

Post by curiouslearn »

Hi,

I tried both commands mentioned above and wanted to given an update:

\renewcommand{clr}[2]{\color{black}#2} worked fine and did the job.

Adding the monochrome option when loading xcolor package did not work, however, because it conflicts with the natbib package. I get the following error (copied from the log file)
! LaTeX Error: Option clash for package xcolor.

See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...

l.10 \usepackage
{natbib}
? h
The package xcolor has already been loaded with options:
[]
There has now been an attempt to load it with options
[monochrome]
Adding the global options:
,monochrome
to your \documentclass declaration may fix this.
I tried the command \documentclass[12pt,monochrome]{article}. But that did not work. Is there a way around this? As I said, gmedina's suggestion work out well, but it would be good to know how to make this latter solution work when there is a clash with another package. That would be helpful, for example, when I am using a package written by someone else (say a package to make exams) but want to override their color settings and print the exam in black and white.

Thanks for your help.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Re: Converting colored text to Black for printing

Post by Juanjo »

Have you loaded xcolor two times? This may cause the error you report. If this is not the issue, could you paste here the preamble of your .tex file so I can try to detect the problem?
curiouslearn
Posts: 105
Joined: Fri Nov 30, 2007 11:32 pm

Converting colored text to Black for printing

Post by curiouslearn »

Juanjo, thanks for your attention to this problem. I have loaded xcolor only once. Though it might be loaded by the other packages that I load. Here is the preamble:

Code: Select all

\documentclass[12pt]{article}

\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
%\usepackage[total={6.5in,8.5in},top=1.2in,left=1in]{geometry}%Total textwidth=6.5in, textheight=8.5in,topmargin=1.2in and leftmargin=1in
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{natbib}


\usepackage{makeidx}
\makeindex

\definecolor{dgray}{RGB}{64,64,64}

\usepackage[pdftex,pdfstartview=FitH]{hyperref}
\hypersetup{pdfpagemode=none,
            colorlinks=true,
            urlcolor=blue,
            citecolor=dgray}

\definecolor{ored}{RGB}{178,34,34} %defining custom color (command available in the xcolor package}
\definecolor{fgreen}{RGB}{34,139,34} %Forestgreen

\setlength{\parindent}{0pt} %  Zero indentation for paragraphs
\setlength{\parskip}{1ex plus 0.5ex minus 0.2ex} %Space between paragraph
                                                 %Plus and Minus allow Latex to adjust the spacing if necessary for proper alignment.

\setlength{\marginparwidth}{1in}

%BEGIN Shortcuts
\newcommand{\Pai}[1]{P_{A}(#1)}
\newcommand{\clr}[2]{{\color{#1}#2}}
\newcommand{\mymargin}[1]{\marginpar{\vskip-\baselineskip\scriptsize\raggedright\clr{fgreen}{#1}}}
%\renewcommand{\clr}[2]{{\color{black}#2}} %To convert to black color for printing.
%END Shortcuts

User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Converting colored text to Black for printing

Post by Juanjo »

The tikz package already loads xcolor. To disable coloring, you have two simple solutions: either you load xcolor and tikz in reverse order

Code: Select all

\usepackage[monochrome]{xcolor}
\usepackage{tikz}
or you put the monochrome option in \documentclass and load only tikz

Code: Select all

\documentclass[12pt,monochrome]{article}
\usepackage{tikz}
To reenable coloring, in the first case, you should load only tikz and, in the second, you should remove the monochrome option from \documentclass. Check your actual code, since you load xcolor innecessarily.

By the way, \textcolor also works in math mode. Try this, for example:

Code: Select all

\[ \textcolor{blue}{\sum_{i=1}^n i^2}\textcolor{cyan}{{}={}}\textcolor{magenta}{\frac{n(n+1)(2n+1)}{6}} \]
curiouslearn
Posts: 105
Joined: Fri Nov 30, 2007 11:32 pm

Converting colored text to Black for printing

Post by curiouslearn »

Thanks Juanjo. I will try these. I tried putting monochrome option in the \documentclass (as in the code below)

Code: Select all

\documentclass[12pt,monochrome]{article}
\usepackage{tikz}
\usepackage{xcolor}
But that did not work, perhaps because I am loading xcolor twice. I will try

Code: Select all

\documentclass[12pt,monochrome]{article}
\usepackage{tikz}
Can you please also suggest what I may do if I have a situation such as below, where both tikz and package Apackage load xcolor? Do you think using monochrome in \documentclass will help in that case, given that it does work when xcolor is loaded twice (once in tikz and once by itself)?

Code: Select all

\documentclass[12pt,monochrome]{article}
\usepackage{tikz}
\usepackage{Apackage}
Thanks again.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Converting colored text to Black for printing

Post by Juanjo »

To my knowledge, the package options mechanism works as follows (I hope not to be wrong!). When LaTeX finds a \usepackage command, it checks the optional arguments of this command and that of \documentclass for any options declared in the package. Then it loads the package with those found options. For example, assume that mypack is package that admits options optA and optB. The code

Code: Select all

\documentclass[optA]{article}
\usepackage[optB]{mypack}
loads mypack with both options optA and optB. In this case, optA is a global option and also applies to any other package which eventually could recognize it. Likewise, optB is a local option and applies to mypack, but not to any other package loaded with a diffferent \usepackage command.

We may run into problems when mypack is loaded twice. If, in the second call to the \usepackage command, there are, at most, the same local options than in the first call, nothing happens. LaTeX ignores the second call. However, if the second call introduces a new local option, LaTeX complains. It writes the option clash error message in the TeX console and stops. Fortunately, this error can be recovered. Simply press <return>. LaTeX then ignores the second \usepackage comand and concludes compilation. So, this code works:

Code: Select all

\usepackage[optA,optB]{mypack}
\usepackage[optB]{mypack}
(It is nevertheless "bad" code, due to the second superfluous \usepackage). Contrarily, this code fails:

Code: Select all

\usepackage[optA]{mypack}
\usepackage[optB]{mypack}
But you can still finish compilation by hitting <return> in the TeX console when you are prompted. The mypack package is loaded only once with option optA.

Now, assume that packA and packB are packages that loads mypack with options optA and optB, respectively. As before, this code fails:

Code: Select all

\usepackage{packA}
\usepackage{packB}
Even if the compilation could be finish, things presumably wouldn't work, since mypack wouldn't be loaded with the optB option required by packB. Perhaps the authors of these packages have foreseen such a situation and have declared optA and optB as options of their respective packages. Then this code may work:

Code: Select all

\usepackage[optA,optB]{packA}
\usepackage{packB}
Other approach could be to make global the needed options:

Code: Select all

\documentclass[optA,optB]{article}
\usepackage{packA}
\usepackage{packB}
Now, if compilation stops, the error can be recovered as said above and mypack is loaded with both options optA and optB.

So, let's return to your particular case. The code

Code: Select all

\documentclass[12pt,monochrome]{article}
\usepackage{tikz}
\usepackage{xcolor}
should work. At least, it does in my computer. Observe that the second call to xcolor does not add local options. Anyway, the right code should be

Code: Select all

\documentclass[12pt,monochrome]{article}
\usepackage{tikz}
You then ask:
curiouslearn wrote: Can you please also suggest what I may do if I have a situation such as below, where both tikz and package Apackage load xcolor? Do you think using monochrome in \documentclass will help in that case, given that it does work when xcolor is loaded twice (once in tikz and once by itself)?

Code: Select all

\documentclass[12pt,monochrome]{article}
\usepackage{tikz}
\usepackage{Apackage}
.
From the preceding explanation, it follows that this code may work provided that Apackage does not introduces more xcolor options. If this were the case, add them to \documentclass. Although compilation may stop, you can still get your document.
Post Reply