Testing for a “if not defined” boolean

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












How can I test for a not defined boolean?



I want to have



usepackagexr-hyper
usepackagehyperref

provideboolis_single_book
booltrueis_single_book


in my main.tex and in each of the chapter TeX files.



if not defined is_single_book
externaldocumentvolume1[http://mybook.com/volume1.pdf]
fi


So when I compile the entire book, externaldocumentvolume1[http://mybook.com/volume1.pdf] isn't executed.



It doesn't have to be a boolean if something else is simpler or doesn't require an extra package.







share|improve this question






















  • Why do you want "is not defined" rather than "true" and "false"? Does a package specify that boolean?
    – TeXnician
    Aug 16 at 11:06







  • 3




    So you essentially want a three-way switch: boolean undefined, boolean true and boolean false? Or can you simplify this to a simple two-valued logic with a true and false boolean? Since you use etoolbox commands you can use ifcsundefifis_single_book<undef><def> to check if the boolean is_single_book is undefined. Note that providebool makes sure the boolean is defined, so ifcsundefifis_single_book will execute the false branch.
    – moewe
    Aug 16 at 11:06















up vote
3
down vote

favorite












How can I test for a not defined boolean?



I want to have



usepackagexr-hyper
usepackagehyperref

provideboolis_single_book
booltrueis_single_book


in my main.tex and in each of the chapter TeX files.



if not defined is_single_book
externaldocumentvolume1[http://mybook.com/volume1.pdf]
fi


So when I compile the entire book, externaldocumentvolume1[http://mybook.com/volume1.pdf] isn't executed.



It doesn't have to be a boolean if something else is simpler or doesn't require an extra package.







share|improve this question






















  • Why do you want "is not defined" rather than "true" and "false"? Does a package specify that boolean?
    – TeXnician
    Aug 16 at 11:06







  • 3




    So you essentially want a three-way switch: boolean undefined, boolean true and boolean false? Or can you simplify this to a simple two-valued logic with a true and false boolean? Since you use etoolbox commands you can use ifcsundefifis_single_book<undef><def> to check if the boolean is_single_book is undefined. Note that providebool makes sure the boolean is defined, so ifcsundefifis_single_book will execute the false branch.
    – moewe
    Aug 16 at 11:06













up vote
3
down vote

favorite









up vote
3
down vote

favorite











How can I test for a not defined boolean?



I want to have



usepackagexr-hyper
usepackagehyperref

provideboolis_single_book
booltrueis_single_book


in my main.tex and in each of the chapter TeX files.



if not defined is_single_book
externaldocumentvolume1[http://mybook.com/volume1.pdf]
fi


So when I compile the entire book, externaldocumentvolume1[http://mybook.com/volume1.pdf] isn't executed.



It doesn't have to be a boolean if something else is simpler or doesn't require an extra package.







share|improve this question














How can I test for a not defined boolean?



I want to have



usepackagexr-hyper
usepackagehyperref

provideboolis_single_book
booltrueis_single_book


in my main.tex and in each of the chapter TeX files.



if not defined is_single_book
externaldocumentvolume1[http://mybook.com/volume1.pdf]
fi


So when I compile the entire book, externaldocumentvolume1[http://mybook.com/volume1.pdf] isn't executed.



It doesn't have to be a boolean if something else is simpler or doesn't require an extra package.









share|improve this question













share|improve this question




share|improve this question








edited Aug 16 at 19:14









Peter Mortensen

48336




48336










asked Aug 16 at 11:04









Sandra Schlichting

294410




294410











  • Why do you want "is not defined" rather than "true" and "false"? Does a package specify that boolean?
    – TeXnician
    Aug 16 at 11:06







  • 3




    So you essentially want a three-way switch: boolean undefined, boolean true and boolean false? Or can you simplify this to a simple two-valued logic with a true and false boolean? Since you use etoolbox commands you can use ifcsundefifis_single_book<undef><def> to check if the boolean is_single_book is undefined. Note that providebool makes sure the boolean is defined, so ifcsundefifis_single_book will execute the false branch.
    – moewe
    Aug 16 at 11:06

















  • Why do you want "is not defined" rather than "true" and "false"? Does a package specify that boolean?
    – TeXnician
    Aug 16 at 11:06







  • 3




    So you essentially want a three-way switch: boolean undefined, boolean true and boolean false? Or can you simplify this to a simple two-valued logic with a true and false boolean? Since you use etoolbox commands you can use ifcsundefifis_single_book<undef><def> to check if the boolean is_single_book is undefined. Note that providebool makes sure the boolean is defined, so ifcsundefifis_single_book will execute the false branch.
    – moewe
    Aug 16 at 11:06
















Why do you want "is not defined" rather than "true" and "false"? Does a package specify that boolean?
– TeXnician
Aug 16 at 11:06





Why do you want "is not defined" rather than "true" and "false"? Does a package specify that boolean?
– TeXnician
Aug 16 at 11:06





3




3




So you essentially want a three-way switch: boolean undefined, boolean true and boolean false? Or can you simplify this to a simple two-valued logic with a true and false boolean? Since you use etoolbox commands you can use ifcsundefifis_single_book<undef><def> to check if the boolean is_single_book is undefined. Note that providebool makes sure the boolean is defined, so ifcsundefifis_single_book will execute the false branch.
– moewe
Aug 16 at 11:06





So you essentially want a three-way switch: boolean undefined, boolean true and boolean false? Or can you simplify this to a simple two-valued logic with a true and false boolean? Since you use etoolbox commands you can use ifcsundefifis_single_book<undef><def> to check if the boolean is_single_book is undefined. Note that providebool makes sure the boolean is defined, so ifcsundefifis_single_book will execute the false branch.
– moewe
Aug 16 at 11:06











3 Answers
3






active

oldest

votes

















up vote
6
down vote



accepted










While you could check if a bool is defined by using ifcsundefif... (see comment from moewe), it looks as if you actually want a boolean which is always false if you are in a chapter tex file.



This can be archived by using providebool, which defines the bool as false, iff is is not defined already:



Use



newboolis_single_book
booltrueis_single_book


in your main file and



provideboolis_single_book
ifboolis_single_book%
externaldocumentvolume1[http://mybook.com/volume1.pdf]%



in your chapter files.



Even better would be to use toggles. They are almost the same but avoid name clashes and provide slightly better performance:



To use them, insert



newtoggleis_single_book
toggletrueis_single_book


in your main file and



providetoggleis_single_book
iftoggleis_single_book%
externaldocumentvolume1[http://mybook.com/volume1.pdf]%



in the chapters.






share|improve this answer




















  • +1 for toggles.
    – moewe
    Aug 16 at 11:28










  • Something seams to be wrong for the chapters part. externaldocumentvolume1[http://mybook.com/volume1.pdf] isn't being executed. Is is_single_book true or fasle right after providetoggleis_single_book?
    – Sandra Schlichting
    Aug 16 at 12:34










  • externaldocumentvolume1[http://mybook.com/volume1.pdf] should be executed if is_single_book is false or undefined.
    – Sandra Schlichting
    Aug 16 at 12:37

















up vote
6
down vote













providebool and booltrue makes me think you are using etoolbox.



etoolbox booleans are just a thin wrapper around TeX's newif.



That means that newbool<name> defines if<name>, so you can check if a boolean is defined by checking if if<name> is defined.



documentclass[british]article
usepackage[T1]fontenc
usepackage[utf8]inputenc
usepackagebabel
usepackageetoolbox

newcommand*ifboolundef[1]ifcsundefif#1

newcommand*sabooleantest[1]%
bool ttfamily#1 is
ifboolundef#1
undefined
ifbool#1
true
false

begindocument
sabooleantestflobbel

newboolglobbel
boolfalseglobbel
sabooleantestglobbel

newboolhobbel
booltruehobbel
sabooleantesthobbel
enddocument



bool flobbel is undefined



bool globbel is false



bool hobbel is true




The question is whether you really need something that comes down to a three-valued logic. Is a two-valued boolean not enough?



If you want to avoid problems in case the boolean is undefined, you could just check if a macro is undefined or defined. This also has two states, but unlike with booleans there are no errors if the thing is not defined.



newcommand*mybool
ifcsundefmybool
not defined
defined


You could also define 'permissive toggles' that are false if they don't exist



documentclass[british]article
usepackage[T1]fontenc
usepackage[utf8]inputenc
usepackagebabel
usepackageetoolbox

newcommand*permissivetoggletrue[1]csletcssa@ptogl@#1@empty
newcommand*permissivetogglefalse[1]csundefsa@ptogl@#1
newcommand*ifpermissivetoggle[1]ifcsdefsa@ptogl@#1

begindocument
flobbel
ifpermissivetoggleflobbel
true
false

permissivetoggletrueglobbel
globbel
ifpermissivetoggleglobbel
true
false
enddocument


This can also be done without etoolbox if you like those sort of things



makeatletter
newcommand*permissivetoggletrue[1]%
expandafterletcsname sa@ptogl@#1endcsname@empty
newcommand*permissivetogglefalse[1]%
expandafterletcsname sa@ptogl@#1endcsname@sa@undef% this assumes @sa@undef is undefined
newcommand*ifpermissivetoggle[1]%
ifcsname sa@ptogl@#1endcsname
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
makeatother





share|improve this answer






















  • I were only using etoolbox for this one thing, so if this package can be avoided, then it would be much appreciated.
    – Sandra Schlichting
    Aug 16 at 11:34






  • 2




    @SandraSchlichting Theoretically especially in my last two examples we could do away with etoolbox, but I would end up essentially copying (a bit of) code from that package, so why not load it?
    – moewe
    Aug 16 at 11:38










  • I see. Good point.
    – Sandra Schlichting
    Aug 16 at 11:42

















up vote
0
down vote














It doesn't have to be a boolean, if something else is simpler or doesn't require an extra package.




I think ifdefined and def provide what you're looking for. You don't need an extra package or any definition.



In your main document, write



defisSingleBook


or, alternatively, newcommandisSingleBook or providecommandisSingleBook.



Then you can check with:



ifdefinedisSingleBook
%% you're in the book
else
%% you're not in the book
fi





share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "85"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f446273%2ftesting-for-a-if-not-defined-boolean%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    6
    down vote



    accepted










    While you could check if a bool is defined by using ifcsundefif... (see comment from moewe), it looks as if you actually want a boolean which is always false if you are in a chapter tex file.



    This can be archived by using providebool, which defines the bool as false, iff is is not defined already:



    Use



    newboolis_single_book
    booltrueis_single_book


    in your main file and



    provideboolis_single_book
    ifboolis_single_book%
    externaldocumentvolume1[http://mybook.com/volume1.pdf]%



    in your chapter files.



    Even better would be to use toggles. They are almost the same but avoid name clashes and provide slightly better performance:



    To use them, insert



    newtoggleis_single_book
    toggletrueis_single_book


    in your main file and



    providetoggleis_single_book
    iftoggleis_single_book%
    externaldocumentvolume1[http://mybook.com/volume1.pdf]%



    in the chapters.






    share|improve this answer




















    • +1 for toggles.
      – moewe
      Aug 16 at 11:28










    • Something seams to be wrong for the chapters part. externaldocumentvolume1[http://mybook.com/volume1.pdf] isn't being executed. Is is_single_book true or fasle right after providetoggleis_single_book?
      – Sandra Schlichting
      Aug 16 at 12:34










    • externaldocumentvolume1[http://mybook.com/volume1.pdf] should be executed if is_single_book is false or undefined.
      – Sandra Schlichting
      Aug 16 at 12:37














    up vote
    6
    down vote



    accepted










    While you could check if a bool is defined by using ifcsundefif... (see comment from moewe), it looks as if you actually want a boolean which is always false if you are in a chapter tex file.



    This can be archived by using providebool, which defines the bool as false, iff is is not defined already:



    Use



    newboolis_single_book
    booltrueis_single_book


    in your main file and



    provideboolis_single_book
    ifboolis_single_book%
    externaldocumentvolume1[http://mybook.com/volume1.pdf]%



    in your chapter files.



    Even better would be to use toggles. They are almost the same but avoid name clashes and provide slightly better performance:



    To use them, insert



    newtoggleis_single_book
    toggletrueis_single_book


    in your main file and



    providetoggleis_single_book
    iftoggleis_single_book%
    externaldocumentvolume1[http://mybook.com/volume1.pdf]%



    in the chapters.






    share|improve this answer




















    • +1 for toggles.
      – moewe
      Aug 16 at 11:28










    • Something seams to be wrong for the chapters part. externaldocumentvolume1[http://mybook.com/volume1.pdf] isn't being executed. Is is_single_book true or fasle right after providetoggleis_single_book?
      – Sandra Schlichting
      Aug 16 at 12:34










    • externaldocumentvolume1[http://mybook.com/volume1.pdf] should be executed if is_single_book is false or undefined.
      – Sandra Schlichting
      Aug 16 at 12:37












    up vote
    6
    down vote



    accepted







    up vote
    6
    down vote



    accepted






    While you could check if a bool is defined by using ifcsundefif... (see comment from moewe), it looks as if you actually want a boolean which is always false if you are in a chapter tex file.



    This can be archived by using providebool, which defines the bool as false, iff is is not defined already:



    Use



    newboolis_single_book
    booltrueis_single_book


    in your main file and



    provideboolis_single_book
    ifboolis_single_book%
    externaldocumentvolume1[http://mybook.com/volume1.pdf]%



    in your chapter files.



    Even better would be to use toggles. They are almost the same but avoid name clashes and provide slightly better performance:



    To use them, insert



    newtoggleis_single_book
    toggletrueis_single_book


    in your main file and



    providetoggleis_single_book
    iftoggleis_single_book%
    externaldocumentvolume1[http://mybook.com/volume1.pdf]%



    in the chapters.






    share|improve this answer












    While you could check if a bool is defined by using ifcsundefif... (see comment from moewe), it looks as if you actually want a boolean which is always false if you are in a chapter tex file.



    This can be archived by using providebool, which defines the bool as false, iff is is not defined already:



    Use



    newboolis_single_book
    booltrueis_single_book


    in your main file and



    provideboolis_single_book
    ifboolis_single_book%
    externaldocumentvolume1[http://mybook.com/volume1.pdf]%



    in your chapter files.



    Even better would be to use toggles. They are almost the same but avoid name clashes and provide slightly better performance:



    To use them, insert



    newtoggleis_single_book
    toggletrueis_single_book


    in your main file and



    providetoggleis_single_book
    iftoggleis_single_book%
    externaldocumentvolume1[http://mybook.com/volume1.pdf]%



    in the chapters.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Aug 16 at 11:19









    Marcel Krüger

    10.4k11032




    10.4k11032











    • +1 for toggles.
      – moewe
      Aug 16 at 11:28










    • Something seams to be wrong for the chapters part. externaldocumentvolume1[http://mybook.com/volume1.pdf] isn't being executed. Is is_single_book true or fasle right after providetoggleis_single_book?
      – Sandra Schlichting
      Aug 16 at 12:34










    • externaldocumentvolume1[http://mybook.com/volume1.pdf] should be executed if is_single_book is false or undefined.
      – Sandra Schlichting
      Aug 16 at 12:37
















    • +1 for toggles.
      – moewe
      Aug 16 at 11:28










    • Something seams to be wrong for the chapters part. externaldocumentvolume1[http://mybook.com/volume1.pdf] isn't being executed. Is is_single_book true or fasle right after providetoggleis_single_book?
      – Sandra Schlichting
      Aug 16 at 12:34










    • externaldocumentvolume1[http://mybook.com/volume1.pdf] should be executed if is_single_book is false or undefined.
      – Sandra Schlichting
      Aug 16 at 12:37















    +1 for toggles.
    – moewe
    Aug 16 at 11:28




    +1 for toggles.
    – moewe
    Aug 16 at 11:28












    Something seams to be wrong for the chapters part. externaldocumentvolume1[http://mybook.com/volume1.pdf] isn't being executed. Is is_single_book true or fasle right after providetoggleis_single_book?
    – Sandra Schlichting
    Aug 16 at 12:34




    Something seams to be wrong for the chapters part. externaldocumentvolume1[http://mybook.com/volume1.pdf] isn't being executed. Is is_single_book true or fasle right after providetoggleis_single_book?
    – Sandra Schlichting
    Aug 16 at 12:34












    externaldocumentvolume1[http://mybook.com/volume1.pdf] should be executed if is_single_book is false or undefined.
    – Sandra Schlichting
    Aug 16 at 12:37




    externaldocumentvolume1[http://mybook.com/volume1.pdf] should be executed if is_single_book is false or undefined.
    – Sandra Schlichting
    Aug 16 at 12:37










    up vote
    6
    down vote













    providebool and booltrue makes me think you are using etoolbox.



    etoolbox booleans are just a thin wrapper around TeX's newif.



    That means that newbool<name> defines if<name>, so you can check if a boolean is defined by checking if if<name> is defined.



    documentclass[british]article
    usepackage[T1]fontenc
    usepackage[utf8]inputenc
    usepackagebabel
    usepackageetoolbox

    newcommand*ifboolundef[1]ifcsundefif#1

    newcommand*sabooleantest[1]%
    bool ttfamily#1 is
    ifboolundef#1
    undefined
    ifbool#1
    true
    false

    begindocument
    sabooleantestflobbel

    newboolglobbel
    boolfalseglobbel
    sabooleantestglobbel

    newboolhobbel
    booltruehobbel
    sabooleantesthobbel
    enddocument



    bool flobbel is undefined



    bool globbel is false



    bool hobbel is true




    The question is whether you really need something that comes down to a three-valued logic. Is a two-valued boolean not enough?



    If you want to avoid problems in case the boolean is undefined, you could just check if a macro is undefined or defined. This also has two states, but unlike with booleans there are no errors if the thing is not defined.



    newcommand*mybool
    ifcsundefmybool
    not defined
    defined


    You could also define 'permissive toggles' that are false if they don't exist



    documentclass[british]article
    usepackage[T1]fontenc
    usepackage[utf8]inputenc
    usepackagebabel
    usepackageetoolbox

    newcommand*permissivetoggletrue[1]csletcssa@ptogl@#1@empty
    newcommand*permissivetogglefalse[1]csundefsa@ptogl@#1
    newcommand*ifpermissivetoggle[1]ifcsdefsa@ptogl@#1

    begindocument
    flobbel
    ifpermissivetoggleflobbel
    true
    false

    permissivetoggletrueglobbel
    globbel
    ifpermissivetoggleglobbel
    true
    false
    enddocument


    This can also be done without etoolbox if you like those sort of things



    makeatletter
    newcommand*permissivetoggletrue[1]%
    expandafterletcsname sa@ptogl@#1endcsname@empty
    newcommand*permissivetogglefalse[1]%
    expandafterletcsname sa@ptogl@#1endcsname@sa@undef% this assumes @sa@undef is undefined
    newcommand*ifpermissivetoggle[1]%
    ifcsname sa@ptogl@#1endcsname
    expandafter@firstoftwo
    else
    expandafter@secondoftwo
    fi
    makeatother





    share|improve this answer






















    • I were only using etoolbox for this one thing, so if this package can be avoided, then it would be much appreciated.
      – Sandra Schlichting
      Aug 16 at 11:34






    • 2




      @SandraSchlichting Theoretically especially in my last two examples we could do away with etoolbox, but I would end up essentially copying (a bit of) code from that package, so why not load it?
      – moewe
      Aug 16 at 11:38










    • I see. Good point.
      – Sandra Schlichting
      Aug 16 at 11:42














    up vote
    6
    down vote













    providebool and booltrue makes me think you are using etoolbox.



    etoolbox booleans are just a thin wrapper around TeX's newif.



    That means that newbool<name> defines if<name>, so you can check if a boolean is defined by checking if if<name> is defined.



    documentclass[british]article
    usepackage[T1]fontenc
    usepackage[utf8]inputenc
    usepackagebabel
    usepackageetoolbox

    newcommand*ifboolundef[1]ifcsundefif#1

    newcommand*sabooleantest[1]%
    bool ttfamily#1 is
    ifboolundef#1
    undefined
    ifbool#1
    true
    false

    begindocument
    sabooleantestflobbel

    newboolglobbel
    boolfalseglobbel
    sabooleantestglobbel

    newboolhobbel
    booltruehobbel
    sabooleantesthobbel
    enddocument



    bool flobbel is undefined



    bool globbel is false



    bool hobbel is true




    The question is whether you really need something that comes down to a three-valued logic. Is a two-valued boolean not enough?



    If you want to avoid problems in case the boolean is undefined, you could just check if a macro is undefined or defined. This also has two states, but unlike with booleans there are no errors if the thing is not defined.



    newcommand*mybool
    ifcsundefmybool
    not defined
    defined


    You could also define 'permissive toggles' that are false if they don't exist



    documentclass[british]article
    usepackage[T1]fontenc
    usepackage[utf8]inputenc
    usepackagebabel
    usepackageetoolbox

    newcommand*permissivetoggletrue[1]csletcssa@ptogl@#1@empty
    newcommand*permissivetogglefalse[1]csundefsa@ptogl@#1
    newcommand*ifpermissivetoggle[1]ifcsdefsa@ptogl@#1

    begindocument
    flobbel
    ifpermissivetoggleflobbel
    true
    false

    permissivetoggletrueglobbel
    globbel
    ifpermissivetoggleglobbel
    true
    false
    enddocument


    This can also be done without etoolbox if you like those sort of things



    makeatletter
    newcommand*permissivetoggletrue[1]%
    expandafterletcsname sa@ptogl@#1endcsname@empty
    newcommand*permissivetogglefalse[1]%
    expandafterletcsname sa@ptogl@#1endcsname@sa@undef% this assumes @sa@undef is undefined
    newcommand*ifpermissivetoggle[1]%
    ifcsname sa@ptogl@#1endcsname
    expandafter@firstoftwo
    else
    expandafter@secondoftwo
    fi
    makeatother





    share|improve this answer






















    • I were only using etoolbox for this one thing, so if this package can be avoided, then it would be much appreciated.
      – Sandra Schlichting
      Aug 16 at 11:34






    • 2




      @SandraSchlichting Theoretically especially in my last two examples we could do away with etoolbox, but I would end up essentially copying (a bit of) code from that package, so why not load it?
      – moewe
      Aug 16 at 11:38










    • I see. Good point.
      – Sandra Schlichting
      Aug 16 at 11:42












    up vote
    6
    down vote










    up vote
    6
    down vote









    providebool and booltrue makes me think you are using etoolbox.



    etoolbox booleans are just a thin wrapper around TeX's newif.



    That means that newbool<name> defines if<name>, so you can check if a boolean is defined by checking if if<name> is defined.



    documentclass[british]article
    usepackage[T1]fontenc
    usepackage[utf8]inputenc
    usepackagebabel
    usepackageetoolbox

    newcommand*ifboolundef[1]ifcsundefif#1

    newcommand*sabooleantest[1]%
    bool ttfamily#1 is
    ifboolundef#1
    undefined
    ifbool#1
    true
    false

    begindocument
    sabooleantestflobbel

    newboolglobbel
    boolfalseglobbel
    sabooleantestglobbel

    newboolhobbel
    booltruehobbel
    sabooleantesthobbel
    enddocument



    bool flobbel is undefined



    bool globbel is false



    bool hobbel is true




    The question is whether you really need something that comes down to a three-valued logic. Is a two-valued boolean not enough?



    If you want to avoid problems in case the boolean is undefined, you could just check if a macro is undefined or defined. This also has two states, but unlike with booleans there are no errors if the thing is not defined.



    newcommand*mybool
    ifcsundefmybool
    not defined
    defined


    You could also define 'permissive toggles' that are false if they don't exist



    documentclass[british]article
    usepackage[T1]fontenc
    usepackage[utf8]inputenc
    usepackagebabel
    usepackageetoolbox

    newcommand*permissivetoggletrue[1]csletcssa@ptogl@#1@empty
    newcommand*permissivetogglefalse[1]csundefsa@ptogl@#1
    newcommand*ifpermissivetoggle[1]ifcsdefsa@ptogl@#1

    begindocument
    flobbel
    ifpermissivetoggleflobbel
    true
    false

    permissivetoggletrueglobbel
    globbel
    ifpermissivetoggleglobbel
    true
    false
    enddocument


    This can also be done without etoolbox if you like those sort of things



    makeatletter
    newcommand*permissivetoggletrue[1]%
    expandafterletcsname sa@ptogl@#1endcsname@empty
    newcommand*permissivetogglefalse[1]%
    expandafterletcsname sa@ptogl@#1endcsname@sa@undef% this assumes @sa@undef is undefined
    newcommand*ifpermissivetoggle[1]%
    ifcsname sa@ptogl@#1endcsname
    expandafter@firstoftwo
    else
    expandafter@secondoftwo
    fi
    makeatother





    share|improve this answer














    providebool and booltrue makes me think you are using etoolbox.



    etoolbox booleans are just a thin wrapper around TeX's newif.



    That means that newbool<name> defines if<name>, so you can check if a boolean is defined by checking if if<name> is defined.



    documentclass[british]article
    usepackage[T1]fontenc
    usepackage[utf8]inputenc
    usepackagebabel
    usepackageetoolbox

    newcommand*ifboolundef[1]ifcsundefif#1

    newcommand*sabooleantest[1]%
    bool ttfamily#1 is
    ifboolundef#1
    undefined
    ifbool#1
    true
    false

    begindocument
    sabooleantestflobbel

    newboolglobbel
    boolfalseglobbel
    sabooleantestglobbel

    newboolhobbel
    booltruehobbel
    sabooleantesthobbel
    enddocument



    bool flobbel is undefined



    bool globbel is false



    bool hobbel is true




    The question is whether you really need something that comes down to a three-valued logic. Is a two-valued boolean not enough?



    If you want to avoid problems in case the boolean is undefined, you could just check if a macro is undefined or defined. This also has two states, but unlike with booleans there are no errors if the thing is not defined.



    newcommand*mybool
    ifcsundefmybool
    not defined
    defined


    You could also define 'permissive toggles' that are false if they don't exist



    documentclass[british]article
    usepackage[T1]fontenc
    usepackage[utf8]inputenc
    usepackagebabel
    usepackageetoolbox

    newcommand*permissivetoggletrue[1]csletcssa@ptogl@#1@empty
    newcommand*permissivetogglefalse[1]csundefsa@ptogl@#1
    newcommand*ifpermissivetoggle[1]ifcsdefsa@ptogl@#1

    begindocument
    flobbel
    ifpermissivetoggleflobbel
    true
    false

    permissivetoggletrueglobbel
    globbel
    ifpermissivetoggleglobbel
    true
    false
    enddocument


    This can also be done without etoolbox if you like those sort of things



    makeatletter
    newcommand*permissivetoggletrue[1]%
    expandafterletcsname sa@ptogl@#1endcsname@empty
    newcommand*permissivetogglefalse[1]%
    expandafterletcsname sa@ptogl@#1endcsname@sa@undef% this assumes @sa@undef is undefined
    newcommand*ifpermissivetoggle[1]%
    ifcsname sa@ptogl@#1endcsname
    expandafter@firstoftwo
    else
    expandafter@secondoftwo
    fi
    makeatother






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 16 at 13:18

























    answered Aug 16 at 11:28









    moewe

    74.8k797285




    74.8k797285











    • I were only using etoolbox for this one thing, so if this package can be avoided, then it would be much appreciated.
      – Sandra Schlichting
      Aug 16 at 11:34






    • 2




      @SandraSchlichting Theoretically especially in my last two examples we could do away with etoolbox, but I would end up essentially copying (a bit of) code from that package, so why not load it?
      – moewe
      Aug 16 at 11:38










    • I see. Good point.
      – Sandra Schlichting
      Aug 16 at 11:42
















    • I were only using etoolbox for this one thing, so if this package can be avoided, then it would be much appreciated.
      – Sandra Schlichting
      Aug 16 at 11:34






    • 2




      @SandraSchlichting Theoretically especially in my last two examples we could do away with etoolbox, but I would end up essentially copying (a bit of) code from that package, so why not load it?
      – moewe
      Aug 16 at 11:38










    • I see. Good point.
      – Sandra Schlichting
      Aug 16 at 11:42















    I were only using etoolbox for this one thing, so if this package can be avoided, then it would be much appreciated.
    – Sandra Schlichting
    Aug 16 at 11:34




    I were only using etoolbox for this one thing, so if this package can be avoided, then it would be much appreciated.
    – Sandra Schlichting
    Aug 16 at 11:34




    2




    2




    @SandraSchlichting Theoretically especially in my last two examples we could do away with etoolbox, but I would end up essentially copying (a bit of) code from that package, so why not load it?
    – moewe
    Aug 16 at 11:38




    @SandraSchlichting Theoretically especially in my last two examples we could do away with etoolbox, but I would end up essentially copying (a bit of) code from that package, so why not load it?
    – moewe
    Aug 16 at 11:38












    I see. Good point.
    – Sandra Schlichting
    Aug 16 at 11:42




    I see. Good point.
    – Sandra Schlichting
    Aug 16 at 11:42










    up vote
    0
    down vote














    It doesn't have to be a boolean, if something else is simpler or doesn't require an extra package.




    I think ifdefined and def provide what you're looking for. You don't need an extra package or any definition.



    In your main document, write



    defisSingleBook


    or, alternatively, newcommandisSingleBook or providecommandisSingleBook.



    Then you can check with:



    ifdefinedisSingleBook
    %% you're in the book
    else
    %% you're not in the book
    fi





    share|improve this answer
























      up vote
      0
      down vote














      It doesn't have to be a boolean, if something else is simpler or doesn't require an extra package.




      I think ifdefined and def provide what you're looking for. You don't need an extra package or any definition.



      In your main document, write



      defisSingleBook


      or, alternatively, newcommandisSingleBook or providecommandisSingleBook.



      Then you can check with:



      ifdefinedisSingleBook
      %% you're in the book
      else
      %% you're not in the book
      fi





      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote










        It doesn't have to be a boolean, if something else is simpler or doesn't require an extra package.




        I think ifdefined and def provide what you're looking for. You don't need an extra package or any definition.



        In your main document, write



        defisSingleBook


        or, alternatively, newcommandisSingleBook or providecommandisSingleBook.



        Then you can check with:



        ifdefinedisSingleBook
        %% you're in the book
        else
        %% you're not in the book
        fi





        share|improve this answer













        It doesn't have to be a boolean, if something else is simpler or doesn't require an extra package.




        I think ifdefined and def provide what you're looking for. You don't need an extra package or any definition.



        In your main document, write



        defisSingleBook


        or, alternatively, newcommandisSingleBook or providecommandisSingleBook.



        Then you can check with:



        ifdefinedisSingleBook
        %% you're in the book
        else
        %% you're not in the book
        fi






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 16 at 13:19









        Scz

        1,213512




        1,213512



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f446273%2ftesting-for-a-if-not-defined-boolean%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery