问题

问题

我最好给出如下的说明。

静态创建并调用的宏

\newcommand{\mymacro}{test}
\mymacro

动态创建并调用的宏

\expandafter\csname mymacro\endcsname{test}
\csname mymacro\endcsname

静态创建、设置和使用条件

\newif\ifIsInserted
\IsInsertedtrue

\ifIsInserted
    It has been inserted.
\else
    It has not been inserted yet.
\fi

问题

如何动态创建、设置和调用条件?

平均能量损失

\documentclass{article}

\begin{document}

% statically created macro
% \newcommand{\mymacro}{test}
% \mymacro

% dynamically created macro
\expandafter\csname mymacro\endcsname{test}
\csname mymacro\endcsname


% statically created conditional
\newif\ifIsInserted
\IsInsertedtrue

\ifIsInserted
    It has been inserted.
\else
    It has not been inserted yet.
\fi


% dynamically created conditional
% ?
\end{document}

答案1

\expandafter\newif\csname ifIsInserted\endcsname
\csname IsInsertedtrue\endcsname

定义\IsInsertedtrue,但如果您将其作为另一个宏的一部分,您可以用或您需要的任何其他构造IsInsertedtrue来替换。#1

笔记

% dynamically created macro
\expandafter\csname mymacro\endcsname{test}

不创建宏,我假设你的意思是

% dynamically created macro
\expandafter\def\csname mymacro\endcsname{test}

答案2

\documentclass{article}
\newcommand\Create[2][false]{%
    \expandafter\def\csname#2\endcsname{#2}%
    \expandafter\newif\csname if#2\endcsname \csname#2#1\endcsname}

\begin{document}

\Create{test} \iftest \test \fi % prints nothing

\Create[true]{Test} \ifTest \Test \fi % prints Test
\end{document}

相关内容