\NeedsTeXFormat{LaTeX2e}%                                                     %
\ProvidesPackage{tonnetz}[2008/09/01 v1.0 generate a tonnetz]%        %
\RequirePackage{tikz}
%\usetikzlibrary{calc}
\def\tn@multisyllable#1#2{%
    \@tempcnta#1\relax
    \@whilenum \@tempcnta > 0
    \do{%
      #2% 
      \advance\@tempcnta by -1
    }%
}

\def\tnstdtonname#1#2{%
  #2%
  \@tempcntb#1%
  \ifnum \@tempcntb<0
  \tn@multisyllable{-\@tempcntb}{es}%
  \else
  \tn@multisyllable\@tempcntb{is}%
  \fi
}

\def\tn@deftonname#1#2{%
  \expandafter\def\csname tn@name@#1\endcsname##1{#2}%
}
\def\tn@deftnstdtonname#1#2{%
  \tn@deftonname{#1}{%
    \tnstdtonname{##1}{#2}%
  }%
}
\tn@deftnstdtonname{-1}f
\tn@deftnstdtonname0c
\tn@deftnstdtonname1g
\tn@deftnstdtonname2d
\tn@deftonname3{%
  \@tempcntb#1%
  \ifnum \@tempcntb<0
  \tn@multisyllable{-\@tempcntb}{as}%
  \else
  a\tn@multisyllable\@tempcntb{is}%
  \fi
} 
\tn@deftonname4{%
  \@tempcntb#1%
  \ifnum \@tempcntb<0
  \tn@multisyllable{-\@tempcntb}{es}%
  \else
  e\tn@multisyllable\@tempcntb{is}%
  \fi
}
\tn@deftonname5{%
  \ifnum#1=-1
  b%
  \else
  \tnstdtonname{#1}h%
  \fi
}

\def\tnnodesize{1.5em}
\def\tntonname#1#2{\csname tn@name@#1\endcsname{#2}}
\newcommand\tnnodetext[2]{%
    \@tempcnta#2\relax
    \@tempcntb#1\relax
    \multiply\@tempcnta by 4
    \advance\@tempcntb by \@tempcnta
    \@tempcnta=0
    \@whilenum \@tempcntb <-1 \do {%
      \advance \@tempcnta by -1
      \advance \@tempcntb by 7
    }%
    \@whilenum \@tempcntb >5 \do {%
      \advance \@tempcnta by 1
      \advance \@tempcntb by -7
    }%
    \tntonname{\the\@tempcntb}\@tempcnta%
    \@tempcnta#2
    \ifnum \@tempcnta=0
    \else
    \multiply\@tempcnta by -1
    ${}^{\the\@tempcnta}$%
    \fi
}

\newcommand\tndeftonestyle[3]{%
  \expandafter\def\csname tn@ton-#1-#2-style\endcsname{#3}%
}
\newcommand\undeftonestyle[2]{
  \expandafter\let\csname tn@ton-#1-#2-style\endcsname\relax%
}
\tikzstyle tonnetz node=[fill=white,shape=circle,draw,
minimum size=8mm,inner sep=0pt]

\def\tn@node#1#2{%
  \edef\tempa{\noexpand\draw (#1,#2) node (ton-#1-#2)[tonnetz node%
    \@ifundefined{tn@ton-#1-#2-style}{}{%
      ,\csname tn@ton-#1-#2-style\endcsname%
    }] {\noexpand\tnnodetext{#1}{#2}};}%
  \tempa
}


% (row from, row to, column from, column to)
\newcommand\tonnetz[4]{%
  \node (lu) at (#3,#1) {};
  \draw (#4,#2)+(0.5,0.5) node (grl){};
  \draw (lu)+(-0.5,-0.5) grid (grl);
%  \foreach \r in {#1,...,#2} \draw (#3,\r) -- (#4, \r);
%  \foreach \c in {#3,...,#4} \draw (\c,#1) -- (\c, #2);
  \foreach \r in {#1,...,#2}
  \foreach \c in {#3,...,#4}
  {
  \tn@node{\c}{\r}
}
%   \@tempcntb#1
%   \advance\@tempcntb by -1
%   \@whilenum \@tempcntb < #2 \do {%
%     \advance\@tempcntb by 1
%     \expandafter\tn@row\expandafter{\the\@tempcntb}{#3}{#4}%
%     \par
%   }%
}

\def\tn@add#1#2#3{%
  \@tempcnta#2
  \@tempcntb#3
  \advance\@tempcnta by \@tempcntb
  \edef#1{\the\@tempcnta}%
}
\def\tn@mkstylecoords#1#2#3#4{%
  \tn@add\tn@tempa{#1}{#2}%
  \tn@add\tn@tempb{#3}{#4}%
  \tndeftonestyle\tn@tempa\tn@tempb
}
\tikzstyle tonnetz active node=[fill=skyblue,text=darkblue,draw=darkblue]
\tikzstyle tonnetz center node=[fill=turquoise!20,text=darkblue,draw=darkblue]
\newcommand\fundamentalsystem[2]{%
  \tn@mkstylecoords{#1}{-1}{#2}{1}{%
    tonnetz active node}%
  \tn@mkstylecoords{#1}{0}{#2}{1}{%
    tonnetz active node}%
  \tn@mkstylecoords{#1}{1}{#2}{1}{%
    tonnetz active node}%
  \tn@mkstylecoords{#1}{2}{#2}{1}{%
    tonnetz active node}%
  \tn@mkstylecoords{#1}{-2}{#2}{0}{%
    tonnetz active node}%
  \tn@mkstylecoords{#1}{-1}{#2}{0}{%
    tonnetz active node}%
  \tn@mkstylecoords{#1}{0}{#2}{0}{%
    tonnetz center node}%
  \tn@mkstylecoords{#1}{1}{#2}{0}{%
    tonnetz active node}%
  \tn@mkstylecoords{#1}{2}{#2}{0}{%
    tonnetz active node}%
  \tn@mkstylecoords{#1}{-1}{#2}{-1}{%
    tonnetz active node}%
  \tn@mkstylecoords{#1}{0}{#2}{-1}{%
    tonnetz active node}%
  \tn@mkstylecoords{#1}{1}{#2}{-1}{%
    tonnetz active node}%
  \draw (#1,#2) ++(-2.6,0) node(fundamental1){}%
  ++(1.5,1.5) node(fundamental2){}%
  ++(3.6,0) node(fundamental3){}%
  ++(0,-1.6) node(fundamental4){}%
  ++(-1.4,-1.4) node(fundamental5){}%
  ++(-2.2,0) node(fundamental6){};  
}