% Copyright (C) 2016, 2023, 2025 Einar Halvorsen
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tensormatrix}[2025/08/08 v1.0.0 Package for displaying structure of tensor matrix-representations]
 
\RequirePackage{tikz}

\DeclareOption*{\PackageWarning{tensormatrix}{Unknown ‘\CurrentOption’}}
\ProcessOptions\relax

\tikzstyle{tkp}=[circle,fill=black,inner sep=0pt,minimum size=4pt]  
\tikzstyle{tkn}=[circle,draw=black,fill=white,inner sep=0pt,minimum size=4pt]  
\tikzstyle{tkz}=[circle,draw=black,fill=black,inner sep=0pt,minimum size=1pt]  
\newlength{\tmatds}
\setlength{\tmatds}{12pt}
\newlength{\tmatbndx}
\newlength{\tmatbndy}

\newenvironment{tmat}[2]{
  \newcommand{\tmatm}{#1}
  \newcommand{\tmatn}{#2}
  \setlength{\tmatbndx}{\tmatn\tmatds}
  \addtolength{\tmatbndx}{0.25\tmatds}
  \setlength{\tmatbndy}{\tmatm\tmatds}
  \addtolength{\tmatbndy}{0.25\tmatds}
  \left[
  \begin{tikzpicture}[baseline={([yshift=-0.25\tmatds] current bounding box.west)}]
    \foreach \m in {1,...,\tmatm}
    \foreach \n in {1,...,\tmatn}
    \draw (\n*\tmatds,-\m*\tmatds) node[tkz] {};
    \draw(0.75*\tmatds,-0.75*\tmatds) node {};
    \draw(\tmatbndx,-0.75*\tmatds) node {};
    \draw(0.75*\tmatds,-\tmatbndy) node {};
}{%
  \end{tikzpicture}
  \right]
}

\newcommand{\tmatpv}[2]{%
\node (#1 #2) at (#2\tmatds,-#1\tmatds) [tkp] {};
}
\newcommand{\tmatpn}[2]{%
\node (#1 #2) at (#2\tmatds,-#1\tmatds) [tkn] {};
}
\newcommand{\tmatpx}[2]{%
  \node (#1 #2) at (#2\tmatds,-#1\tmatds) [] {};
  \draw[line width=1pt,line cap=round,fill=black] (#2\tmatds-2.5pt,-#1\tmatds + 2.5pt) -- (#2\tmatds + 2.5pt,-#1\tmatds - 2.5pt);
  \draw[line width=1pt,line cap=round,fill=black] (#2\tmatds-2.5pt,-#1\tmatds - 2.5pt) -- (#2\tmatds + 2.5pt,-#1\tmatds + 2.5pt);
}
\newcommand{\tmatpdv}[2]{%
  \node (#1 #2) at (#2\tmatds,-#1\tmatds) [] {};
  \draw (#2\tmatds,-#1\tmatds) circle (3pt);
  \draw[fill=black] (#2\tmatds,-#1\tmatds) circle (1.2pt);
}
\newcommand{\tmatpdn}[2]{%
  \node (#1 #2) at (#2\tmatds,-#1\tmatds) [] {};
  \draw (#2\tmatds,-#1\tmatds) circle (3pt);
  \draw[line width=0.5pt,fill=white] (#2\tmatds,-#1\tmatds) circle (1.2pt);
}

\newcommand{\tmatlink}[2]{%
  \draw[line width=1pt,line cap=round,fill=black] (#1) -- (#2);
}
