3

I'm plotting a line from a file with pstricks using the following code:

\begin{pspicture}(0,0)(10,10)
    \saveDataAsNodes{some-file.data}{N}
    \psncurve{->}(0,6){N}
    \cnodeput(N0){A}{A}
    \cnodeput(N6){B}{B}
\end{pspicture}

The points at the beginning and the end should be labeled with A and B respectively.

With my current code I always have to adapt two positions in the code to match the number of points in the file (7 in this case, hence \psncurve{->}(0,6){N} and \cnodeput(N6){B}{B}).

Is there any way to adapt the graphic automatically to the number of points in the file?

2 Answers2

4

I guess you want \psLoopIndex from p. 41 of the pstricks documentation.

\documentclass[a4paper]{article}
\usepackage{filecontents} % from https://tex.stackexchange.com/questions/194752/plotting-a-directed-graph-using-pgfplots/194806#194806
\begin{filecontents*}{node2.dat}
12  12
12  13
13  15
15  15
15  13
13  12
12  10
10  8
8   8
8   10
10  12
12  12
12  10
10  10
\end{filecontents*}
\usepackage{pst-node,pst-plot}
\begin{document}
\begin{pspicture}(6.5,6.5)(16,16)
    \saveDataAsNodes{node2.dat}{N}
    \newcount\iB
    \iB=\numexpr\the\psLoopIndex-1\relax % from the pstricks documentation
    \typeout{read \the\iB coordinates}
    \psncurve{->}(0,\the\iB){N}
    \cnodeput(N0){A}{A}
    \cnodeput(N\the\iB){B}{B}
\end{pspicture}
\end{document}
4

With the pst-node.tex from http://archiv.dante.de/~herbert/texnik/tex/generic/pst-node/ you can use the node #1Last:

\documentclass[a4paper]{article}    
\usepackage{pstricks-add}
\begin{document}
    \begin{pspicture}[showgrid](6.5,6.5)(16,16)
    \saveDataAsNodes{node2.dat}{N}
    \psncurve{->}(0,\the\psLoopIndex){N}
    \cnodeput(N0){A}{A}
    \cnodeput(NLast){B}{B}
    \end{pspicture}
\end{document}

enter image description here