我的\Render
尝试迭代列表并动态调用宏会产生错误。给定的 MWE 带有详细注释,说明了我的目的。如何修复它?我不知道如何修复以下错误:
平均能量损失
\documentclass{book}
\usepackage{filecontents}
% There is a huge number of Japanese words as follows.
% Format: \NewWord{<word macro name>}{<word in Kanji>}{<meaning in English>}
\begin{filecontents*}{vocabularies.tex}
\NewWord{watashi}{私}{I}
\NewWord{ki@spirit}{気}{spirit}
\NewWord{ki@tree}{木}{tree}
\NewWord{aikidou}{合気道}{aikidou}
\end{filecontents*}
\usepackage{xeCJK}
\usepackage{etoolbox}
\setmainfont{Cambria}
\setCJKmainfont{ipaexm.ttf}
\usepackage{pgffor}
% \FlashCard is used to create a flash card.
% Format: \FlashCard{<word in Kanji>}{<meaning in English>}
\newcommand\FlashCard[2]{\fbox{#1: #2} }
\gdef\Pushed{}
% \Push is used to push "macro name" into \Pushed.
% \Pushed is a comma separated list of "macro name".
\newcommand\Push[1]{\xdef\Pushed{#1,\Pushed}}
% #1: macro name,
% #2: word in Kanji,
% #3: meaning in English
% ----------------------
\newcommand\NewWord[3]{%
%
% create conditional macro
\expandafter\newif\csname if#1IsInserted\endcsname
% set the conditional macro to false
\csname #1IsInsertedfalse\endcsname
%
% create flash card macro
\expandafter\newrobustcmd\csname#1FlashCard\endcsname{\FlashCard{#2}{#3}}%
%
% create "word macro",
\expandafter\newrobustcmd\csname#1\endcsname[1][#2]{%
% output
##1%
% uniqueness routine
\csname if#1IsInserted\endcsname
% do nothing
\else
\Push{#1}
\csname #1IsInsertedtrue\endcsname
\fi
}%
}
\newcommand\Render{%
\par Flash Cards:\\[12pt]%
% For each <element> in \Pushed, invoke its <element>FlashCard and
% set <element>IsInserted to false.
\foreach \x in \Pushed {\csname\x FlashCard\endcsname\global\csname\x IsInsertedfalse\endcsname}%
%
% Make \Pushed empty
\gdef\Pushed{}%
}
\makeatletter
\input{vocabularies.tex}
\begin{document}
\begin{enumerate}
\item
\watashi は合\ki@spirit 道が好きです。 いい天\ki@spirit ですので、木の下に座っています。
\Render
\item
\ki@tree の下に誰もいないです。
\Render
\end{enumerate}
\end{document}
答案1
您的列表中有一个尾随,
,这意味着最后一步\x
是空的并产生了错误:
\documentclass{book}
\usepackage{filecontents}
% There is a huge number of Japanese words as follows.
% Format: \NewWord{<word macro name>}{<word in Kanji>}{<meaning in English>}
\begin{filecontents*}{vocabularies.tex}
\NewWord{watashi}{私}{I}
\NewWord{ki@spirit}{気}{spirit}
\NewWord{ki@tree}{木}{tree}
\NewWord{aikidou}{合気道}{aikidou}
\end{filecontents*}
\usepackage{xeCJK}
\usepackage{etoolbox}
\setmainfont{Cambria}
\setCJKmainfont{ipaexm.ttf}
\usepackage{pgffor}
% \FlashCard is used to create a flash card.
% Format: \FlashCard{<word in Kanji>}{<meaning in English>}
\newcommand\FlashCard[2]{\fbox{#1: #2} }
\gdef\Pushed{}
% \Push is used to push "macro name" into \Pushed.
% \Pushed is a comma separated list of "macro name".
\newcommand\Push[1]{\xdef\Pushed{#1\ifx\Pushed\empty\else,\Pushed\fi}}
% #1: macro name,
% #2: word in Kanji,
% #3: meaning in English
% ----------------------
\newcommand\NewWord[3]{%
%
% create conditional macro
\expandafter\newif\csname if#1IsInserted\endcsname
% set the conditional macro to false
\csname #1IsInsertedfalse\endcsname
%
% create flash card macro
\expandafter\newrobustcmd\csname#1FlashCard\endcsname{\FlashCard{#2}{#3}}%
%
% create "word macro",
\expandafter\newrobustcmd\csname#1\endcsname[1][#2]{%
% output
##1%
% uniqueness routine
\csname if#1IsInserted\endcsname
% do nothing
\else
\Push{#1}
\csname #1IsInsertedtrue\endcsname
\fi
}%
}
\newcommand\Render{%
\par Flash Cards:\\[12pt]%
% For each <element> in \Pushed, invoke its <element>FlashCard and
% set <element>IsInserted to false.
%\show\Pushed
\foreach \x in \Pushed {%
%\show\x
\csname\x FlashCard\endcsname\global\csname\x IsInsertedfalse\endcsname}%
%
% Make \Pushed empty
\gdef\Pushed{}%
}
\makeatletter
\input{vocabularies.tex}
\begin{document}
\begin{enumerate}
\item
\watashi は合\ki@spirit 道が好きです。 いい天\ki@spirit ですので、木の下に座っています。
\Render
\item
\ki@tree の下に誰もいないです。
\Render
\end{enumerate}
\end{document}