使用宏将参数传递给命令

使用宏将参数传递给命令

\iftagged我正在尝试将一些标签从包传递给命令tagging。请参阅下面的 MWE。

据我所知,我的宏\mytags没有扩展。我在这里发现了很多类似的问题,但仍然无法弄清楚。请帮忙!

\documentclass[10pt]{article}

\usepackage{tagging}

\begin{document}

\newcommand{\mytags}{%
tagone,
tagtwo,
tagthree
}

\usetag{tagone}

% Command
\iftagged{\mytags}{Tags passed to the command successfully!}{Tags not passed to the command!}

% Environment
\begin{taggedblock}{\mytags}
    Tags passed to the environment successfully!
\end{taggedblock}

\begin{taggedblock}{tagone,tagtwo,tagthree}
    Tags passed directly successfully!
\end{taggedblock}

\end{document}

我得到的结果:

Tags not passed to the command!
Tags passed directly successfully!

编辑

\iftagged\begin{taggedblock}来自包的命令标记

基本上,我需要在编译期间\iftagged{\mytags}用 替换\iftagged{tagone,tagtwo,tagthree}。用 也一样\begin{taggedblock}{\mytags}

答案1

该命令\iftagged不对其第一个参数进行扩展。

你可能想做

\expandafter\iftagged\expandafter{\mytags}{...}{...}

或者采取更大的步骤并立即进行扩展(这应该没有风险,因为标签是简单的字符串)。对 也执行相同的操作\taggedblock

\documentclass[10pt]{article}

\usepackage{tagging}
\usepackage{etoolbox}

% replace \docsvlist in \iftagged with \xdocsvlist
\patchcmd{\iftagged}{\docsvlist}{\xdocsvlist}{}{}
\patchcmd{\taggedblock}{\docsvlist}{\xdocsvlist}{}{}
% \xdocsvlist fully expands its argument before passing it to \docsvlist
\newcommand{\xdocsvlist}[1]{%
  \expandafter\docsvlist\expandafter{\expanded{#1}}%
}

\begin{document}

\newcommand{\mytags}{%
tagone,
tagtwo,
tagthree
}

\usetag{tagone}

% Command
\iftagged{\mytags}{Tags passed to the command successfully!}{Tags not passed to the command!}

% Environment
\begin{taggedblock}{\mytags}
    Tags passed to the environment successfully!
\end{taggedblock}

\begin{taggedblock}{tagone,tagtwo,tagthree}
    Tags passed directly successfully!
\end{taggedblock}

\end{document}

在此处输入图片描述

相关内容