7

I am using conditional tests in my .cls file which has two options "final" and "draft". I am rather familiar with conditional tests but I am just curious to know how to define the "or" in the following code.

\if@‎final OR ‎\if@draft FINAL TEST ‎‎ ‎\else NO TEST‎ ‎\fi\fi

Edit:

I have tried the following code and have gotten the desired result, so I just want to know whether I CAN use something like "\or" in \newif conditional or not. In other word, does such a command (I mean \or) exist at all?

‎\documentclass[‎draft‎]‎{article}‎
‎\makeatletter‎
‎‎\newif\if@‎final‎
‎\@‎final‎false‎
‎\DeclareOption{‎final‎}{\@‎final‎true}‎
‎\newif\if@‎draft‎‎
‎\@‎draft‎false‎
‎\DeclareOption{‎draft‎}{\@‎draft‎true}‎‎
‎‎\ProcessOptions‎‎‎
‎\newcommand{‎\mytest‎}‎{\if@‎final FINAL TEST ‎\else‎  ‎\if@draft FINAL TEST ‎‎ ‎\else ‎NO ‎TEST‎ ‎\fi\fi‎‎‎‎‎}‎‎
‎‎\makeatother‎
‎\begin{document}‎
‎\mytest‎
‎‎\end{document}
  • Do you have different outputs based on whether you're final or draft, since your faux if statement doesn't show it? – Werner Dec 29 '11 at 22:24
  • In both cases, I want to have "FINAL TEST" on my output. – Vahid Damanafshan Dec 29 '11 at 22:31
  • 1
    Yes, \or exists, but is not used in the context of boolean expressions like you might be used to. Instead it is used as a choice conditioning when using \ifcase. See some examples of \or in the TeX Primitive Control Sequence Reference. – Werner Dec 29 '11 at 23:56
11

The basis for your OR statement looks like this:

\if@final TRUE\else\if@draft TRUE\else FALSE\fi\fi

The following minimal example illustrates this:

\documentclass{article}
\begin{document}
\makeatletter
\newif\if@final \newif\if@draft
\newcommand{\test}{\if@final TRUE\else\if@draft TRUE\else FALSE\fi\fi}
% final = false; draft = false
\@finalfalse \@draftfalse \test

% final = true; draft = false
\@finaltrue \@draftfalse \test

% final = false; draft = true
\@finalfalse \@drafttrue \test

% final = true; draft = true
\@finaltrue \@drafttrue \test
\end{document}

yielding the OR truth table:

enter image description here

Comparable output is obtained by using booleans defined via etoolbox:

\documentclass{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\begin{document}
\makeatletter
%\newif\if@final \newif\if@draft
\newbool{@final} \newbool{@draft}
\newcommand{\test}{\ifboolexpr{bool {@final} or bool {@draft}}{TRUE}{FALSE}}
% final = false; draft = false
\boolfalse{@final} \boolfalse{@draft} \test

% final = true; draft = false
\booltrue{@final} \boolfalse{@draft} \test

% final = false; draft = true
\boolfalse{@final} \booltrue{@draft} \test

% final = true; draft = true
\booltrue{@final} \booltrue{@draft} \test
\end{document}

Of course, you can change the TRUE/FALSE output to whatever you want.


As Marco remarked, etoolbox's boolean flags provide a front-end to LaTeX's \newif. As such you can either define them using \newbool or \newif. See section 3.5 Boolean Flags (p 12 onward) of the etoolbox documentation.

  • 1
    Since \ifboolexpr and \ifboolexpe imply processing overhead, there is generally no benefit in employing them for a single/simple test. etoolbox documentation. – Ahmed Musa Dec 30 '11 at 8:35

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.