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}
projetmbc
  • 13,315

5 Answers5

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.

Werner
  • 603,163
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.)

egreg
  • 1,121,712
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.

Boris
  • 38,129
  • 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
Peter Grill
  • 223,288
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. :)

Count Zero
  • 17,424