如何循环调用命令?

如何循环调用命令?

有一个代码

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}
\makeatletter
\newcommand*{\dothis}[1]{%
\stringcases
{#1}%
{%
  {a}{so you typed a}%
  {b}{now this is b}%
  {c}{you want me to do c?}%
}%
{[nada]}%
}
\newcommand{\stringcases}[3]{%
\romannumeral
\str@case{#1}#2{#1}{#3}\q@stop
}
\newcommand{\str@case}[3]{%
\ifnum\pdf@strcmp{\unexpanded{#1}}{\unexpanded{#2}}=\z@
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\str@case@end{#3}}
{\str@case{#1}}%
}
\newcommand{\str@case@end}{}
\long\def\str@case@end#1#2\q@stop{\z@#1}
\makeatother

\begin{document}

\dothis{a}

\dothis{b}

\dothis{c}

\dothis{e}

\end{document}

相反,

\dothis{a}

\dothis{b}

\dothis{c}

\dothis{e}

做这样的事情

\foreach \n in {a,b,c,e} {\dothis{\n}}

答案1

假设在每种情况下输入的\dothis长度恰好为一个字母(对于这个特定的例子来说,这成立——所以我猜这是实际的用例),您可以简单地使用\expandafter\foreach \n in {a,b,c,e} {\expandafter\dothis\n}

完整示例

\documentclass{beamer}
\usepackage{tikz}

\makeatletter
\newcommand*{\dothis}[1]{%
\stringcases
{#1}%
{%
  {a}{so you typed a}%
  {b}{now this is b}%
  {c}{you want me to do c?}%
}%
{[nada]}%
}
\newcommand{\stringcases}[3]{%
\romannumeral
\str@case{#1}#2{#1}{#3}\q@stop
}
\newcommand{\str@case}[3]{%
\ifnum\pdf@strcmp{\unexpanded{#1}}{\unexpanded{#2}}=\z@
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\str@case@end{#3}}
{\str@case{#1}}%
}
\newcommand{\str@case@end}{}
\long\def\str@case@end#1#2\q@stop{\z@#1}
\makeatother

\begin{document}
\foreach \n in {a,b,c,e} {\expandafter\dothis\n\par}
\end{document}

输出裁剪

相关内容