该tensor
包允许使用以下命令排版张量,
\[
\tensor{X}{^b^o_a_t}
\]
产生以下表达式:
使用该physics
包可以排版涉及张量的表达式,例如,
\[
\tensor{\qty(\tensor{\qty(\tensor{T}{^g^o_a_t})}{^b^o_x})}{^d^o_g}
\]
如您所见,随着嵌入张量数量的增加,跟踪括号变得更加困难。我正在尝试编写一个执行此操作的宏,但是当我编译代码时出现以下错误:
f:/tensorgroup/tensorgroup.tex:84: Argument of \stensor has an extra }.
整个代码如下所示:
\documentclass[12pt,DIV=15,parskip=full]{scrartcl}
\usepackage{mathptmx}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{tensor}
\usepackage{physics}
\usepackage{xparse}
\usepackage{ifthen}
\usepackage{listofitems}
\usepackage{etoolbox}
% group tensor list is supgroup0,subgroup0]supgrroup1,subgroup1]...
% supgroupN is letter0:letter1:... (same for subgroupN)
\NewDocumentCommand{\stensor}{m m}
{
\setsepchar{,/:}
\greadlist*\indices{#2} % indices are superscripts,subscripts with letters separated by :
\def\expression{}
\def\sups{}
\foreach \i in {1, ...,\listlen\indices[1]}
{
\xappto\sups{^\indices[1,\i]}
}
\def\subs{}
\foreach \i in {1, ...,\listlen\indices[2]}
{
\xappto\subs{_\indices[2,\i]}
}
\xappto\expression{\noexpand\tensor{#1}{\sups \subs}}
\expression
}
\NewDocumentCommand{\gtensor}{m m}
{
\setsepchar{,/:}
\greadlist*\indices{#2} % indices are superscripts,subscripts with letters separated by :
\def\fish{}
\def\sups{}
\foreach \i in {1, ...,\listlen\indices[1]}
{
\xappto\sups{^\indices[1,\i]}
}
\def\subs{}
\foreach \i in {1, ...,\listlen\indices[2]}
{
\xappto\subs{_\indices[2,\i]}
}
\xappto\fish{\noexpand\tensor{\noexpand\qty(#1)}{\sups \subs}}
\fish
}
\NewDocumentCommand{\evalgtensor}{m m}
{
\setsepchar{]/,/:}
\readlist\allidx{#2}
\def\bird{}
\foreach \i in {2, ...,\listlen\allidx[]}
{
\xappto\bird{ \noexpand\gtensor{ }
}
\xappto\bird{\noexpand\stensor{#1}{\allidx[1]}}
\foreach \i in {2, ...,\listlen\allidx[]}
{
\xappto\bird{ }{\allidx[\i]}}
}
{\bird}
}
\begin{document}
\[
\stensor{T}{g:o,a:t}
\]
\[
\gtensor{T}{g:o,a:t}
\]
\[
\evalgtensor{T}{g:o,a:t]b:o,x]d,o:g]f:l:o,a:t}
%\gtensor{\gtensor{\gtensor{\stensor{T}{g:o,a:t}}{b:o,x}}{d,o:g}}{f:l:o,a:t}
\]
\end{document}
等式,
\[
\gtensor{\gtensor{\gtensor{\stensor{T}{g:o,a:t}}{b:o,x}}{d,o:g}}{f:l:o,a:t}
\]
工作并产生预期的结果。
我已确保在\evalgtensor
命令中避免出现新手错误,例如大括号不匹配。如果我将命令替换为\gtensor
、gtensor
with\stensor
和stensor
convert{
以及}
to \{
,\}
我会得到括号匹配的文本。但出于某种原因,当实际宏在\evalgtensor
宏中时,它会失败。
答案1
我不会用,\left
而且\right
喜欢。说实话,physics
我根本不会用。physics
这是一个具有简单语法的实现:
\documentclass{article}
\usepackage{tensor}
\ExplSyntaxOn
\NewDocumentCommand{\gtensor}{mm}
{% #1 is the symbol, #2 the list of tensor indices
\tedblack_gtensor:nn { #1 } { #2 }
}
\cs_new_protected:Nn \__tedblack_tensor:nn { \tensor{#1}{#2} }
\seq_new:N \g__tedblack_gtensor_list_seq
\tl_new:N \l__tedblack_gtensor_out_tl
\cs_new_protected:Nn \tedblack_gtensor:nn
{
\tl_set:Nx \l__tedblack_gtensor_out_tl
{
\__tedblack_tensor:nn { #1 } { \tl_item:nn { #2 } { 1 } }
}
\int_step_inline:nnn { 2 } { \tl_count:n { #2 } }
{
\tl_set:Nx \l__tedblack_gtensor_out_tl
{
\__tedblack_tensor:nn
{ \group_begin: (\exp_not:V \l__tedblack_gtensor_out_tl) \group_end: }
{ \tl_item:nn { #2 } { ##1 } }
}
}
\tl_use:N \l__tedblack_gtensor_out_tl
}
\ExplSyntaxOff
\begin{document}
\[
\gtensor{T}{
{^g^o_a_t}
{^b^o_x}
{^d^o_g}
{^f^l^o_a_t}
}
\]
\end{document}
其思想是从内到外逐步构建整个表达式,在第一步使用基本符号的地方添加括号。
如果你\group_begin: (
用\left(
和) \group_end:
用替换\right)
,你会得到
我觉得它不美观。