Would it be possible to create a new command say \rvec that takes a variable number of arguments like \rvec{a}{b} and produces something like ai(hat) + bj(hat) and also \rvec{a}{b}{c} and produces ai(hat) + bj(hat) + ck(hat) ? I have a command that
allows me to change the style of vector (arrow or bold face) so would it be possible to implement something like that so I can change the hats to arrows ? Any suggestion is welcome.
- 665
1 Answers
\documentclass{article}
\usepackage{etoolbox,amsmath}
\newcounter{ijk}
\newcommand{\ijk}{% Set each vector component
\ifcase\value{ijk}\or
\vecstyle{\text{\i}}\or% 1
\vecstyle{\text{\j}}\or% 2
\vecstyle{\text{k}}\else% 3
?
\fi
}
\newcommand{\vecstyle}{\hat}
%\newcommand{\vecstyle}{\textbf}% Since i, j and k are set as text
%\newcommand{\vecstyle}{\vec}
\newcommand{\insertoperator}{}% Initialise operator between components
\newcommand{\rvec}[1]{% \rvec{<csv list>}
% Using a cunning trick: https://tex.stackexchange.com/a/89187/5764
\renewcommand{\insertoperator}{\renewcommand{\insertoperator}{+}}% Reinitialise operator to be delayed
\setcounter{ijk}{0}% Reset ijk counter
\renewcommand*{\do}[1]{% How each element in the <csv list> is processed
\stepcounter{ijk}% Step ijk
\insertoperator ##1\ijk}% Insert operator, argument and then the vector
\expandafter\docsvlist\expandafter{#1}% Process the entire <csv list>
% \expandafters here allow for passing macros
}
\begin{document}
$\rvec{a,b}$
$\rvec{a,b,c}$
$\rvec{a}$
\end{document}
With a bold vector format:
With a \vec vector format:
- 603,163



\rvec{a,b,c}would be easier. Is that a possibility? – Werner Mar 16 '21 at 20:43