使用 \newcommand 的可變維度向量?

使用 \newcommand 的可變維度向量?
\usepackage{amsmath}
\usepackage{bm}

\newcommand{\uvec}[1]{\bm{\hat{#1}}}  % Bold hatted unit vectors
\newcommand{\BasisChar}{\uvec e}
\newcommand{\Element}[2]{#1_{#2}{\BasisChar}_{#2}}  % \Element{a}{5} yields a_5\uvec e_5

% Nonesense attempt..
\newcommand{\Vector}[2]{  
  \OutieVec = \Element{#1}{0}
  \for(n = 1; #2; n++)
  \OutieVec += \Element{#1}{n} 
  \next
  \the\outievec
                       }

\begin{document}
  \Vector{a}{3} 'results in a_0\uvec e_0 + a_1\uvec e_1 + a_2\uvec e_2 + a_3\uvec e_3 
  
  \Vector{x}{2} 'results in x_0\uvec e_0 + x_1\uvec e_1 + x_2\uvec e_2
  
  \renewcommand{\BasisChar}{\uvec i}
  \vector{b}{4} 
  'results in b_0\uvec i_0 + b_1\uvec i_1 + b_2\uvec i_2 + b_3\uvec i_3 + b_4\uvec i_4 

\end{document}```

答案1

使用 進行循環expl3並不困難,因為基礎設施已經可用\int_step_inline:nn

\documentclass{article}
\usepackage{amsmath,bm}

\newcommand{\uvec}[1]{\bm{\hat{#1}}}
\newcommand{\BasisChar}{\uvec{e}}

\ExplSyntaxOn

\NewDocumentCommand{\Vector}{smm}
 {
  \IfBooleanTF { #1 }
   {% with ellipsis
    \scotparker_vector_ellipsis:nn { #2 } { #3 }
   }
   {% fully written down
    \scotparker_vector_full:nn { #2 } { #3 }
   }
 }

\cs_new_protected:Nn \scotparker_vector_full:nn
 {
  #1\sb{0} \BasisChar\sb{0}
  \int_step_inline:nn { #2 }
   {
    + #1\sb{##1} \BasisChar\sb{##1}
   }
 }

\cs_new_protected:Nn \scotparker_vector_ellipsis:nn
 {
  #1\sb{0} \BasisChar\sb{0} +
  #1\sb{1} \BasisChar\sb{1} +
  \dots +
  #1\sb{#2} \BasisChar\sb{#2}
 }

\ExplSyntaxOff

\begin{document}

$\Vector*{c}{n}$

$\Vector{a}{3}$
  
$\Vector{x}{2}$
  
\renewcommand{\BasisChar}{\uvec{\imath}}

$\Vector{b}{4}$

\end{document}

因為它很容易做到,所以我還添加了“符號”線性組合的版本。

在此輸入影像描述

笑話注。既然您似乎喜歡省略大括號,請嘗試$A_\notin$.

不是開玩笑的筆記。當然,這可以透過“標準”方法來實現(我只顯示“完整”版本)。

\makeatletter
\newcommand{\Vector}[2]{%
  #1_{0} \BasisChar_{0}%
  \begingroup\count@=0
  \loop\ifnum#2>\count@
  \advance\count@ by 1
    +#1_{\the\count@} \BasisChar_{\the\count@}%
  \repeat
  \endgroup
}
\makeatother

選擇最容易閱讀的內容。

相關內容