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 am trying to define a table of direct sums of {\chi_i}in LaTeX, meaning that the entries will be things of the form: 5 \chi_2 \bigoplus \chi_4 \bigoplus 2 \chi_7 for given data like {{1,0},{2,5},{3,0},{4,1},{5,0},{6,0},{7,2}} that tells you how many copies of each \chi_i we want for a given entry. How I wrote the data is just an example. I can format the data in whatever the most convenient form happens to be.

In order to avoid hardcoding in each entry of the table, I'm trying to define a new command in LaTeX, either via \newcommand or \def, that will take in such data and return it in the form of a direct sum as written above, but I'm a bit new to it and don't know how I should go about creating a command with what will be a variable number of arguments, since not each direct sum can be written with just three terms. The fact that \newcommand and \def take in a fixed number of arguments make me think that I should not be using them for this task. Any suggestions would be appreciated.

EDIT:

More accurately, the data concerning the coefficients of the sum is going to look like {5,2,7,8,2,0,0,1,3} where this is meant to be read as 5 \chi_1 \bigoplus 2 \chi_2 \bigoplus \ldot \bigoplus 3 \chi_9 where the sequence is always going to be a fixed size. It should also be the case that the terms with zeros should not show up in the sum.

share|improve this question

2 Answers 2

up vote 11 down vote accepted

Updated for new format:

enter image description here

\documentclass{article}

\makeatletter
\def\zz#1{{\@for\tmp:=#1\do{\expandafter\zzz\tmp\relax}}}
\def\zzz#1,#2\relax{\zzsep#1\chi_{#2}}
\def\zzsep{\def\zzsep{\bigoplus}}

\def\yy#1{{\count@\z@
\@for\tmp:=#1\do{\advance\count@\@ne
\ifnum\tmp=\z@\else\zzsep\tmp\chi_{\the\count@}\fi}}}
\makeatother
\begin{document}


\[
 \zz{{1,0},{2,5},{3,0},{4,1},{5,0},{6,0},{7,2}}
\]

\[
\yy{5,2,7,8,2,0,0,1,3} 
\]
\end{document}
share|improve this answer
    
Yes, thank you. I am surprised to do this would require defining three commands though. It seems like something that should not be too difficult. –  ntropy Oct 20 '13 at 21:18
2  
It doesn't have to use three it was just easier that way, basically because of the`,` syntax, you need one to loop through the outer comma list then another to split up the inner comma pair and the zzsep is just an easy way of avoiding putting the oplus separator before the first. three lines of code to implement a loop, splitting a pair and inserting a separator doesn't seem much to me:-) –  David Carlisle Oct 20 '13 at 21:30
    
Would you please have a look at my edit? –  ntropy Oct 20 '13 at 23:05
    
@aentropy updated uses 2 macros now not 3 (I hope to steal that tick off egreg:-) –  David Carlisle Oct 20 '13 at 23:51
    
Gave you the answer back cause this was the one I ended up using, I couldn't get LaTeX 3 to install on my computer to use egreg's solution. Would you happen to know what's wrong with the following? \def\clebschtable#1{\hline\@for\tmpi:=#1\do{$\chi$\@for\tmpj:=\tmpi\do{~HERE~$\‌‌​​chisum{\tmpj}$~THERE~}\\}} I'm trying to put in a \ttand right where it says ~HERE~, but whenever I do I get an "! Undefined control sequence." on \tmpj. For some reason I can put it ~THERE~ without any error, but it's not what I want. TeX logic is really confusing the heck out of me. –  ntropy Oct 21 '13 at 4:32

This code is longer than David's, but way more modern and fashionable, as it uses LaTeX3 functions.

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\chisum}{m} % a homage to John Wayne
 {
  \aentropy_chisum:n { #1 }
 }
\cs_new_protected:Npn \aentropy_chisum:n #1
 {
  \seq_clear:N \l_aentropy_chisum_seq
  \clist_map_inline:nn { #1 }
   {
    \aentropy_add_summand:n { ##1 }
   }
  \seq_use:Nn \l_aentropy_chisum_seq { \oplus }
 }
\cs_new:Npn \aentropy_add_summand:n #1
 {
  \__aentropy_add_summand:w #1 \q_stop
 }
\cs_new:Npn \__aentropy_add_summand:w #1 , #2 \q_stop
 {
  \int_compare:nTF { #1 = 1 }
   {% if the number of summands is 1, we don't show it
    \seq_put_right:Nn \l_aentropy_chisum_seq { \chi\c_math_subscript_token{#2} }
   }
   {
    \seq_put_right:Nn \l_aentropy_chisum_seq { #1\chi\c_math_subscript_token{#2} }
   }
 }
\ExplSyntaxOff

\begin{document}
$\chisum{{1,0},{2,5},{3,0},{4,1},{5,0},{6,0},{7,2}}$
\end{document}

enter image description here

The idea is just the same: we process the comma separated list and split each item into two components (coefficient and index); we store the built summand in a sequence and then deliver it separating its items with \oplus.


In order to comply with the second specification, some adaptations must be made.

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\chisum}{m} % a homage to John Wayne
 {
  \aentropy_chisum:n { #1 }
 }
\seq_new:N \l_aentropy_chisum_seq
\int_new:N \l_aentropy_chisum_index_int
\cs_new_protected:Npn \aentropy_chisum:n #1
 {
  \seq_clear:N \l_aentropy_chisum_seq
  \int_zero:N \l_aentropy_chisum_index_int
  \clist_map_inline:nn { #1 }
   {
    \int_incr:N \l_aentropy_chisum_index_int
    \aentropy_add_summand:n { ##1 }
   }
  \seq_use:Nn \l_aentropy_chisum_seq { \oplus }
 }
\cs_new:Npn \aentropy_add_summand:n #1
 {
  \int_compare:nF { #1 = 0 }
   {
    \seq_put_right:Nx \l_aentropy_chisum_seq
     {
      \int_compare:nF { #1 = 1 } { #1 }
      \exp_not:n { \chi }
      \c_math_subscript_token
      { \int_to_arabic:n { \l_aentropy_chisum_index_int } }
     }
   }
 }
\ExplSyntaxOff

\begin{document}
$\chisum{5,2,7,8,2,0,0,1,3}$

\end{document}

Note that \exp_not:n { \chi } is not really necessary, because \chi is not expandable; but you might want to change the symbol and so it's better to be safe than sorry.

Zero terms are omitted, one terms are printed without the coefficient 1.

enter image description here

With a different definition of \aentropy_add_summand:n you can also use symbolic coefficients. Each item will be compared to the string 0 and, in this case, no summand will be added; otherwise, the item is compared to 1 and, in this case, no coefficient is added.

\cs_new:Npn \aentropy_add_summand:n #1
 {
  \str_if_eq:nnF { #1 } { 0 }
   {
    \seq_put_right:Nx \l_aentropy_chisum_seq
     {
      \str_if_eq:nnF { #1 } { 1 } { #1 }
      \exp_not:n { \chi }
      \c_math_subscript_token
      { \int_to_arabic:n { \l_aentropy_chisum_index_int } }
     }
   }
 }

Try with

$\chisum{5,2,7,8,2,0,0,1,3}$

$\chisum{a,b,2,0,0,0,0,1,3}$

and you'll get

enter image description here

share|improve this answer
    
I was expecting an L3 list from you:-) (+1 for predictability:-) –  David Carlisle Oct 20 '13 at 22:20
    
Would you please have a look at my edit? –  ntropy Oct 20 '13 at 23:03

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.