Edit: Sorry forgot to add the intention: So my understanding is that \def
expands the current definition, so I wanted to try using \edef
which I believe is meant to fully expand the command even after it has been defined later in the document, after it was called.
So current I am using the command \s
to print an answer sheet at the end of the document, but if I move it to the top of the document, none of the answers have been defined by the \ans{}
document.
\long\def\ans#1{
\expandafter\global\expandafter\def\csname ans\the\value{section}\the\value{Questionpart} \endcsname{\item\quad#1}
#1
}
The above code was changed to the below (with \edef) and the document cannot compile
\long\def\ans#1{
\expandafter\global\expandafter\edef\csname ans\the\value{section}\the\value{Questionpart} \endcsname{\item\quad#1}
#1
}
For context, this custom command is used with
\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}
}
}
Edit: Test code
\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}
\edef
. As you do not say what the intention is it is hard to say what to change other than "do not do that". perhaps you want\protected@edef
but without any information it is hard to say. – David Carlisle Oct 4 '19 at 18:48\raq
is missing. – egreg Oct 5 '19 at 14:10