I need to make a smart vector command that meets the following criteria:
- There are three arguments: an optional first argument specifying a delimiter (comma by default), a mandatory second argument specifying one or more comma separated components that may be numbers or mathematical expressions, an optional third argument specifying a physical unit.
If the command is invoked with
\smartvector{2,4,3}[m]then it should typeset the numbers as a delimited list in angle brackets, separated by the delimiter specified in requirement 1, with the unit outside the right bracket. All the entries in the comma separated list must be numbers. See the MWE below; I already have this working. Note that the unit is optional and may be omitted. Here's an example.
I'd like to add the capability to invoked the command with
\smartvector{2}[m]and have it just typeset the number and the unit with no brackets or commas. So I think I need to test whether the comma separated list in the mandatory second argument is a single number. Here's an example.
If the comma separated list contains ANY item that is NOT a number, as in
\smartvector{2x^2,y^3,2}then it should typeset as in requirement 2 but MUST leave off the unit. The user will know to leave off the unit as algebraic expressions don't have units. Here's an example.
So I think it comes down to parsing the comma separated list and doing some logic, but I must confess to not understanding that part of my MWE, which was offered to me here in another thread. I don't want to be guilty of cargo cult programming any more than I already am.
Here's my MWE:
\documentclass[10pt]{article}
\usepackage{xargs}
\makeatletter
\newcommandx\smartvector[3][1={,},3=,usedefault]{\ensuremath{%
\global\def\smart@delim{#1}%
\left\langle
\smart@vector #2,\relax\noexpand\@eolst%
\right\rangle{\;#3}}}%
\def\smart@vector #1,#2\@eolst{%
\ifx\relax#2\relax
#1
\else
#1\smart@delim
\smart@vector #2\@eolst%
\fi}
\makeatother
\newcommand{\m}{\ensuremath{\mathrm{m}}}
\begin{document}
\[
\smartvector{2,4,3}[\m]
\]
\end{document}
