是否可以创建一个新的命令,比如说\rvec
,它接受可变数量的参数\rvec{a}{b}
,并产生类似 ai(hat) + bj(hat) 的东西,以及\rvec{a}{b}{c}
产生 ai(hat) + bj(hat) + ck(hat) 的东西?我有一个命令可以让我改变矢量的样式(箭头或粗体),那么是否可以实现类似的东西,以便我可以将帽子改为箭头?欢迎提出任何建议。
答案1
\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}
有大胆的矢量格式:
采用\vec
矢量格式: