Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm soon going to take Live-notes of a course where Tensors are commonly used.

Tensors consist of a Letter, say T, with numerous sub-and superscripts.

Generally, a tensor could look like

T^{i_{1},i{2},i{3}}_ {j_{1},j_{2},j_{3}} 

I would like to know if it is possible to create a \newcommand with 4 parameters,

\Tensor{T}{i}{j}{3}

Which sets the Tensor letter, the name of the super and subscript, and specifically the number of sub-and superscripts. I don't know whether this is possible.

share|improve this question
    
Is it necessary that both i and j will have the same number of indices? Secondly, can there only be one superscript and one subscript? –  recluze Feb 16 '13 at 8:56
    
No it is not necessary, but in general I think we'll be working with tensors that have the same number of super and subscripts. The answer to your second question is yes. But if I stumble upon such a tensor, writing it out without the command could prove faster. –  Mathusalem Feb 16 '13 at 9:02
    
Have you checked out the package tensind? I use that all the time in lectures. –  Nathanael Farley Feb 16 '13 at 9:07
    
It will indeed be useful, but my question is a bit broader than simply creating tensors. I'd really like to know whether it is possible to concatenate strings like i_{1},i_{2},.. and then pass them to T^{concatenated string} –  Mathusalem Feb 16 '13 at 9:18
add comment

1 Answer

up vote 7 down vote accepted

Yes, it's possible in many ways with loops. I prefer LaTeX3 (the experimental kernel and macros):

\documentclass{article}
\usepackage{xparse}

\DeclareMathAlphabet{\mathsfsl}{OT1}{cmss}{m}{sl}

\ExplSyntaxOn
% #1 = tensor
% #2 = number of indices
% #3 = upper index
% #4 = number of lower indices (optional)
% #5 = lower index
\NewDocumentCommand{\Tensor}{mmmom}
 {
  \IfNoValueTF { #4 }
   {
    \tens_make:nnnnn { #1 } { #2 } { #3 } { #2 } { #5 }
   }
   {
    \tens_make:nnnnn { #1 } { #2 } { #3 } { #4 } { #5 }
   }
 }
\cs_new_protected:Npn \tens_make:nnnnn #1 #2 #3 #4 #5
 {
  \tl_clear:N \l__tens_sup_tl
  \tl_clear:N \l__tens_sub_tl
  \tens_create_script:Nnn \l__tens_sup_tl { #2 } { #3 }
  \tens_create_script:Nnn \l__tens_sub_tl { #4 } { #5 }
  \mathsfsl{#1}
  \sp{\l__tens_sup_tl}
  \sb{\l__tens_sub_tl}
 }
\cs_new_protected:Npn \tens_create_script:Nnn #1 #2 #3
 {
  \int_step_inline:nnnn { 1 } { 1 } { #2 }
   {
    \int_compare:nTF { ##1 == 1 }
     {
      \tl_put_right:Nn #1 { #3 \sb {##1} }
     }
     {
      \tl_put_right:Nn #1 { , #3 \sb {##1} }
     }
   }
 }

\tl_new:N \l__tens_sup_tl
\tl_new:N \l__tens_sub_tl

\ExplSyntaxOff

\begin{document}

$\Tensor{T}{3}{i}{j}$

$\Tensor{T}{3}{i}[5]{j}$

$\Tensor{T}{2}{i}[0]{j}$

\end{document}

With a change of syntax, we can accommodate for different numbers of superscripts and subscripts: if there's no optional argument before the subscript variable name, the number will be the same as for the superscript variables. You can even have one (or both) equal to zero.

enter image description here

share|improve this answer
    
Thank you, this is the desired form. I will try and familiarise myself with this syntax. –  Mathusalem Feb 19 '13 at 10:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.