# How to delay expansion

Consider the following document:

\documentclass{article}
\usepackage{polynomial}

\begin{document}
\def\a{-3}
\def\b{-2}
\def\c{-4}
$\polynomial{\a,\b,\c}\quad\polynomial{-3,-2,-4}$
\end{document}


As can be seen in the result

the first \polynomial produces extra + signs in front of negative coefficients, most probably due to late expansion of \a, \b, and \c. How can this be prevented?

## Edit:

The solution provided

\begingroup\edef\x{\endgroup\noexpand\polynomial{\a,\b,\c}}\x


fails in a moving argument:

\documentclass{article}
\usepackage{polynomial}

\begin{document}
\def\a{-3}
\def\b{-2}
\def\c{-4}
\begin{figure}
\caption{$\begingroup\edef\x{\endgroup\noexpand\polynomial{\a,\b,\c}}\x$}
\end{figure}
\end{document}


What modifications are required in this case?

This question appeared while providing an answer to Parameters in a caption.

You can redefine \polynomial to expand its argument:

\documentclass{article}
\usepackage{polynomial,xparse}

\ExplSyntaxOn
\cs_set_eq:NN \gonzalo_poly:n \polynomial
\cs_generate_variant:Nn \gonzalo_poly:n { x }
\RenewDocumentCommand{\polynomial}{m}
{
\gonzalo_poly:x { #1 }
}
\ExplSyntaxOff

\begin{document}

\begin{figure}
\def\a{-3}
\def\b{-2}
\def\c{-4}

X
\caption{$\polynomial{\a,\b,\c}$}
\end{figure}
\end{document}

• Ah, I like this. It's perfectly in line with my intention to learn expl3 syntax. Thanks! – Gonzalo Medina Jul 25 '15 at 22:33

You need to force expansion of the arguments before \polynomial. A typical approach would be

\documentclass{article}
\usepackage{polynomial}

\begin{document}
\def\a{-3}
\def\b{-2}
\def\c{-4}
$\begingroup\edef\x{\endgroup\noexpand\polynomial{\a,\b,\c}}\x \quad\polynomial{-3,-2,-4}$
\end{document}


assuming that \a, \b and \c can be taken to be fully expandable (as here).

You can declare a robust command so it is safe in a caption:

\documentclass{article}
\usepackage{polynomial}
\DeclareRobustCommand\xpolynomial[1]{%
\edef\next{\noexpand\protect\noexpand\polynomial{#1}}\next}

\begin{document}

\listoftables

\begin{table}
\def \a{-3}
\def\b{-2}
\def \c{-4}

\caption{$\xpolynomial{\a,\b,\c}$}

\end{table}

zzzz

\end{document}


Or to do it locally as requested in comments

\documentclass{article}
\usepackage{polynomial}

\begin{document}

\listoftables

\begin{table}
\def \a{-3}
\def\b{-2}
\def \c{-4}
\def\xpolynomial#1#2{%
\edef#1{\noexpand\protect\noexpand\polynomial{#2}}}
\xpolynomial\thispoly{\a,\b,\c}

\caption{$\thispoly$}

\end{table}

zzzz

\end{document}

• Thanks, David. Let's say I don't want to declare it robust from the beginning. Would it be possible to \protect what needs to be protected in \edef\next{\noexpand\protect\noexpand\polynomial{#1}}\next} so it works in captions? – Gonzalo Medina Jul 26 '15 at 0:00
• @GonzaloMedina updated answer – David Carlisle Jul 26 '15 at 9:05