Nested inline math for new command with argument

There exists some consensus on the fact that one shouldn't use $...$, $$...$$ or \ensuremath inside of newcommand (or NewDocumentCommand, ...) definitions to wrap passed macro arguments that are to typeset in math mode:

\documentclass{article}

\begin{document}
\newcommand*{\somecommand}[1]{
$$#1$$
}
\somecommand{Hello!}
\end{document}

Here, Hello! will be typeset as math, but the user/author of that macro might have never known what hit them. This leaves something to be desired and might be pretty surprising to the user. It should work, for example, like \somecommand{$Hello!$}, where the \newcommand definition omits entering math mode. The author should be in command and be aware of math and text modes.

Now, I have a command that is supposed to combine the macro argument input with more math, in this case a single equal sign:

\documentclass{article}

\begin{document}
\newcommand*{\somecommand}[1]{
#1$$=$$
}
\somecommand{$$A$$}
\end{document}

But this destroys math spacing:

We effectively have $$A$$$$=$$. That makes little sense. We can't define \somecommad to have $$#1=$$: this works when #1 is just text, but not if it's math mode already (LaTeX Error: Bad math environment delimiter). That case would expand to $$\(A$$ = \).

The question is: why does that not work?

• I provided more context to the original question. It mainly boils down to nesting $$...$$ and my lack of understanding why that is not possible. Apr 19, 2021 at 7:50