# Prevent two \psmatrix objects from connecting to each other

I'm attempting to draw graphs using pstricks' \psmatrix command with the following code:

\documentclass{article}
\usepackage{pstricks,pst-node,pst-tree}

\def\e{\strut \hphantom{.}}

\newenvironment{graph}
{
\begin{center}
$\psmatrix[rowsep=1cm,colsep=1.5cm,mnode=circle] } { \endpsmatrix$
\end{center}
}

\begin{document}

\begin{graph}
\e & \e & \e & \e & \e
\ncline{1,1}{1,2}
\ncline{1,2}{1,3}
\ncline{1,3}{1,4}
\ncline{1,4}{1,5}
\end{graph}

\begin{graph}
& \e \\
\e & \e & \e & \e
\ncline{1,1}{1,2}
\ncline{1,2}{1,3}
\ncline{1,3}{1,4}
\ncline{1,2}{2,2}
\end{graph}

\end{document}


It produces the following output, where the top row of circles come from the first graph, and the second and third rows are from the second graph: I would expect that every pair of \psmatrix, \endpsmatrix would be independent of each other, but instead the two graphs seem to be using the same set of nodes. For example, if I remove all of the \nclines from the first graph, I get this result: How would I separate the two graphs so that code from one doesn't affect the other?

That is the correct behaviour! Your second example has only one cell in the first row, the reason why the \ncline will take the cells from the first row of the first example.

\documentclass{article}
\usepackage{pstricks,pst-node,pst-tree}

\def\e{\strut \hphantom{.}}

\newenvironment{graph}
{
\begin{center}
\psmatrix[rowsep=1cm,colsep=1.5cm,mnode=circle]
}
{
\endpsmatrix
\end{center}
}

\begin{document}

\begin{graph}
\e & \e & \e & \e & \e
\ncline{1,1}{1,2}
\ncline{1,2}{1,3}
\ncline{1,3}{1,4}
\ncline{1,4}{1,5}
\end{graph}

\vspace{1cm}
\psset{linecolor=red}

\begin{graph}
\e & \e & \e & \e\\
\e & \e & \e & \e
\ncline{1,1}{1,2}
\ncline{1,2}{1,3}
\ncline{1,3}{1,4}
\ncline{1,2}{2,2}
\end{graph}

\end{document} • Ah. I understand now where I went wrong. I had the indexes backwards. It's surprising, though, that it takes nodes from the previous psmatrix, rather than either initializing empty nodes or throwing an error. – Brian Jan 13 '16 at 12:57
• {1,3} is taken als nodename. And it is not redefined in the second example – user2478 Jan 13 '16 at 14:21