11

I would like to produce two kinds of formattings for my documents.

  1. The 1st formatting will use colors. This is typically for reading on a screen.
  2. The 2nd formatting will use gray colors for printing.

Which method can I use to say to LaTeX which kind of formatting rules to use ?

Here is a very small example of the commands that I'm building.

\documentclass[10pt,a4paper]{article}
    \usepackage[utf8x]{inputenc}
    \usepackage{ucs}
    \usepackage{graphicx}
    \usepackage{xparse}
    \usepackage[x11names, svgnames]{xcolor}
    \usepackage{fourier-orns}

    \newcommand{\boldit}[1]{%
        \mathversion{bold}\textbf{#1}\mathversion{normal} %
    }

    \newbox\FBox
    \NewDocumentCommand\boxit{O{black}O{white}mO{0.5pt}O{0pt}O{0pt}}{%
        \setlength\fboxsep{#4}\sbox\FBox{\fcolorbox{#1}{#2}{#3\rule[-#5]{0pt}{#6}}}\usebox\FBox}

% The colored version.
    \newcommand{\warning}[1]{\textcolor{Red3}{\raisebox{\depth}{\danger}\boldit{\,#1}}}
% The gray version that must be used instead
% of \warning if the mode is black and white.
    \newcommand{\warningBW}[1]{\boxit[black!80][black!30]{\raisebox{\depth}{\danger}\boldit{\,#1}}}


\begin{document}

Let's try one example... \warning{Danger !}

\end{document}
  • You could try \ifthen for the parts which differ. The argument could be a value you declare in the preamble and set before compiling. – Count Zero Nov 21 '11 at 17:13
  • 1
    BTW: See tex.stackexchange.com/questions/13067/utf8x-vs-utf8-inputenc about using utf8x/ucs. – Schweinebacke Nov 21 '11 at 17:19
  • This question deals with hyperlinks – cmhughes Nov 21 '11 at 19:23
  • Would one possible solution be just to produce the document in colour, then use the greyscale option of your PDF viewer's print dialogue? Or do you want to make specific tweaks to the greyscale version? – Brent.Longborough Nov 21 '11 at 20:45
  • Yes, I will do specific tweaks to the greyscale version. For example, the color orange is good for screen but not really viewable in greyscale printings. – projetmbc Nov 21 '11 at 21:21
4

I'd organize the document like this, where the first two lines are the key:

\newif\ifcolored
\coloredtrue
\documentclass[10pt,a4paper]{article}
\usepackage[utf8x]{inputenc}
\usepackage{graphicx}
\usepackage{xparse}
\usepackage[x11names, svgnames]{xcolor}
\usepackage{fourier-orns}

\newcommand{\boldit}[1]{\textbf{\mathversion{bold}#1}}

\newbox\FBox
\NewDocumentCommand\boxit{O{black}O{white}mO{0.5pt}O{0pt}O{0pt}}{%
  \setlength\fboxsep{#4}\sbox\FBox{\fcolorbox{#1}{#2}{#3\rule[-#5]{0pt}{#6}}}\usebox\FBox}

\ifcolored
  % The colored version.
  \newcommand{\warning}[1]{\textcolor{Red3}{\raisebox{\depth}{\danger}\boldit{\,#1}}}
  % The gray version.
\else
  \newcommand{\warning}[1]{\boxit[black!80][black!30]{\raisebox{\depth}{\danger}\boldit{\,#1}}}
\fi

That is, for the commands that depend on coloring, you have two versions. If you comment the second line, the "uncolored" version will be chosen.

(Note a slightly more efficient definition of \boldit.)

  • Thanks for this elegant solution and also for the amelioration of \boldit – projetmbc Nov 21 '11 at 21:26
4

The etoolbox package provides a "toggle" (or switch) which could be considered similar to a boolean true/false variable:

\newtoggle{coloured}

Now you can condition on the value of coloured using

\iftoggle{coloured}{<true>}{<false>}

At the beginning of your document (or within the document preamble, after defining the toggle coloured), you can use \toggletrue{coloured} to set the toggle "on" (to true), or \togglefalse{coloured} to set the toggle "off" (to false, the default starting condition).

In your context, depending on the toggle setting, you could use

\iftoggle{coloured}{% true
   \newcommand{\warning}[1]{\textcolor{Red3}{\raisebox{\depth}{\danger}\boldit{\,#1}}}%
}{% false
   \newcommand{\warning}[1]{\boxit[black!80][black!30]{\raisebox{\depth}{\danger}\boldit{\,#1}}}%
}

This is similar to defining your own if command - see LaTeX conditional expression.

3

There are many ways to achieve this. For example, consider adding to your document

\newif\ifcolored
\input{config.cfg}

Here the file config.cfg has one line: either \coloredfalse or \coloredtrue

Then you can say

\ifcolored
   \newcommand{\warning}[1]{\textcolor{Red3}{\raisebox{\depth}{\danger}\boldit{\,#1}}}
\else
   \newcommand{\warning}[1]{\boxit[black!80][black!30]{\raisebox{\depth}{\danger}\boldit{\,#1}}}
 \fi

etc.

By changing the line in config.cfg you change the option in effect.

  • This solution is very intersting for me because my LaTeX files will be produced using one Python script. – projetmbc Nov 21 '11 at 21:15
2

As mentioned by the other answyers, there are numerous ways to do Conditional typesetting / build, but if you want to be able to specify which version to produce on the command line, I would use a \ifdefined. By default, this will produce a color version. If you want the black and white version you can just say:

pdflatex "\def\usebw{}\input{file.tex}

And in the file you would simply use:

\ifdefined\usebw
    \newcommand{\warning}[1]{\boxit[black!80][black!30]{\raisebox{\depth}{\danger}\boldit{\,#1}}}
\else
    \newcommand{\warning}[1]{\textcolor{Red3}{\raisebox{\depth}{\danger}\boldit{\,#1}}}
\fi
2

As mentioned in my comment, you can also use ifthen. Here is a simple code:

\documentclass[10pt,a4paper]{article}
    \usepackage[utf8x]{inputenc}
%   \usepackage{ucs}
    \usepackage{graphicx}
    \usepackage{xparse}
    \usepackage[x11names, svgnames]{xcolor}
    \usepackage{ifthen}
%    \usepackage{fourier-orns}

    \newcommand{\boldit}[1]{%
        \mathversion{bold}\textbf{#1}\mathversion{normal} %
    }

\def\isBW{false}

    \newbox\FBox
    \NewDocumentCommand\boxit{O{black}O{white}mO{0.5pt}O{0pt}O{0pt}}{%
        \setlength\fboxsep{#4}\sbox\FBox{\fcolorbox{#1}{#2}{#3\rule[-#5]{0pt}{#6}}}\usebox\FBox}

\ifthenelse{\equal{\isBW}{false}}{%
    \newcommand{\warning}[1]{\textcolor{Red3}{\boldit{\,#1}}}
    }
% The gray version.
    {%
    \newcommand{\warning}[1]{\boxit[black!80][black!30]{\boldit{\,#1}}}
    }


\begin{document}

Let's try one example... \warning{Danger !}

\end{document}

Although I must admit that there are several more elegant answers already posted by others. :)

  • Thansk for this proposition. – projetmbc Nov 21 '11 at 21:21

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.