[Tex/LaTex] Matrix from graph description

graphsmatrices

Are there any LaTeX package that draws/plots/generates a matrix from a "graph" description (node relations). Any type of description/language could be useful. The trick is to generate automaticly a matrix from a description of "relations".

For example, from the following "mathematical" relation:

(1, 1)
(1, 2)
(1, 5)
(2, 1)
(2, 3)
(2, 5)
(3, 2)
(3, 4)
(4, 3)
(4, 5)
(4, 6)
(5, 1)
(5, 2)
(5, 4)
(6, 4)

which represents the next graph:

An example graph

to get the following matrix representation of this relation:

Matrix representation

Are there packages available for LaTeX to accomplish this type of "translation"? (from the mathematical relation to the matrix representation of its corresponding graph).

Best Answer

Here is one way to do it (with much room for improvement). Given a list of node relations:

\newcommand{\NodeRelations}{%
    (1,1),
    (1,2),
    (1,5),
    (2,1),
    (2,3),
    (2,5),
    (3,2),
    (3,4),
    (4,3),
    (4,5),
    (4,6),
    (5,1),
    (5,2),
    (5,4),
    (6,4)
}%

you use the MyMatrix environment and pass in dummy data:

\begin{MyMatrix}{\NodeRelations}
    - & - & - & - & - & - \\
    - & - & - & - & - & - \\
    - & - & - & - & - & - \\
    - & - & - & - & - & - \\
    - & - & - & - & - & - \\
    - & - & - & - & - & - \\
\end{MyMatrix}

which gets replaced with a 1 or 0:

enter image description here

Code:

\documentclass{article}
\usepackage{pgffor}
\usepackage{xstring}
\usepackage{collcell}
\usepackage{etoolbox}

\newcounter{RowCount}
\newcounter{ColCount}

\newcommand*{\NextColumn}[1]{%
    \addtocounter{ColCount}{1}%
    \IfIsInNodeRelations{\arabic{ColCount}}{\arabic{RowCount}}{1}{0}%
}
\newcommand*{\FirstColumn}[1]{%
    \setcounter{ColCount}{0}%
    \addtocounter{RowCount}{1}%
    \NextColumn{#1}%
}


\newtoggle{IsInNodeRelations}
\newcommand*{\IfIsInNodeRelations}[4]{%
    \global\togglefalse{IsInNodeRelations}%
    \foreach \Node in \NodeRelations {%
        \StrBetween{\Node}{(}{,}[\x]
        \StrBetween{\Node}{,}{)}[\y]
        \IfEq{\x}{#1}{%
            \IfEq{\y}{#2}{%
                \global\toggletrue{IsInNodeRelations}%
                \breakforeach%
            }{}%
        }{}%
    }%
    \iftoggle{IsInNodeRelations}{#3}{#4}
}

\newcommand{\NodeRelations}{%
    (1,1),
    (1,2),
    (1,5),
    (2,1),
    (2,3),
    (2,5),
    (3,2),
    (3,4),
    (4,3),
    (4,5),
    (4,6),
    (5,1),
    (5,2),
    (5,4),
    (6,4)
}%

\newcommand*{\ExtractedX}{}%
\newcommand*{\ExtractedY}{}%
\def\ExtractXY(#1,#2){%
    \xdef\ExtractedX{#1}%
    \xdef\ExtractedY{#2}%
}%

\newenvironment{MyMatrix}[1]{%
    % #1 = List of node relations
    \newcolumntype{F}{>{\collectcell\FirstColumn}c<{\endcollectcell}}%
    \newcolumntype{C}{>{\collectcell\NextColumn}c<{\endcollectcell}}%
    \setcounter{RowCount}{0}
    \setcounter{ColCount}{0}
    $\left[\begin{array}{F C C C C C}%
}{%
    \end{array}\right]$%
}%

\begin{document}


\begin{MyMatrix}{\NodeRelations}
    - & - & - & - & - & - \\
    - & - & - & - & - & - \\
    - & - & - & - & - & - \\
    - & - & - & - & - & - \\
    - & - & - & - & - & - \\
    - & - & - & - & - & - \\
\end{MyMatrix}
\end{document}