%
%	plotting.tex - TeX Plotting Macros
%
%	author: Laurence G. Yaffe (lgy@newton.phys.washington.edu)	1987
%
\setbox0 = \hbox{$0$}%
\newdimen \digitheight		\digitheight  = \ht0
\newdimen \titlespacing		\titlespacing = 1.5 ex
\newdimen \labelspacing		\labelspacing = 0.9 ex
\newdimen \labelwidth		\labelwidth   = 0.0 pt
\newdimen \ticklength		\ticklength   = 0.7 em
\newdimen \plotheight		\plotheight   = 5.0 in
\newdimen \plotwidth		\plotwidth    = 5.0 in
\newdimen \linewidth		\linewidth    = 0.241 pt
\newskip  \padding		\padding      = 0pt plus 1fil
%
\newbox	\xAxis
\newbox	\XAxis
\newbox	\yAxis
\newbox	\YAxis
\newbox	\PlotData
\newbox	\PlotBox
%
\def\ratio	#1:#2%		adjust padding for log scale plotting
    {%
    \count255 = #1#2
    \ifnum \count255=11	\global\padding = 0pt plus 1.00000 fil \else
    \ifnum \count255=12	\global\padding = 0pt plus 0.30103 fil \else
    \ifnum \count255=23	\global\padding = 0pt plus 0.17609 fil \else
    \ifnum \count255=34	\global\padding = 0pt plus 0.12494 fil \else
    \ifnum \count255=45	\global\padding = 0pt plus 0.09691 fil \else
    \ifnum \count255=56	\global\padding = 0pt plus 0.07918 fil \else
    \ifnum \count255=67	\global\padding = 0pt plus 0.06695 fil \else
    \ifnum \count255=78	\global\padding = 0pt plus 0.05799 fil \else
    \ifnum \count255=89	\global\padding = 0pt plus 0.05115 fil \else
    \ifnum \count255=91	\global\padding = 0pt plus 0.04576 fil \else
    \ifnum \count255=13	\global\padding = 0pt plus 0.47712 fil \else
    \ifnum \count255=14	\global\padding = 0pt plus 0.60206 fil \else
    \ifnum \count255=24	\global\padding = 0pt plus 0.30103 fil \else
    \ifnum \count255=25	\global\padding = 0pt plus 0.39794 fil \else
    \ifnum \count255=26	\global\padding = 0pt plus 0.47712 fil \else
    \ifnum \count255=36	\global\padding = 0pt plus 0.30103 fil \else
    \ifnum \count255=46	\global\padding = 0pt plus 0.17609 fil \else
    \ifnum \count255=61	\global\padding = 0pt plus 0.22185 fil \else
    \ifnum \count255=51	\global\padding = 0pt plus 0.30103 fil \else
    \ifnum \count255=41	\global\padding = 0pt plus 0.39794 fil \else
    \ifnum \count255=31	\global\padding = 0pt plus 0.52288 fil \else
    \ifnum \count255=21	\global\padding = 0pt plus 0.69897 fil \else
			\global\padding = 0pt
    \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi
    }%
%
\def\centered	#1{\vbox to 0pt {\vss \hbox to 0pt {\hss #1\hss}\vss}}%
%
\def\dash	#1%
    {%
    \vbox {\hrule height 0.5 \linewidth depth 0.5 \linewidth width #1}%
    }%
%
\def\yaxis	#1#2%
    {%
    \global\setbox \yAxis = \hbox
	{%
	\def \temp {#1}%
	\ifx \temp\empty \else
	    \vbox to \plotheight {\vss \hbox {#1}\vss}%
	    \kern 0.7 \titlespacing
	\fi
	\def \temp {#2}%
	\ifx \temp\empty \else
	    \labelwidth = 0pt
	    \setbox1=\box0\ytic #2,\cr,%
	    \ifnum \labelwidth = 0 \kern -\labelspacing \fi
	    \hbox to \labelwidth {\hfil}\box0%
	\fi
	\hbox to 0pt
	    {\hss \vrule height \plotheight depth 0pt width \linewidth \hss}%
	}%
    }%
\def\ytic	#1,%
    {%
    \def \temp {#1}%
    \def \quit {\cr}%
    \ifx \temp\quit
	\let\next=\relax
	\setbox0 = \vbox to \plotheight {\unvbox0 \unskip}%
    \else
	\let\next=\ytic \skip0 = \padding
	\setbox1 = \hbox {\tenpoint $#1$}%
	\ifnum \labelwidth < \wd1
	       \labelwidth = \wd1
	\fi
	\setbox1 = \hbox
	    {%
	    \smash
		{%
		\ifnum \wd1 = 0
		    \kern \labelspacing
		    \hbox to 0pt {\dash {0.67 \ticklength}\hss}%
		\else
		    \lower 0.42 \digitheight \hbox to 0pt {\hss \unhbox1}%
		    \kern \labelspacing
		    \hbox to 0pt {\dash \ticklength \hss}%
		\fi
		}%
	    }%
	\setbox0 = \vbox to \plotheight {\box1 \vskip \skip0 \unvbox0}%
    \fi
    \next
    }%
%
\def\Yaxis	#1#2%
    {%
    \global\setbox \YAxis = \hbox
	{%
	\hbox to 0pt
	    {\hss \vrule height \plotheight depth 0pt width \linewidth \hss}%
	\def \temp {#2}%
	\ifx \temp\empty \else \setbox1=\box0 \Ytic #2,\cr,\box0 \fi
	\def \temp {#1}%
	\ifx \temp\empty \else
	    \kern 0.7 \titlespacing
	    \vbox to \plotheight {\vss \hbox {#1}\vss}%
	\fi
	}%
    }%
\def\Ytic	#1,%
    {%
    \def \temp {#1}%
    \def \quit {\cr}%
    \ifx \temp\quit
	\let\next=\relax
	\setbox0 = \vbox to \plotheight {\unvbox0 \unskip}%
    \else
	\let\next=\Ytic \skip0=\padding
	\setbox1 = \hbox {\tenpoint $#1$}%
	\setbox1 = \hbox
	    {%
	    \smash
		{%
		\ifnum \wd1=0
		    \hbox to 0pt {\hss \dash {0.67 \ticklength}}%
		\else
		    \hbox to 0pt {\hss \dash \ticklength}%
		    \kern \labelspacing
		    \lower 0.42 \digitheight \box1%
		\fi
		}%
	    }%
	\setbox0 = \vbox to \plotheight {\box1 \vskip \skip0 \unvbox0}%
    \fi
    \next
    }%
%
\def\xaxis	#1#2%
    {%
    \global\setbox \xAxis = \vtop
	{%
	\smash {\dash \plotwidth}%
	\nointerlineskip
	\def \temp {#2}%
	\ifx \temp\empty \else \hbox to \plotwidth {\xtic #2,\cr,}\fi
	\nointerlineskip
	\def \temp {#1}%
	\ifx \temp\empty \else
	    \kern 0.7 \titlespacing
	    \hbox to \plotwidth {\hss #1\hss}%
	\fi
	}%
    }%
\def\xtic	#1,%
    {%
    \def \temp {#1}%
    \def \quit {\cr}%
    \ifx \temp\quit
	\let\next=\unskip
    \else
	\let\next=\xtic
	\vtop
	    {%
	    \setbox1 = \hbox to 0pt {\hss \tenpoint $#1$\hss}%
	    \ifnum \ht1=0
		\smash
		  {\hbox to 0pt
		    {\hss \vrule height 0.67 \ticklength width \linewidth \hss}%
		  }%
	    \else
		\smash
		  {\hbox to 0pt
		    {\hss \vrule height \ticklength width \linewidth \hss}%
		  }%
		\nointerlineskip
		\kern \labelspacing
		\box1%
	    \fi
	    }%
	\hskip \padding
    \fi
    \next
    }%
%
\def\Xaxis	#1#2%
    {%
    \global\setbox \XAxis = \vbox
	{%
	\def \temp {#1}%
	\ifx \temp\empty \else
	    \hbox to \plotwidth {\hss #1\hss}%
	    \kern \titlespacing
	\fi
	\nointerlineskip
	\def \temp {#2}%
	\ifx \temp\empty \else \hbox to \plotwidth {\Xtic #2,\cr,}\fi
	\nointerlineskip
	\smash {\dash \plotwidth}%
	}%
    }%
\def\Xtic	#1,%
    {%
    \def \temp {#1}%
    \def \quit {\cr}%
    \ifx \temp\quit
	\let\next=\unskip
    \else
	\let\next=\Xtic
	\vbox
	    {%
	    \setbox1 = \hbox to 0pt {\hss \tenpoint $#1$\hss}%
	    \ifnum \ht1=0
		\smash
		  {\hbox to 0pt
		    {\hss \vrule depth 0.67 \ticklength width \linewidth \hss}%
		  }%
	    \else
		\box1%
		\kern \labelspacing
		\nointerlineskip
		\smash
		  {\hbox to 0pt
		    {\hss \vrule depth \ticklength width \linewidth \hss}%
		  }%
	    \fi
	    }%
	\hskip \padding
    \fi
    \next
    }%
%
\def\plotbox	(#1,#2)%
    {%
    \global\setbox \PlotData = \hbox to 0pt
	{%
	\unhbox \PlotData
	\rlap {\kern #1\plotwidth \raise #2\plotheight \copy\PlotBox}%
	}%
    }%
\def\plottext	(#1,#2)#3%
    {%
    \setbox \PlotBox = \hbox {#3}%
    \plotbox (#1,#2)%
    }%
\def\plotpoint	(#1,#2)%
    {%
    \setbox \PlotBox = \centered.%
    \plotbox (#1,#2)%
    }%
\def\plotsymbol	(#1,#2)#3%
    {%
    \setbox \PlotBox = \centered {#3}%
    \plotbox (#1,#2)%
    }%
\def\xrange	#1]{\def\XRange {#1]}}%
\def\yrange	#1]{\def\YRange {#1]}}%
%
\def\makeplot	#1#2#3%
    {%
    \plotwidth = #1 \plotheight = #2
    \setbox \xAxis = \hbox to \plotwidth  {\hfil}%
    \setbox \XAxis = \hbox to \plotwidth  {\hfil}%
    \setbox \yAxis = \vbox to \plotheight {\vfil}%
    \setbox \YAxis = \vbox to \plotheight {\vfil}%
    \setbox \PlotData = \hbox {}%
    {#3}%
    \hbox
	{%
	\box \yAxis
	\box \PlotData
	\vbox
	    {%
	    \box \XAxis
	    \nointerlineskip \kern \plotheight \nointerlineskip
	    \box \xAxis
	    }%
	\box \YAxis
	}%
    }%
%
\def\psfile	#1%		--- deferred input of postscript file ---
    {%
    \global\setbox \PlotData = \hbox to 0pt
	{%
	\unhbox \PlotData \special {psfile=#1}%
	}%
    }%
%
\def\addPS	#1%		--- immediate input of postscript commands ---
    {%
    \global\setbox \PlotData = \hbox to 0pt
	{%
	\unhbox \PlotData \special {"#1}%
	}%
    }%
%
\def\plotdatafile	#1#2%		--- deferred input of data file ---
    {%
    \global\setbox \PlotData = \hbox to 0pt
	{%
	\unhbox \PlotData
	\special
	    {%
	    datafile="#1"
	    dash="[#2]"
	    xrange="\XRange"
	    yrange="\YRange"
	    hsize=\the\plotwidth\space
	    vsize=\the\plotheight\space
	    linewidth=\the\linewidth
	    }%
	}%
    }%
%
\def\plotdata	#1#2%			--- immediate input of data points ---
    {%
    \global\setbox \PlotData = \hbox to 0pt
	{%
	\unhbox \PlotData
	\special
	    {%
	    datalist="#1"
	    dash="[#2]"
	    xrange="\XRange"
	    yrange="\YRange"
	    hsize=\the\plotwidth\space
	    vsize=\the\plotheight\space
	    linewidth=\the\linewidth
	    }%
	}%
    }%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%	Trivial Plotting Example
%
%\makeplot {5 in}{4.5 in}%
%    {%
%    \linewidth = 0.3 pt
%    \Xaxis {Title}{}
%    \Yaxis {\kern -5pt $Y$}{,2,4,6,8,10}
%    \yaxis {$y$}{0.0,,0.2,,0.4,,0.6,,0.8,,1.0}
%    \xaxis {$x$}{10^0 \ratio 1:2,\ratio 2:3,\ratio 3:4,\ratio 4:5,\ratio 5:6,
%		      \ratio 6:7,\ratio 7:8,\ratio 8:9,\ratio 9:1,
%		 10^1 \ratio 1:2,\ratio 2:3,\ratio 3:4,\ratio 4:5,\ratio 5:6,
%		      \ratio 6:7,\ratio 7:8,\ratio 8:9,\ratio 9:1,
%		 10^2 \ratio 1:1}
%    \plotsymbol (.9,.9)a
%    \plottext  (.2,.4){some text}
%    \plottext (.25,.7){more text}
%    }%
%\bye
