2

\usepackage{ifthen}

%booleandeclaration
\newboolean{istest}
\setboolean{istest}{false}

%Set boolean in if
% \newcommand{\activate}[1]{%
% \ifnum#1>0{\setboolean{istest}{true}}\fi }

\newcommand{\activate}[1]{%
  \ifnum#1>0 \setboolean{istest}{true}\fi}


%test boolean of true or false
\newcommand{\test}{\ifthenelse{\boolean{istest}}{TRUE}{FALSE}}%

\activate{10}
\begin{document}
\test
\end{document}

How can I use true or false as args.

\activate{true}

This has to set istest to true.

2 Answers 2

3

Adaptations

  • Use \setboolean{istest}{<true|false>} directly
  • or put it into a command (\activate)

Note: You could also use other ways (bool or toggle, see package manual of etoolbox).

Code

\documentclass{article}

\usepackage{ifthen}

%booleandeclaration
\newboolean{istest}
\setboolean{istest}{false}

\newcommand{\activate}[1]{%
    \setboolean{istest}{#1}
}


%test boolean of true or false
\newcommand{\test}{\ifthenelse{\boolean{istest}}{TRUE}{FALSE}}%

\begin{document}
\activate{true}
\test
\activate{false}
\test
\end{document}
4

You can do even better: the \returnTorF command accepts an argument in the forms

  1. t, T, true, True, TRUE
  2. y, Y, yes, Yes, YES
  3. f, F, false, False, FALSE
  4. n, N, no, No, NO
  5. any integer

In cases 1 and 2, it returns true; in cases 3 and 4 it returns false (any capitalization, even TrUe is actually accepted); in case 5, true is returned if the integer is positive, false otherwise.

As an argument you can also use \value{<counter>}, which is a legal integer.

\documentclass{article}
\usepackage{ifthen}% but there are better methods

% we want to accept
%
% t, T, true, True, TRUE, y, yes, YES
%
% or
%
% f, F, false, False, FALSE, n, no, NO
%
% but also integers, with positive ones yielding true
% and nonpositive yielding false

\ExplSyntaxOn

\NewExpandableDocumentCommand{\returnTorF}{m}
 {
  \darkshadow_tf:n { #1 }
 }

\cs_new:Nn \darkshadow_tf:n
 {
  \str_case_e:nnF { \str_casefold:n { #1 } }
   {
    {t}{true}
    {y}{true}
    {true}{true}
    {yes}{true}
    {f}{false}
    {n}{false}
    {false}{false}
    {no}{false}
   }
   { \int_compare:nTF { #1 > 0 } { true } { false } }
 }

\ExplSyntaxOff

\newboolean{istest}
\setboolean{istest}{false}

\newcommand{\activate}[1]{\setboolean{istest}{\returnTorF{#1}}}

\newcommand{\test}{\ifthenelse{\boolean{istest}}{TRUE}{FALSE}}

\begin{document}

\test\ (false)\par
\activate{y}
\test\ (true)\par
\activate{NO}
\test\ (false)\par
\activate{true}
\test\ (true)\par
\activate{0}
\test\ (false)\par
\activate{2}
\test\ (true)\par

\end{document}

enter image description here

1
  • Whether it is better to allow a variety of inputs that mean the same thing certainly depends on the use case. I can think offhand of 2 disadvantages compared to explicit input: Unnecessary conflicts in projects with multiple people if one boolean is changed in different ways each time. It complicates the search (needs regex) if you want to find where the boolean was set to true (or false).
    – dexteritas
    Commented Feb 16, 2023 at 13:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .