Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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
add comment

2 Answers

up vote 11 down vote accepted

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.

share|improve this answer
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
add comment
  • Your example doesn't implement a class so I'm assuming you're trying to implement a package.
  • If the user-defined-command doesn't rely on arguments, then it's more efficient if you define it using \edef.

In the following, I show how to implement the parsing with the pgfopts package, which makes pgfkeys-style option parsing available. I find it much easier to use in general than using other packages that provide key=value parsing. More information about pgfkeys may be found in the pgf manual.

\NeedsTeXFormat{LaTeX2e}[2009/09/24]
\ProvidesPackage{mypackage}[2011/12/30]

\makeatletter
% Define the main TeX switches
\newif\ifmypackage@final      
\newif\ifmypackage@draft      

\RequirePackage{pgfopts}
% Define the final and draft options. Both default to false.
\pgfkeys{/mypackage/.cd,                                    
         final/.is if=mypackage@final,                      
         draft/.is if=mypackage@draft,                      
         final=false,                                       
         draft=false}
\ProcessPgfPackageOptions{/mypackage}

\edef\myfinal@final@test{%
    \ifmypackage@draft%
        DRAFT and FINAL.%
    \else%
        FINAL only.%
    \fi%
}

\edef\myfinal@nonfinal@test{%
    \ifmypackage@draft%
        DRAFT only.
    \else%
        NOTHING, you lose.%
    \fi%
}

\edef\mytest{%
    \ifmypackage@final%
        \myfinal@final@test%
    \else%
        \myfinal@nonfinal@test%
    \fi%
}
\makeatother
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.