2

I have a LaTeX document where I have defined a switch case command using LaTeX3 syntax. I am using the case inside the \equal condition of an \ifthenelse command. However, I keep getting the wrong result; seems to me that inside the \equal, the switch case cannot be expandend.

What I am doing wrong? In the MWE below I am expecting "Function is A :)" but got "Function is B :(":

\documentclass{book}
\usepackage{xparse}
\usepackage{ifthen}

\ExplSyntaxOn
\newcommand\caseswitch[2]{
  \str_case_e:nn{#1}{#2}
}
\ExplSyntaxOff

\newcommand\mycommand[1]{
  \caseswitch{#1}{
    {A}{Aaa}
    {B}{Bbb}
  }
}

\begin{document}

% expected "Function is A :)" but got "Function is B :("
\ifthenelse{\equal{\mycommand{A}}{Aaa}}{Function is A :)}{Function is B :(}

% another version - does not work too
%\caseswitch{\mycommand{A}}{{Aaa}{Function is A :)}{Bbb}{Function is B :(}}

\end{document}
3
  • as egreg just posted.you have spaces from end of line apart from that it is a bit weird to use \ifthenelse when, as you show, expl3 provides far better control constructs. What is the use case here? Commented Nov 17, 2022 at 23:51
  • In that case would be special case of tex.stackexchange.com/questions/7453/… then. -- that having said, in order to debug this one, apart from "staring at the code" I suggest \tl_show:x{\mycommand{A}}, \cs_generate_variant:Nn \tl_analysis_show:n{x} \tl_analysis_show:x{\mycommand{A}} which will print out the blank spaces.
    – user202729
    Commented Nov 17, 2022 at 23:53
  • (personally I have a local package for debugging which prints out token lists like this in this case so that you'll be aware of both whether there are spaces in the code and the catcodes of the tokens in the token list, without being unnecessarily verbose like \tl_analysis_show:n which is frustrating to read)
    – user202729
    Commented Nov 17, 2022 at 23:55

2 Answers 2

3

An interesting case of the spurious space syndrome: you're comparing Aaa with  Aaa  (with two spaces around the string).

\documentclass{book}
\usepackage{xparse}
\usepackage{ifthen}

\ExplSyntaxOn
\newcommand\caseswitch[2]{
  \str_case_e:nn{#1}{#2}
}
\ExplSyntaxOff

\newcommand\mycommand[1]{% <--- HERE
  \caseswitch{#1}{
    {A}{Aaa}
    {B}{Bbb}
  }% <--- HERE
}

\begin{document}

% expected "Function is A :)" but got "Function is B :("
\ifthenelse{\equal{\mycommand{A}}{Aaa}}{Function is A :)}{Function is B :(}

% another version - does not work too
\caseswitch{\mycommand{A}}{{Aaa}{Function is A :)}{Bbb}{Function is B :(}}

\end{document}

enter image description here

I'd use the simpler

\ExplSyntaxOn
\cs_set_eq:NN \caseswitch \str_case_e:nn
\ExplSyntaxOff

and also the reimplementation of \ifthenelse I proposed elsewhere on the site (see the link for examples of use).

\documentclass{book}

\ExplSyntaxOn

%%% Expandable reimplementation of \ifthenelse
\NewExpandableDocumentCommand{\xifthenelse}{mmm}
 {
  \bool_if:nTF { #1 } { #2 } { #3 }
 }

\cs_new_eq:NN \numtest     \int_compare_p:n
\cs_new_eq:NN \oddtest     \int_if_odd_p:n
\cs_new_eq:NN \fptest      \fp_compare_p:n
\cs_new_eq:NN \dimtest     \dim_compare_p:n
\cs_new_eq:NN \deftest     \cs_if_exist_p:N
\cs_new_eq:NN \namedeftest \cs_if_exist_p:c
\cs_new_eq:NN \eqdeftest   \token_if_eq_meaning_p:NN
\cs_new_eq:NN \streqtest   \str_if_eq_p:ee
\cs_new_eq:NN \emptytest   \tl_if_empty_p:n
\cs_new_eq:NN \blanktest   \tl_if_blank_p:n
\cs_new_eq:NN \boolean     \legacy_if_p:n
\cs_new:Npn \modetest #1
 {
  \str_case:nnF { #1 }
   {
    {h}{\mode_if_horizontal_p:}
    {v}{\mode_if_vertical_p:}
    {m}{\mode_if_math_p:}
    {i}{\mode_if_inner_p:}
   }
   {\c_false_bool}
 }
\cs_new:Npn \enginetest #1
 {
  \str_case:nnF { #1 }
   {
    {luatex}{\sys_if_engine_luatex_p:}
    {pdftex}{\sys_if_engine_pdftex_p:}
    {ptex}{\sys_if_engine_ptex_p:}
    {uptex}{\sys_if_engine_uptex_p:}
    {xetex}{\sys_if_engine_xetex_p:}
   }
   {\c_false_bool}
 }

% a utility command
\cs_set_eq:NN \caseswitch \str_case_e:nn

\ExplSyntaxOff

\newcommand\mycommand[1]{%
  \caseswitch{#1}{
    {A}{Aaa}
    {B}{Bbb}
  }%
}

\begin{document}

% expected "Function is A :)" but got "Function is B :("
\xifthenelse{\streqtest{\mycommand{A}}{Aaa}}{Function is A :)}{Function is B :(}

% another version - does not work too
\caseswitch{\mycommand{A}}{{Aaa}{Function is A :)}{Bbb}{Function is B :(}}

\end{document}
1
  • The spaces! Of course, I should notice that. Thank you! It worked perfectly! Thank you also for the reimplementation of ifthenelse.
    – Alberto
    Commented Nov 27, 2022 at 21:07
5

If you are not in Expl3 you need to be aware of spaces at end of line. Also you never really need \ifthenelse especially if you are already using expl3 which has far better control constructs.

\documentclass{book}


\ExplSyntaxOn
\cs_set_eq:NN \caseswitch \str_case_e:nn

\newcommand\mycommand[1]{
  \caseswitch{#1}{
    {A}{Aaa}
    {B}{Bbb}
  }
}
\ExplSyntaxOff

\begin{document}


\caseswitch{\mycommand{A}}{{Aaa}{Function is A :)}{Bbb}{Function is B :(}}

\end{document}

You must log in to answer this question.

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