In general, TeX does not allow use to 'look before' a command, only to 'look after' (exceptions are primitives such as \over and LuaTeX callbacks to alter the flow of processing). As such, the only way to deal with both cases is to make them 'aware' of what follows. Thus one could for example do
\documentclass{article}
\newcommand*{\metre}{%
\ensuremath{\mathrm{m}}%
}
\newcommand*{\m}{\metre}
\makeatletter
\newcommand*{\square}{%
\@ifnextchar\metre
{\square@aux}
{%
\@ifnextchar\m
{\square@aux}
{\ensuremath{^{2}}}%
}%
}
\newcommand{\square@aux}[1]{%
\ensuremath{#1^{2}}%
}
\begin{document}
The command used as a prefix gives \square\m.
The command used as a postfix gives \m\square.
\end{document}
which looks ahead at the token following \square and makes a choice accordingly. You can see that this would get unwieldy for a long list of units: one possible way around that would be to use a 'marker token' at the start of each unit, do a partial expansion and then check for that token. That might look like
\documentclass{article}
\usepackage{pdftexcmds}
\makeatletter
\let\joeh@unit@marker\relax
\newcommand*{\metre}{%
\joeh@unit@marker
\@firstofone{\ensuremath{\mathrm{m}}}% \@firstofone needed to remove "{" and "}"
}
\newcommand*{\m}{\metre}
\newcommand*{\square}{%
\expandafter\square@aux@i
\romannumeral-`0%
}
\newcommand{\square@aux@i}{%
\@ifnextchar\joeh@unit@marker % Tests by meaning
{\square@aux@ii}
{\ensuremath{^{2}}}%
}
\newcommand{\square@aux@ii}[1]{%
\ifnum\pdf@strcmp{\unexpanded{#1}}{\unexpanded{\joeh@unit@marker}}=\z@
\expandafter\square@aux@iii
\else
\expandafter\square@aux@iv
\fi
{#1}%
}
\newcommand{\square@aux@iii}[3]{%
% #1 = \joeh@unit@marker
% #2 = \@firstofone
% #3 = Unit code
\ensuremath{#3^{2}}%
}
\newcommand{\square@aux@iv}[1]{%
#1%
\ensuremath{^{2}}%
}
\begin{document}
The command used as a prefix gives \square\m.
The command used as a postfix gives \m\square.
\end{document}
You'll see this gets a bit tricky, particularly to cover many cases.
The bigger problem comes when you get to more complex units. For example, what would
\m\square\kg
actually mean? Getting the spacing and meaning right in more complex cases is what siunitx aims to do, and one part of that is differentiating between 'power to add to the next unit' and 'power to add to the previous unit'.
\parat each call of\square. I don't think it's a good idea to have such a behavior of\square; surely the way you're trying will not work: a macro with anmargument will always find one and\IfNoValueTF{#1}will follow the true branch in all cases. Also I can't understand why trying to reinvent the wheel:\si{m^2}is available withsiunitxand is definitely clearer. – egreg Dec 27 '12 at 23:59siunitxisn't appropriate. Besides, reinventing the wheel can only lead to a better wheel. There are needs that haven't been considered anywhere else yet. If it's not possible then so be it. – Dec 28 '12 at 00:07ginstead ofm, then you can use\square{\m}and\m\square. You will also need to add%after$. – Qrrbrbirlbel Dec 28 '12 at 01:17siunitxto get one that will cover all of the cases correctly! Also, using units at the document level leads to some issues with naming (for example\squareis used for math symbols, as is\bar). – Joseph Wright Dec 28 '12 at 09:17\velocity{}that automatically "know" the correct SI unit. All the user need type is\velocity{3}to get$3\;\mathrm{m}/\mathrm{s}$. – Dec 29 '12 at 03:09