0

What's wrong with the syntax of the \if...\else \if...\else...\fi below?

My intention is that if either OptA is true, then \which is defined to be Chose A, or if instead OptB is true, then \which is defined to be Chose B, or else if neither is true, then \which is defined to be Chose other.

But despite setting OptA to have value true, I still find that \which produces Chose other.

  \documentclass{article}
  
  \usepackage{ifthen}
  \newboolean{OptA}
  \newboolean{OptB}

  \setboolean{OptA}{true}
        
  \ifOptA
    \newcommand\which{Chose A} 
  \else
    \ifOptB
        \newcommand\which{Chose B}
  \else
    \newcommand\which{Chose other}
  \fi
     
  \begin{document}
  \which
  \end{document}
5
  • 1
    \newcommand\which} will give an error on } Nov 18, 2023 at 21:48
  • 1
    you are also missing a closing \fi Nov 18, 2023 at 21:49
  • 1
    the setting of optA has no effect on anything as you never test its value after it is set. Nov 18, 2023 at 21:51
  • Sorry for the original typo. And in abstracting from my actual code to this simplfied example, I forgot that the \setboolean does indeed come before the entire \if... My actual error, then, was forgetting the second \fi. I've edited my MWE so it leaves only the missing 2nd \fi.
    – murray
    Nov 18, 2023 at 21:58
  • After the edit you changed the warning to an error ! Incomplete \iftrue; all text was ignored after line 11. and it makes no pdf output at all, you do not get Chose other Nov 18, 2023 at 22:10

1 Answer 1

0

The document produces the warning

(\end occurred when \iffalse on line 7 was incomplete)

as the outer \ifOpA is never closed.

Also

\newcommand\which}

would generate an error if executed due to the spurious }

Fixing both of those produces

\documentclass{article}
  
  \usepackage{ifthen}
  \newboolean{OptA}
  \newboolean{OptB}
  
  \ifOptA
    \newcommand\which{Chose A} 
  \else
    \ifOptB
        \newcommand\which{Chose B}
    \else
        \newcommand\which{Chose other}
    \fi
  \fi
  
  \setboolean{OptA}{true}
  
  \begin{document}
  \which
  \end{document}

enter image description here

The setting of optA obviously has no effect if you set it after the test, so moving the setting earlier:

 \documentclass{article}
  
  \usepackage{ifthen}
  \newboolean{OptA}
  \newboolean{OptB}
  
  \setboolean{OptA}{true}

  \ifOptA
    \newcommand\which{Chose A} 
  \else
    \ifOptB
        \newcommand\which{Chose B}
    \else
        \newcommand\which{Chose other}
    \fi
  \fi
  
 
  
  \begin{document}
  \which
  \end{document}

produces

enter image description here

3
  • Please see my comment to the OP and my edit in the question so as to show my intended code.
    – murray
    Nov 18, 2023 at 22:01
  • @murray OK but this answer still covers it I think, just adding the \fi the other things removing the } and moving the setting you did in the edit. Nov 18, 2023 at 22:05
  • Yes, thank you!
    – murray
    Nov 18, 2023 at 22:11

You must log in to answer this question.

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