Graphics, Figures & Tablespst-plot | "\savedata" with "\newcommand"

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
mamboreina79
Posts: 5
Joined: Fri Nov 25, 2011 8:49 pm

pst-plot | "\savedata" with "\newcommand"

Post by mamboreina79 »

Hello, I just joined today and this is my first post, I couldn't find anything here or on the web about this problem and I don't know enough about LaTeX to figure out how to fix it.

I'm using pst-plot package to make a figure that shows several plots with the same style. Like this:
3 beautiful plots with same style :)
3 beautiful plots with same style :)
kk.jpg (89.28 KiB) Viewed 2976 times
I want to define my own pspicture with a command before the begin{document} tag, so that I can just enclose the plotting information inside a tag in the body, thus separating style from content.

This is the smallest minimal example code I was able to write that will show the first of the plots:

Code: Select all

\documentclass[12pt]{article} 
\usepackage[english]{babel}
\usepackage{graphicx,color}
\usepackage{pstricks,pst-plot,pstricks-add}

% The size of my figure:
\newcommand*{\ancho}{125.22mm}
\newcommand*{\alto} {270.26mm}
\special{papersize=\ancho,\alto}

% Free canvas: (basically zero everything)
\setlength{\hoffset}{-1in}	\setlength{\voffset}{-1in}
\setlength{\marginparsep}{0cm}	\setlength{\marginparwidth}{0cm}
\setlength{\oddsidemargin}{0cm}	\setlength{\evensidemargin}{0cm} \setlength{\marginparpush}{0cm}
\setlength{\topmargin}{0cm}	\setlength{\footskip}{0cm}
\setlength{\headsep}{0cm}	\setlength{\headheight}{0cm}
\setlength{\textwidth} {\ancho}	\setlength{\textheight} {\alto}
\setlength{\paperwidth}{\ancho}	\setlength{\paperheight}{\alto}

\begin{document}
  \pagestyle{empty} % No page numbering 'cos it's a figure
  \centering
  \psset{xunit=0.01748mm, yunit=1209.21mm, 
         plotstyle=bar, barwidth=2pt, fillstyle=solid, fillcolor=gray
         }
  \begin{pspicture}(-1350,-0.008)(5810,0.0665)
    \psaxes[Dx=1000,Dy=0.02] (0,0)(5735,0.065)
    \savedata{\mydata}[482,0.06]
    \listplot{\mydata}
  \end{pspicture}
\end{document}
And what I want to do is something like this:

Code: Select all

\documentclass[12pt]{article} 
\usepackage[english]{babel}
\usepackage{graphicx,color}
\usepackage{pstricks,pst-plot,pstricks-add}

% The size of my figure:
\newcommand*{\ancho}{125.22mm}
\newcommand*{\alto} {270.26mm}
\special{papersize=\ancho,\alto}

% Free canvas: (basically zero everything)
\setlength{\hoffset}{-1in}	\setlength{\voffset}{-1in}
\setlength{\marginparsep}{0cm}	\setlength{\marginparwidth}{0cm}
\setlength{\oddsidemargin}{0cm}	\setlength{\evensidemargin}{0cm} \setlength{\marginparpush}{0cm}
\setlength{\topmargin}{0cm}	\setlength{\footskip}{0cm}
\setlength{\headsep}{0cm}	\setlength{\headheight}{0cm}
\setlength{\textwidth} {\ancho}	\setlength{\textheight} {\alto}
\setlength{\paperwidth}{\ancho}	\setlength{\paperheight}{\alto}

% ----------------- MY PSPICTURE ------------------------
\newcommand{\myfig}[1]{
  \psset{xunit=0.01748mm, yunit=1209.21mm, 
         plotstyle=bar, barwidth=2pt, fillstyle=solid, fillcolor=gray
         }
  \begin{pspicture}(-1350,-0.008)(5810,0.0665)
    \psaxes[Dx=1000,Dy=0.02] (0,0)(5735,0.065)
    #1     % --------> MY PLOTTING STUFF
  \end{pspicture}
}

\begin{document}
  \pagestyle{empty} % No page numbering 'cos it's a figure
  \centering
  \myfig{ % MY CALL INSIDE THE DOCUMENT
    \savedata{\mydata}[482,0.06]
    \listplot{\mydata}
  }

% ANOTHER PLOT WITH THE SAME STYLE
% \myfig{ ...STUFF... }
% ANOTHER PLOT WITH THE SAME STYLE
% \myfig{ ...STUFF... }
% ETC.
\end{document}
I'm compiling like this:

Code: Select all

latex myfig
dvips myfig.dvi -o myfig.ps
ps2pdf myfig.ps myfig.pdf
on an Ubuntu 10.11 and using gedit for the writing.

The first example, using pspicture inside the document, works fine.
The second example, calling an external pspicture, doesn't. I get a plot with nothing inside it, and an error in the last step when trying to convert to pdf. It starts with "Error: /undefined in 482,0.06", which is the point I am passing to savedata to make the plot.
Error: /undefined in 482,0.06
Operand stack:
--nostringval--
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1910 1 3 %oparray_pop 1909 1 3 %oparray_pop 1893 1 3 %oparray_pop 1787 1 3 %oparray_pop --nostringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval--
Dictionary stack:
--dict:1161/1684(ro)(G)-- --dict:0/20(G)-- --dict:119/200(L)-- --dict:176/300(L)-- --dict:38/200(L)-- --dict:93/200(L)--
Current allocation mode is local
Last OS error: 2
Current file position is 137116
GPL Ghostscript 9.04: Unrecoverable error, exit code 1
Do you have any idea of why is it happening? Is there a better way to do this? Thanks in advance ;-)
"Light travels faster than sound. This is why some people appear bright until you hear them speak"

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

mamboreina79
Posts: 5
Joined: Fri Nov 25, 2011 8:49 pm

pst-plot | "\savedata" with "\newcommand"

Post by mamboreina79 »

I have been playing a bit more with this and I think there is no problem with passing "\data" to an external function. I defined a small new command "\myplot" that works like "\myfig", but substitutes the "\listplot" command.

So basically I used the same code of the second example and added two lines before the "\begin{document}":

Code: Select all

% ...
% ...Same header as before...
% ...

% --- TWO NEW LINES: ----
\newcommand{\myplot}[2]{
  \listplot[fillcolor=gray,#1]{#2}
}

    \begin{document}
      \pagestyle{empty} % No page numbering 'cos it's a figure
      \centering

% --- USING THE OLD AND NEW COMMANDS ----
\myfig % <----------------------- OLD
  { 
  \readdata{\data}{data.dat}
  \myplot{}{\data} % <----------- NEW
  }

    % ANOTHER PLOT WITH THE SAME STYLE
    % \myfig{ ...STUFF... }
    % ANOTHER PLOT WITH THE SAME STYLE
    % \myfig{ ...STUFF... }
    % ETC.
  \end{document}
The content of data.dat is 482 0.06.
This code works fine and reproduces the same results as the first example of my post. So the problem is something with the "\savedata" command... and not with passing "\data"... but I still dunno what :(

Why can't I pass data read with "\savedata" to the "\myfig" command ??
I would appreciate any help on this.
"Light travels faster than sound. This is why some people appear bright until you hear them speak"
mamboreina79
Posts: 5
Joined: Fri Nov 25, 2011 8:49 pm

Re: pst-plot | "\savedata" with "\newcommand"

Post by mamboreina79 »

Helloooo....
"Light travels faster than sound. This is why some people appear bright until you hear them speak"
Post Reply