我有一个 LaTeX 文档,其中我使用 LaTeX3 语法定义了一个 switch case 命令。我在命令\equal
的条件内使用了 case \ifthenelse
。但是,我一直得到错误的结果;在我看来,在 中\equal
,switch case 无法展开。
我做错了什么?在下面的 MWE 中,我期望“功能是 A:)“但得到了”功能为 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}
答案1
一个有趣的案例虚假空间综合症:您正在Aaa
与 Aaa
(字符串周围有两个空格)进行比较。
\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}
我会用更简单的
\ExplSyntaxOn
\cs_set_eq:NN \caseswitch \str_case_e:nn
\ExplSyntaxOff
以及\ifthenelse
我提议的重新实施网站其他地方(请参阅链接了解使用示例)。
\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}
答案2
如果您没有使用 Expl3,则需要注意行尾的空格。此外,您实际上并不需要,\ifthenelse
特别是如果您已经在使用expl3
具有更好控制结构的 Expl3。
\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}