[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: pstrick



b> 
> > 2) It would also be nice to have the option to export to some format that can
> >    be used "as is" in some latex documents eg eps. Or if it could genetate
> >    some pstricks code.
> 
> That shouldn't be too hard. I'll try to massage the printing
> functionality into an EPS exporter.
> 

Well I got some free time and I wrote a perl script that converts a gt 
graph to a latex file using the package pstricks. It is really  
straightforward and pstricks is a good format becuse one can easily edit 
the resulting latex file. I thing it shouldn't be hard to have gt produce 
pstrick code. 

Anyway I attach the perl script in case any one wants to use it. I am not 
very experienced in perl so don't expect anything elegant. It does work 
though. I named it gt2pst.pl.

Best

Nikos.     

------------- The script ---------------

#! /usr/bin/perl

# use strict;
# use warnings;


# This converts the output of graphthing (a file with gt ".gt") to a
# LaTeX picture using the pstricks package. The graphthing thing
# format is very similar to the pstricks format so that this was an
# easy exercise. Put this file in your path and give it executive
# rights "chmod 755 gt2pst.pl". If your perl executable is not in
# /usr/bin you should change the first line accordingly.  The usage
# is
#
#		   gt2pst.pl anything original.gt
#
# and the output is a LaTeX file original.gt.tex. If anything is 0
# the produced graph has "fat dots" as vertices and it looks very
# much like the graph you draw in graphthing. If anything is not 0
# (I suggest to use 1 in this case) the nodes are drawn as encircled
# letters (their labels). I hope the code is self explanatory. It
# parses the gt file and collects the nodes, their names and their
# coordinates as well as the edges, the names of the edges they
# connect and their kind ({--} or {->}).  All these data are
# converted into their pstricks counterparts in a straightforward
# manner. The only slight hassle is that gt measures hight "upside
# down" so that the y-coordinates of the nodes have first to be
# turned "downside up". If this is not clear you might want to
# compare the "original.gt" file with the output "original.gt.tex".
#
# If you want to use this consider it under the GPL version 2, or at
# your option, any later version.
#
# nea

my $switch = $ARGV[0];
my $file = $ARGV[1];

open(CSV,$file) || die "Cannot read from $file\n";
open (TMP,">$file.$$") || die "Cannot write to $file.$$\n" ;

my $nindex = 0;
my $eindex = 0;

my @ycoordinates = ();
my @nodes = ();
my @edges = ();

while (<CSV>){
  my @list = split(/ /);
  s/^"|"$//g foreach @list;
  if ($_=~/^vertex/){
    $nodes[$nindex] = $list[1]; 
    $nodes[$nindex+1] = $list[3];
    $nindex = $nindex + 2 ;
  }
  else{
    if ($_=~/^edge/){
      $edges[$eindex] = $list[1]; 
      $edges[$eindex+1] = $list[2];
      $edges[$eindex+2] = $list[3];
      $eindex = $eindex+3 ;
    }
  }
}

close CSV;

my @nodescoor = ();

my $i = 0;
for ($i = 0; $i < $#nodes/2; $i++) {
  $nodescoor[$i] = $nodes[2*$i+1];
}


my @ycoord = @nodescoor;
my @xcoord = @nodescoor;

s/^\(\d+,(\d+)\)/\1/ foreach @ycoord;
s/^\((\d+),\d+\)/\1/ foreach @xcoord;

my @sortycoord = sort @ycoord;
my @sortxcoord = sort @xcoord;

# This is the preamble and the beginning of the body. I needed the
# largest and smaller x and y coordinates to get the size of the
# image.

print TMP "\\documentclass\{article\}\n
\\usepackage\{pstricks,pst-node\}\n
\\begin\{document\}\n
\\psset\{unit=1pt\}\n
\\psset\{arrowsize=5pt 2\}\n
\\psset\{dotsize=3pt 2\}\n
\\begin\{pspicture\}\($sortxcoord[0],$sortycoord[0]\)\($sortxcoord[$#sortxcoord],$sortycoord[$#sortycoord]\)\n";

# End of the beginning stuff.


$i = 0;
for ($i = 0; $i < $#ycoord+1; $i++) {                     #  This turns gt
  $ycoord[$i] = $sortycoord[$#sortycoord] - $ycoord[$i];  #  coordinates
}                                                         #  upside down.

print TMP "%--------------- The nodes ---------------%\n";
$i = 0;
for ($i = 0; $i < $#ycoord+1; $i++) {
  if ($ARGV[0] == 0 ){
    print TMP "\\dotnode\($xcoord[$i],$ycoord[$i]\)\{$nodes[2*$i]\}\n";
  } else {
    print TMP "\\cnodeput\($xcoord[$i],$ycoord[$i]\)\{$nodes[2*$i]\}\{$nodes[2*$i]\}\n";
  }
}

print TMP "%--------------- The edges ---------------%\n";
$i = 0;
for ($i = 0; $i < $#edges/3; $i++) {
  print TMP "\\ncline\{$edges[3*$i+1]\}\{$edges[3*$i]\}\{$edges[3*$i+2]\}\n";
}


print TMP "\\end\{pspicture\}\n \\end\{document\}";


close (TMP);
rename "$file.$$","$file.tex" || die "Cannot update $file\n";

#! /usr/bin/perl

# use strict;
# use warnings;


# This converts the output of graphthing (a file with gt ".gt") to a
# LaTeX picture using the pstricks package. The graphthing thing
# format is very similar to the pstricks format so that this was an
# easy exercise. Put this file in your path and give it executive
# rights "chmod 755 gt2pst.pl". If your perl executable is not in
# /usr/bin you should change the first line accordingly.  The usage
# is
#
#		   gt2pst.pl anything original.gt
#
# and the output is a LaTeX file original.gt.tex. If anything is 0
# the produced graph has "fat dots" as vertices and it looks very
# much like the graph you draw in graphthing. If anything is not 0
# (I suggest to use 1 in this case) the nodes are drawn as encircled
# letters (their labels). I hope the code is self explanatory. It
# parses the gt file and collects the nodes, their names and their
# coordinates as well as the edges, the names of the edges they
# connect and their kind ({--} or {->}).  All these data are
# converted into their pstricks counterparts in a straightforward
# manner. The only slight hassle is that gt measures hight "upside
# down" so that the y-coordinates of the nodes have first to be
# turned "downside up". If this is not clear you might want to
# compare the "original.gt" file with the output "original.gt.tex".
#
# If you want to use this consider it under the GPL version 2, or at
# your option, any later version.
#
# nea

my $switch = $ARGV[0];
my $file = $ARGV[1];

open(CSV,$file) || die "Cannot read from $file\n";
open (TMP,">$file.$$") || die "Cannot write to $file.$$\n" ;

my $nindex = 0;
my $eindex = 0;

my @ycoordinates = ();
my @nodes = ();
my @edges = ();

while (<CSV>){
  my @list = split(/ /);
  s/^"|"$//g foreach @list;
  if ($_=~/^vertex/){
    $nodes[$nindex] = $list[1]; 
    $nodes[$nindex+1] = $list[3];
    $nindex = $nindex + 2 ;
  }
  else{
    if ($_=~/^edge/){
      $edges[$eindex] = $list[1]; 
      $edges[$eindex+1] = $list[2];
      $edges[$eindex+2] = $list[3];
      $eindex = $eindex+3 ;
    }
  }
}

close CSV;

my @nodescoor = ();

my $i = 0;
for ($i = 0; $i < $#nodes/2; $i++) {
  $nodescoor[$i] = $nodes[2*$i+1];
}


my @ycoord = @nodescoor;
my @xcoord = @nodescoor;

s/^\(\d+,(\d+)\)/\1/ foreach @ycoord;
s/^\((\d+),\d+\)/\1/ foreach @xcoord;

my @sortycoord = sort @ycoord;
my @sortxcoord = sort @xcoord;

# This is the preamble and the beginning of the body. I needed the
# largest and smaller x and y coordinates to get the size of the
# image.

print TMP "\\documentclass\{article\}\n
\\usepackage\{pstricks,pst-node\}\n
\\begin\{document\}\n
\\psset\{unit=1pt\}\n
\\psset\{arrowsize=5pt 2\}\n
\\psset\{dotsize=3pt 2\}\n
\\begin\{pspicture\}\($sortxcoord[0],$sortycoord[0]\)\($sortxcoord[$#sortxcoord],$sortycoord[$#sortycoord]\)\n";

# End of the beginning stuff.


$i = 0;
for ($i = 0; $i < $#ycoord+1; $i++) {                     #  This turns gt
  $ycoord[$i] = $sortycoord[$#sortycoord] - $ycoord[$i];  #  coordinates
}                                                         #  upside down.

print TMP "%--------------- The nodes ---------------%\n";
$i = 0;
for ($i = 0; $i < $#ycoord+1; $i++) {
  if ($ARGV[0] == 0 ){
    print TMP "\\dotnode\($xcoord[$i],$ycoord[$i]\)\{$nodes[2*$i]\}\n";
  } else {
    print TMP "\\cnodeput\($xcoord[$i],$ycoord[$i]\)\{$nodes[2*$i]\}\{$nodes[2*$i]\}\n";
  }
}

print TMP "%--------------- The edges ---------------%\n";
$i = 0;
for ($i = 0; $i < $#edges/3; $i++) {
  print TMP "\\ncline\{$edges[3*$i+1]\}\{$edges[3*$i]\}\{$edges[3*$i+2]\}\n";
}


print TMP "\\end\{pspicture\}\n \\end\{document\}";


close (TMP);
rename "$file.$$","$file.tex" || die "Cannot update $file\n";