%\iffalse -*- doctex -*- \fi
%
%% \RCS$Id: pmn-pstc.sty,v 1.7 2003/11/18 00:33:53 turtle Exp $
%% \RCS$Revision: 1.7 $
%% \RCS$Date: 2003/11/18 00:33:53 $
%
%\iffalse metacomment
% -*- doctex -*-
%%  $Id: pmn-pstc.sty,v 1.7 2003/11/18 00:33:53 turtle Exp $
%%  (C) 2003 Peter Møller Neergaard
%% 
%%  This program may be distributed and/or modified under the
%%  conditions of the LaTeX Project Public License, either version 1.2
%%  of this license or (at your option) any later version.
%%  The latest version of this license is in
%%    http://www.latex-project.org/lppl.txt
%%  and version 1.2 or later is part of all distributions of LaTeX 
%%  version 1999/12/01 or later.
%
% This program consists of the file pmn-pstc.sty.
% 
% To print the documentation (which for the moment is uninteresting)
% remove you will have to uncomment all the lines starting with %D%.
% 
%\fi
%
% \CheckSum{214}
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \MakeShortVerb{\"}
%
% \title{The \package{pmn-pstc} package\thanks
%   {This file has CVS revision \RCSRevision, dated \RCSDate.}}
% \author{Peter Møller Neergaard}
% \maketitle
%
% \begin{abstract}
%   This package extends \package{pstricks} with the (optionally)
%   showing of a frame indicating the size of each "pspicture"
%   environment.  This can aid while making the drafts.  By default it
%   defines a separate environment "FRpspicture".  Furthermore it
%   makes the "pspicture" environment work with \package{pdftricks}.
% \end{abstract}
%
% There are three options
% \begin{description}
% \item[\texttt{replace}:] the "pspicture" environment is replaced by the "FRpspicture"
%   environment
% \item[\texttt{boxes}:] put boxes around the "FRpspicture" environment
% \item[\texttt{pmn-drft}:] follow the draft setting of the package
% \item[\texttt{usepdftricks}:] make the "pspicture"
%   environments work with \package{pdftricks} and PDF\TeX.
% \end{description}
%
% Nested usage of "pspicture" does not work with option "usepdftricks";
% instead the outermost "pspicture"-environment should be replaced by
% "pdfpicture".  
%
% \DescribeMacro{\rnodeput}
% \DescribeMacro{\Rnodeput}
% \mcmd{\rnodeput}\parg{x,y}\oarg{refpoint}\marg{name}\marg{stuff} and
% \mcmd{\Rnodeput}\parg{x,y}\marg{name}\marg{stuff} are versions of
% \package{pstrikcs}' \cmd{rnode} and \cmd{\Rnode} which typesets the
% node at coordinate \parg{x,y}.
%
% \DescribeMacro{\ahead}
% \DescribeMacro{\bhead}
% \DescribeMacro{\aheadlabel}
% \DescribeMacro{\bheadlabel}
% \DescribeMacro{\arrowhead}
%   \mcmd{\alabel}\oarg{pos}\marg{label} and its friends
%   \cmd{\blabel}, \cmd{\aheadlabel}, and \cmd{\bheadlabel} typeset a
%   math mode label over or under a recently drawn connector.  The
%   commands \cmd{\aheadlabel} and \cmd{\bheadlabel} also add an
%   arrowhead.  An arrowhead can  be added individually with the
%   command \mcmd{\arrowhead}\marg{pos}.
%
% \StopEventually
%
% We announce ourselves to the world.
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{pmn-pstc}[2003/11/02 PMN additions to pstricks]
%    \end{macrocode}
%  First we handle the options
%    \begin{macrocode}
\makeatletter
\newif\if@pmnpst@boxes
\DeclareOption{boxes}{\@pmnpst@boxestrue}
%    \end{macrocode}
% The option "replace" redefines pspicture, at the end of the preamble.
%    \begin{macrocode}
\newif\if@pmnpst@replace
\DeclareOption{replace}{\@pmnpst@replacetrue}
%    \end{macrocode}
% The option "usepdftricks" enables compatibility with
% \package{pdftricks} through a "pdfpspictures" environment which
% looks like a "pspicture" environment, but embeds it inside a
% "pdfpic" environment when \package{pdftricks} is loaded.
%    \begin{macrocode}
\newif\if@pmnpst@pdftcks
\DeclareOption{usepdftricks}{\@pmnpst@pdftckstrue}
% pmn-drft checks the setting at the end of the preamble.
%    \begin{macrocode}
\DeclareOption{pmn-drft}{%
  \AtBeginDocument{%
    \@ifundefined{ifDraft}{}{\ifDraft\@pmnpst@boxestrue\fi}}}
\ProcessOptions
%    \end{macrocode}
% Then we define the "FRpspicture" (framed "pspicture") environment.
% We exploit that the dimensions are stored in "\pst@dimena" to
% "\pst@dimend".
%    \begin{macrocode}
\newenvironment{FRpspicture}
  {\begin{pmnpst@pspicture}}
  {\if@pmnpst@boxes
    \edef\reserved@a{%
      \noexpand\psframe(\the\pst@dima,\the\pst@dimb)%
      (\the\pst@dimc,\the\pst@dimd)}%
    \reserved@a
    \fi
  \end{pmnpst@pspicture}}
%    \end{macrocode}
% If this file has not been processed before, we copy pstricks
% environment at the end of the preamble.
%    \begin{macrocode}
\@ifundefined{pmnpst@picture}{%
  \AtBeginDocument{%
    \let\pmnpst@pspicture\pspicture
    \let\endpmnpst@pspicture\endpspicture
    \if@pmnpst@replace
      \let\pspicture\FRpspicture
      \let\endpspicture\endFRpspicture
    \fi}}{}
%    \end{macrocode}
%
% \begin{macro}{\if@pmnpst@pdftcks@ld@lst}
% \begin{macro}{\@pmnpst@pdftcks@ld@lsttrue}
% \begin{macro}{\@pmnpst@pdftcks@ld@lstfalse}
%   We test whether compatibility with \package{pdftricks} was
%   requested.  We first define a flag to do a sanity check ensuring
%   that \package{pdftricks} is loaded first.
%    \begin{macrocode}
\if@pmnpst@pdftcks\expandafter\@iden\else\expandafter\@gobble\fi
{\newif\if@pmnpst@pdftcks@ld@lst
\@pmnpst@pdftcks@ld@lsttrue
\@ifpackageloaded{pdftricks}{\@pmnpst@pdftcks@ld@lstfalse}{}
%    \end{macrocode}
% \end{macro} ^^A \@pmnpst@pdftcks@ld@lstfalse
% \end{macro} ^^A \@pmnpst@pdftcks@ld@lsttrue
% \end{macro} ^^A \if@pmnpst@pdftcks@ld@lst
%  
% \begin{macro}{\pdfpspicture}
%   We define an alternative "pspicture"-environment which calls
%   "pdfpic".  This alternative environment replaces "pspicture" at
%   the end of the preamble. For deep technical reasons with the
%   implementation of \package{moreverb} and \package{pdftricks} this
%   does not work with nested "pspicture" environments.\footnote{The
%   problem is as follows: \package{pdftricks} copy the contents of
%   the \texttt{pspicture} environment verbatim to a file.  The
%   verbatim copying provided through \package{moreverb} stops when
%   the first \cmd{\end}\texttt{\{pspicture\}} occurs.}
%    \begin{macrocode}
\edef\pdfpspicture{\noexpand\pdfpic%
  \expandafter\string\csname begin\endcsname{pspicture}}
%    \end{macrocode}
% The "pdfpic"-environment writes everything between "\begin{pdfpic}"
% and "\end{pdfpic}" verbatim to an auxiliary file.  We therefore
% define the "pdfpspicture" environment such that it starts the
% "pdfpic"-environment and then is followed by the literal string
% "\begin{pspicture}".  The use of "\string\csname" handles the latter
% so we avoid taking "\begin{pspicture}" as a macro.
% \end{macro} ^^A \pdfpspicture
% \begin{macro}{\pdfendpspicture}\mbox{}
%    \begin{macrocode}
\def\endpdfpspicture{%
  \immediate \write \verbatim@out{\string\end{pspicture}}%
  \endpdfpic}
%    \end{macrocode}
% Some more hacking: "pdftricks" use \package{moreverb} to write
% things literally.  \package{moreverb} recognizes when the current
% environment is ended (that is why we use "\pdfpic" above: to avoid
% changing the current environment away from the caller).  When that
% happens we write a closing "\end{pspicture}" (matching the opening
% above) and closes the environment with "pdfpic"
% \end{macro} ^^A \pdfendpspicture
%
% Finally we set up the switching of "pspicture" to "pdfpicture" at
% the beginning of the document.  We make "pdfpspicture" stand for
% "pspicture" if \package{pdftricks} is not loaded.
%    \begin{macrocode}
\AtBeginDocument{%
  \@ifpackageloaded{pdftricks}{%
    \if@pmnpst@pdftcks@ld@lst       
      \PackageError{pmn-pstc}{pdftricks should be loaded
before pmn-pstc}{You do not have to use the package pdftricks 
for pmn-pstc to work,^^Jbut if you do, it should be loaded 
before pmn-pstc.}%
    \fi
    \let\pmnpst@pdfpicture\pspicture
    \let\endpmnpst@pdfpicture\endpspicture
    \let\pspicture\pdfpspicture
    \let\endpspicture\endpdfpspicture}%
  {\let\pdfpspicture\pspicture
   \let\endpdfpspicture\endpspicture}}
%    \end{macrocode}
% \changes{}{2003/11/08}{Added option to make changing to pdftex/pdftricks easy}
%
% \begin{macro}{\psinputs}
% \begin{macro}{\endpsinputs}
%    Finally, for compatibility we make sure that the "psinputs"
%    environment is defined in case \package{pdftrikcs} is not
%    loaded.  It just outputs the contents so the definitions and
%    packages are available throughout the document.
%    \begin{macrocode}
\@ifundefined{psinputs}{%
  \def\psinputs{\endgroup}%
  \def\endpsinputs{\begingroup\def\@currenvir{psinputs}}}{}}
%    \end{macrocode} 
%    A \LaTeX\ environment opens a group which means that any
%    definitions loaded inside the environment do not take effect.
%    This hack the definition of an environment: it closes the
%    environment immediately after having opened it and then open it
%    at the end.
% \end{macro} ^^A \endpsinputs
% \end{macro} ^^A \psinputs
%
% This just changes  the macros if requested provided that the user
% actually included \package{pdftricks}.
%
% Some short hand in case we don't want to show the boxes locally.
%    \begin{macrocode}
\let\PspictureShowBoxes\pmnpst@boxes@true
\let\PspictureDontShowBoxes\pmnpst@boxes@false
%    \end{macrocode}
%
% And the commands to typeset the documentation.  All enclosed in
% \begin{macro}{\Rnodeput}
% \begin{macro}{\pmn@Rnodeput}
%   \mcmd{\Rnodeput}\parg{x,y}\marg{name}\marg{stuff} makes an \cmd{\Rnode}
%    containing \meta{stuff} named~\meta{name} at \parg{x,y}.
%    \begin{macrocode}
\newcommand{\Rnodeput}{\@ifnextchar({\pmn@Rnodeput}{\Rnode}}
\newcommand{\pmn@Rnodeput}{}
\def\pmn@Rnodeput(#1,#2)#3#4{\rput(#1,#2){\Rnode{#3}{#4}}}
%    \end{macrocode}
%   It uses the auxiliary \cmd{\pmn@Rnodeput} to get the
%   parsing of arguments right.
% \changes{}{2003/11/17}{Added}
% \end{macro} ^^A \pmn@Rnodeput
% \changes{}{2003/11/17}{Added}
% \end{macro} ^^A \Rnodeput
%
% \begin{macro}{\rnodeput}
% \begin{macro}{\pmn@rnodeput@i}
% \begin{macro}{\pmn@rnodeput@ii}
% \begin{macro}{\pmn@rnodeput@iii}
%   \mcmd{\rnodeput}\parg{x,y}\oarg{refpoint}\marg{name}\marg{stuff} makes
%   an \cmd{\rnode} containing \meta{stuff} named~\meta{name} at
%   \parg{x,y}.  The reference point (a point on the surrounding
%   box is set) to \meta{refpoint} if provided.
%    \begin{macrocode}
\newcommand{\rnodeput}{\@ifnextchar({\pmn@rnodeput@i}{\rnode}}
\newcommand{\pmn@rnodeput@i}{}
\def\pmn@rnodeput@i(#1,#2){\@ifnextchar[{\pmn@rnodeput@ii(#1,#2)}{\pmn@rnodeput@iii(#1,#2)}}
\newcommand{\pmn@rnodeput@ii}{}
\def\pmn@rnodeput@ii(#1,#2)[#3]#4#5{\rput(#1,#2){\rnode[#3]{#4}{#5}}}
\newcommand{\pmn@rnodeput@iii}{}
\def\pmn@rnodeput@iii(#1,#2)#3#4{\rput(#1,#2){\rnode{#3}{#4}}}
%    \end{macrocode}
%   It uses the auxiliary \cmd{\pmn@rnodeput@i},
%   \cmd{\pmn@rnodeput@ii}, and \cmd{\pmn@rnodeput@iii} to get the
%   parsing of arguments right.
% \changes{}{2003/11/17}{Added}
% \end{macro} ^^A \pmn@rnodeput@iii
% \changes{}{2003/11/17}{Added}
% \end{macro} ^^A \pmn@rnodeput@ii
% \changes{}{2003/11/17}{Added}
% \end{macro} ^^A \pmn@rnodeput@i
% \changes{}{2003/11/17}{Added}
% \end{macro} ^^A \Rnodeput
%
% \begin{macro}{\alabel}
% \begin{macro}{\blabel}
% \begin{macro}{\aheadlabel}
% \begin{macro}{\bheadlabel}
%   \mcmd{\alabel}\oarg{pos}\marg{label} and its friends
%   \cmd{\blabel}, \cmd{\aheadlabel}, and \cmd{\bheadlabel} typeset a
%   math mode label over or under a recently drawn connector.  The commands
%   \cmd{\aheadlabel}, and \cmd{\bheadlabel} also add an arrowhead.
%    \begin{macrocode}
\newcommand{\alabel}[2][.5]{\aput(#1){$\scriptstyle#2$}}
\newcommand{\blabel}[2][.5]{\bput(#1){$\scriptstyle#2$}}
\newcommand{\aheadlabel}[2][.5]{\aput(#1){$\scriptstyle#2$}\arrowhead{#1}}
\newcommand{\bheadlabel}[2][.5]{\bput(#1){$\scriptstyle#2$}\arrowhead{#1}}
\AtBeginDocument{\psset{labelsep=1pt}}
%    \end{macrocode}
%   We adjust the labels to be closer to the connectors.
% \end{macro} ^^A \bheadlabel
% \end{macro} ^^A \aheadlabel
% \end{macro} ^^A \blabel
% \end{macro} ^^A \alabel
%
% \begin{macro}{\arrowhead}
%   \mcmd{\arrowhead}\marg{pos} adds an arrow head to the a
%   connector that has just been drawn.  The arrow is placed at
%   \meta{pos} on the connector.
%    \begin{macrocode}
\providecommand{\arrowhead}[1]{%
  \edef\@tempa{%
    1 0 \psk@arrowsize CLW mul add 
    \psk@arrowlength mul 2 div neg 0
    ArrowA pop pop}%
  \lput{:U}(#1){\pscustom{\arrows{>->}
% \changes{}{2003/11/17}{Included from \package{proofnet}}
      \expandafter\code\expandafter{\@tempa}}}}
%    \end{macrocode}
%  The \cmd{\lput} places the material rotated to follow the curve of
%  the arrow.  The material given to \cmd{\lput} then places an arrow
%  head with the middle at the given position.  The expression
%  "\psk@arrowsize "$\mathtt{\ldots}$" neg 0" used to calculate the position is
%  found by reverse engineering \package{pstricks}' arrow command.  It
%  calculates the length of the arrow head and then divides it by 2.
% \end{macro}^^A \arrowhead
%
% "<*documentation>"$\ldots$"</documentation>" so we exclude it if we
% ever make a \package{docstrip} file.  For now, we simply stop input with
% \cmd{\endinput} so the file can be used directly as a package file.
%    \begin{macrocode}
%<*documentation>
%    \end{macrocode}
%  First we need to disable the \cmd{\part} command 
%    \begin{macrocode}
 \endinput
\makeatletter
\let\part\@undefined
\let\section\@undefined
\let\psset\@gobble
\makeatother
\documentclass{pmn-ldoc} 
\usepackage[latin1]{inputenc}
\newcommand{\docdate}{2003/11/02}
 %\OnlyDescription
 %\DisableCrossrefs
\begin{document}
  \DocInput{pmn-pstc.sty}
\end{document}
%</documentation>
%    \end{macrocode}
% \Finale \sloppy \PrintChanges
