Titling package gives “Undefined control sequence” error when using a macro that defines a dimen in the title

I have a macro that defines a new dimension (details have been stripped away for the MWE; in the actual command, I want to store an old value in the new one so I can modify it temporarily and then restore the old value, so defining it outside the command won't work). I can run this command in the title just fine---unless I load the titling package, in which case I get an undefined control sequence error for \x, even if I don't call \maketitle in the document. Is there a way to resolve this?

MWE:

\documentclass{article}

\usepackage{titling} % Comment this out and it works

\newcommand{\test}{%
\newdimen\x \x=1pt
}

\title{\test}

\begin{document}
\end{document}

• If you more \newdimen\x out of the \newcommand, the error is gone. Do you need it inside the command? – Schrödinger's cat Mar 26 at 4:07
• To expand on the comment by @Schrödinger'scat, it's inadvisable to use \newdimen in a command that will be called more than once. Instead, create \newdimen independently, then set it inside the "active" command definition. – barbara beeton Mar 26 at 13:25