![将 \def 更改为 \edef 会导致错误 **job aborted, no legal \end found](https://linux22.com/image/402688/%E5%B0%86%20%5Cdef%20%E6%9B%B4%E6%94%B9%E4%B8%BA%20%5Cedef%20%E4%BC%9A%E5%AF%BC%E8%87%B4%E9%94%99%E8%AF%AF%20**job%20aborted%2C%20no%20legal%20%5Cend%20found.png)
编辑:抱歉,忘了添加意图:所以我的理解是\def
扩展当前的定义,所以我想尝试使用\edef
我相信的方法来完全扩展命令,即使它在文档中稍后被定义,在被调用之后。
所以目前我正在使用命令\s
在文档末尾打印答案表,但如果我将其移动到文档顶部,则文档中没有任何答案被定义\ans{}
。
\long\def\ans#1{
\expandafter\global\expandafter\def\csname ans\the\value{section}\the\value{Questionpart} \endcsname{\item\quad#1}
#1
}
上面的代码被改为下面的代码(使用 \edef),文档无法编译
\long\def\ans#1{
\expandafter\global\expandafter\edef\csname ans\the\value{section}\the\value{Questionpart} \endcsname{\item\quad#1}
#1
}
对于上下文,此自定义命令用于
\newcounter{Questionpart}[section]
\newcommand{\Qpart}{(\stepcounter{Questionpart}\alph{Questionpart})
\global\expandafter\def\csname Prob\the\value{section}\endcsname{\the\value{Questionpart}}
}
\newcommand{\p}[1]{
\csname ans#1 \endcsname}
\newcommand{\s}{\newpage
\begin{center}
\Large\textbf{Solutions}
\end{center}
\foreach \x in {1,...,\value{section}}
{
\textbf{Problem \x}\par
\begin{enumerate}[label=(\alph*)]
\foreach \y in {1,...,10}
{\p{\x\y}\par
}
\end{enumerate}
}
}
编辑:测试代码
\documentclass[12pt]{article}
\newcounter{Questionpart}[section]
\newcommand{\Qpart}{(\stepcounter{Questionpart}\alph{Questionpart})
\global\expandafter\def\csname Prob\the\value{section}\endcsname{\the\value{Questionpart}}
}
\newcommand{\cbox}[1]{\parbox[t]{395pt}{#1}}
\newcommand{\qbox}[1]{\parbox[t]{425pt}{#1}}
\newcommand{\secfnt}{\fontsize{12}{14}}
\titlespacing*{\section} {0pt}{3.5ex plus 1ex minus .2ex}{1ex plus .2ex}
\titleformat{\section}{\titlerule\vspace{1mm}\normalfont\secfnt\bfseries}{\thesection}{2em}{}
\renewcommand{\thesection}{{\noindent\hrulefill}\\ Problem \arabic{section}.}
\long\def\ans#1{
\expandafter\global\expandafter\edef\csname ans\the\value{section}\the\value{Questionpart} \endcsname{\item\quad#1}
#1
}%A global 'array' structure which names each element as ans<problem_number><problem_part_in_numbers>
\newcommand{\p}[1]{\csname ans#1 \endcsname}
\newcommand{\s}{\newpage
\foreach \x in {1,...,\value{section}}
{
problem \x\par
\begin{enumerate}[label=(\alph*)]
\foreach \y in {1,...,10}
{\p{\x\y}\par
}
\end{enumerate}
}
}
\include{preamble}
\renewenvironment{comment}{\setlength{\parskip}{\baselineskip}\setlength{\parindent}{0pt}\begin{adjustwidth}{26pt}{0pt}}{\setlength{\parskip}{\baselineskip}\setlength{\parindent}{0pt}\end{adjustwidth}}
\begin{document}
\section{}
\section{}
{Express the flowing complex numbers in their polar form, $r(\cos\theta+\mathrm{i}\sin\theta)$}.
\vspace{5mm}
\Qpart $z=3+4\mathrm{i}$ %%AAAA
\begin{comment}\color{red}
\raq \cbox{Find the $r$ (the magnitude of $z$):\\[1mm]
$r=|z|=\sqrt{3^2+4^2}=\sqrt{25}=5$
Find the argument $arg(z)$ (angle $\theta$):\\[1mm]
$\theta=\mathrm{arg}(z)=\tan^{-1}\frac{imaginary\;part}{real\; part}=\tan^{-1}\frac{4}{3}=53.13^\circ$}
\raq\cbox{$z=a+b\mathrm{i}=r(\cos\theta+\mathrm{i}\sin\theta)$}
\raq\cbox{\ans{Therefore $z=5(\cos 53.13^\circ+\mathrm{i}\sin 53.13^\circ)$}\\}
\end{comment}
{\noindent\dotfill}% % % % % % % % % % % % % % % % % % % % % % % %
\begin{comment} \newpage
\end{comment}
\Qpart Find $z_1+z_2$%%%%%%%%%%%%%AAAAAAAAAAAAAA
\begin{comment}{\color{red}\raq\cbox{
Collect real and complex terms:
\\\ans{$ z_1+z_2=(2+4\mathrm{i})+(4-7\mathrm{i})=(2+4)+(4-7)\mathrm{i}=6-3\mathrm{i}$}}
}\end{comment}
{\noindent\dotfill}
\section{}
\Qpart Find $z_1z_2$%%%%%%%%%CCCCCCCCCCCCCCCCCCC
\begin{comment}{\color{red}\raq\cbox{
Expand the brackets:
\\$ z_1z_2=(2+4\mathrm{i})(4-7\mathrm{i})=2\times4-2\times7\mathrm{i}+4\times4\mathrm{i}-4\times7\mathrm{i^2}$}
\raq\cbox{Collect real and complex terms:
\\\ans{$8+28-14\mathrm{i}+16\mathrm{i}=36+2\mathrm{i}$}}
}\end{comment}
\Qpart
\s
\end{document}