I want to make it easier on myself to write large commands like this by "naming" the arguments.
I did this:
% Define the command for vector projection with named arguments \NewDocumentCommand{\VectorProjection}{O{\mathbf{u}} O{\mathbf{v}}} { { % Local command definitions \newcommand{\vecu}{#1} \newcommand{\vecv}{#2} \begin{align} \text{proj}_{\vecv} \vecu &= \frac{\vecu \cdot \vecv}{\|\vecv\|^2} \vecv \\ &= \left( \frac{\vecu \cdot \vecv}{\|\vecv\|^2} \right) \vecv \\ &= \frac{\vecu \cdot \vecv}{\vecv \cdot \vecv} \vecv \end{align} } } I'm assuming here, that the extra pair of { and } should create a "scope". Think I read that somewhere. Is that correct?
The following is fine:
\VectorProjection \VectorProjection[a][b] This one two:
\VectorProjection \VectorProjection[a][b] \newcommand{\vecu}{U} But this one here complains about redefining \vecu and \vecv, i.e. Command \vecu already defined. is the error message:
\newcommand{\vecu}{U} \VectorProjection \VectorProjection[a][b] This one also complains:
\VectorProjection \VectorProjection[a][b] \newcommand{\vecu}{U} \VectorProjection[a][b] Simply replacing the \newcommand with \renewcommand` doesn't solve it either because then
\VectorProjection fails with Command \vecu undefined.
My workaround, but not really related to the question
For now I'm using CTAN: Package namedef to do something like this:
\named\def\implVectorProjection#[vecu]#[vecv]% { % Vector Projection of vecu onto vecv \begin{align} \text{proj}_{#[vecv]} #[vecu] &= \frac{#[vecu] \cdot #[vecv]}{\|#[vecv]\|^2} #[vecv] \\ &= \left( \frac{#[vecu] \cdot #[vecv]}{\|#[vecv]\|^2} \right) #[vecv] \\ &= \frac{#[vecu] \cdot #[vecv]}{#[vecv] \cdot #[vecv]} #[vecv] \end{align} } \NewDocumentCommand{\XVectorProjection}{O{\mathbf{u}} O{\mathbf{v}}} { \implVectorProjection{#1}{#2} } \XVectorProjection \XVectorProjection[a][b] If anyone knows what solution people normally use to name macro arguments please let me know. Seems like such an essential feature.

\def(which doesn't check) but the answer to your final question is that most people do not try to name aguments but use#1, #2` directly Note also that you should comment out the ends of lines you are adding a lot white space, which will not produce output as this is math mode, but is bad style (and wastes token memory)\renewcomandbut also at the top level have\newcommand{\vecu}{unexpected vecu}so that the local redefinition works and any top level use produces warning text\def\vecu{#1}instead of\newcommand{\vecu}{#1}indeed seems to do the trick. Even when I run this afterwards\def\vecu{X} \vecu \VectorProjection \vecuit will output "X" both times which is exactly what I would expect how scopes work. If I remove the additional{and}I get\mathbf allowed only in math mode.which is also as I'd expect. To me it looks like\newcommandthough doesn't work well with scopes then?\newcommandworks as designed in groups but gives errors if the command is already defined, as you say. Perhaps you were expecting that the start of a group clears all definitions, that certainly does not happen (otherwise you would not be able to use\begin{align}however I would not recommend you have these groups, it is quite hard to ensure they do not affect the formatting.