I need some help!

Denis Girou Denis.Girou at idris.fr
Sun Sep 19 23:36:05 CEST 1999


-----------------------------------------------------------------------------
This is the PSTricks mailing list, devoted to discussions about computational
graphics in (La)TeX using the PSTricks package from Timothy van Zandt.
For help using this mailing list, see instructions at the end of message.
-----------------------------------------------------------------------------

  Sorry to be really late...


>>>>> "Isaiah.Shavitt" == Isaiah Shavitt <shavitt at chemistry.ohio-state.edu> writes:

    Isaiah.Shavitt> I have some specific questions which may perhaps be answered easily.
    Isaiah.Shavitt> Below I copy a sample macro from my macro collection, on which I can 
    Isaiah.Shavitt> illustrate some of my problems.  This macro draws a line segment 
    Isaiah.Shavitt> from point (#2,#3) to (#4,#5) and puts an arrow pointing to (#4,#5) 
    Isaiah.Shavitt> with the CENTER of the arrow (not its tip) #1 of the distance from 
    Isaiah.Shavitt> (#2,#3) to (#4,#5) (default #1=0.5), preceded by some generic 
    Isaiah.Shavitt> settings and followed by use examples:

    Isaiah.Shavitt> ...

    Isaiah.Shavitt> The macro works well, but note the following points:

    Isaiah.Shavitt> 1. It computes the coordinates of the arrow center twice, first in LaTeX 
    Isaiah.Shavitt>    with the \pssetlength, \psaddtolength commands, giving (\xc,\yc) which 
    Isaiah.Shavitt>    define the starting point in the \psline command, and second in the ps 
    Isaiah.Shavitt>    code to help in the calculation of the arrow tip position.  I have not 
    Isaiah.Shavitt>    found a way of using the numerical values of \Xl, \Yl, \Xc, \Yc in the  
    Isaiah.Shavitt>    ps code.

  You must use the internal macro \pst at getlength and rescale the values
(nevertheless, this is not the "common" way to proceed, and probably not
a convenient one).

    Isaiah.Shavitt> 2. I used the LaTeX definition \newcommand{\dline}[5][0.5]{...} instead 
    Isaiah.Shavitt>    of the TeX form \def\dline[#1](#1,#2)(#3,#4){...} because I don't know 
    Isaiah.Shavitt>    how to have an optional argument in the TeX \def.  But as a result, 
    Isaiah.Shavitt>    I cannot use the PST convention for specifying coordinate pairs (x,y) 
    Isaiah.Shavitt>    and have to use {x}{y} instead.

  I give you five versions which do the same thing:
    - first A is your original one,
    - second B use the TeX lengths inside the PostScript code, avoiding
recomputations, as you were looking for,
    - third C, at the opposite, don't use TeX lengths and made all
computations in PostScript,
    - fourth D implement the optional parameter in plain TeX, and also a syntax
for coordinates as (.,.), using the standard TeX technics,
    - fifth E implement the macro as a PSTricks one, allowing to change locally 
some parameters and to define some new ones. It also admit (0,0) as default
first coordinate.

    Isaiah.Shavitt> Any comments about the above code will be appreciated.  I have other 
    Isaiah.Shavitt> questions, but will leave them to another posting.

  So, I will see the others (but I do not promise a quick answer, having
several other waiting tasks...).

D.G.

\documentclass[12pt]{book}

\usepackage{pstricks}

\textwidth 6.5in
\textheight 9.0in
\topmargin -0.35in
\oddsidemargin 0in
\evensidemargin 0in
\lineskip=3pt minus 1pt
\lineskiplimit=2pt
\parskip 0.2in
\parindent 0.25in

\psset{arrowsize=2pt 4,dotsep=2pt}

\newlength{\xl}\newlength{\yl}\newlength{\xc}\newlength{\yc}

\SpecialCoor

\begin{document}

% Original version
% ----------------

\def\ArrowShift{0.15 } % Distance from the center of the arrow to its tip

\newcommand{\dlineA}[5][0.5]{%
\qline(#2,#3)(#4,#5)
\pssetlength{\xl}{#4}\psaddtolength{\xl}{-#2}   % x-component of vector
\pssetlength{\yl}{#5}\psaddtolength{\yl}{-#3}   % y-component of vector
\pssetlength{\xc}{#2}\psaddtolength{\xc}{#1\xl} % x-coor of arrow center
\pssetlength{\yc}{#3}\psaddtolength{\yc}{#1\yl} % x-coor of arrow center
\psline{->}(\xc,\yc)
 (!/Mydict 10 dict def Mydict begin
   /Xl #4 #2 sub def /Yl #5 #3 sub def
   /Xc #1 #4 mul 1 #1 sub #2 mul add def
   /Yc #1 #5 mul 1 #1 sub #3 mul add def
   Yl Xl atan dup cos \ArrowShift mul Xc add
   exch sin \ArrowShift mul Yc add end)
}

\begin{center}
\begin{pspicture}(-0.5,0)(1.5,2)
  \dlineA{0}{0}{0}{2}
  \dlineA{1}{2}{1}{0}
\end{pspicture}
\qquad
\begin{pspicture}(-0.5,0)(2.3,2)
  \dlineA{0}{0}{0.8}{2}
  \dlineA{1.8}{2}{1}{0}
\end{pspicture}
\qquad
\begin{pspicture}(-0.5,0)(2.3,2)
  \dlineA{0.8}{0}{0}{2}
  \dlineA{1}{2}{1.8}{0}
\end{pspicture}
\qquad
\begin{pspicture}(-0.5,0)(2.5,2)
  \dlineA[0.7]{0}{1.5}{2}{1.5}
  \dlineA[0.3]{2}{0.5}{0}{0.5}
\end{pspicture}
\end{center}

% Second version
% --------------

\def\ArrowShift{0.15} % Distance from the center of the arrow to its tip

\makeatletter

\newcommand{\dlineB}[5][0.5]{%
\qline(#2,#3)(#4,#5)
\pssetlength{\xl}{#4}\psaddtolength{\xl}{-#2}   % x-component of vector
\pssetlength{\yl}{#5}\psaddtolength{\yl}{-#3}   % y-component of vector
\pssetlength{\xc}{#2}\psaddtolength{\xc}{#1\xl} % x-coor of arrow center
\pssetlength{\yc}{#3}\psaddtolength{\yc}{#1\yl} % x-coor of arrow center
\pst at getlength{\xl}{\XL}
\pst at getlength{\yl}{\YL}
\pst at getlength{\xc}{\XC}
\pst at getlength{\yc}{\YC}
\psline{->}%
       (\xc,\yc)%
       (! \YL\space 28.45274 div \XL\space 28.45274 div atan dup cos
            \ArrowShift\space mul \XC\space 28.45274 div add exch sin
            \ArrowShift\space mul \YC\space 28.45274 div add)}
\makeatother

\begin{center}
\begin{pspicture}(-0.5,0)(1.5,2)
  \dlineB{0}{0}{0}{2}
  \dlineB{1}{2}{1}{0}
\end{pspicture}
\qquad
\begin{pspicture}(-0.5,0)(2.3,2)
  \dlineB{0}{0}{0.8}{2}
  \dlineB{1.8}{2}{1}{0}
\end{pspicture}
\qquad
\begin{pspicture}(-0.5,0)(2.3,2)
  \dlineB{0.8}{0}{0}{2}
  \dlineB{1}{2}{1.8}{0}
\end{pspicture}
\qquad
\begin{pspicture}(-0.5,0)(2.5,2)
  \dlineB[0.7]{0}{1.5}{2}{1.5}
  \dlineB[0.3]{2}{0.5}{0}{0.5}
\end{pspicture}
\end{center}

% Third version
% -------------

\newcommand{\dlineC}[5][0.5]{%
\qline(#2,#3)(#4,#5)
\psline{->}%
       (! Xc Yc)%
       (! /Xc #1 #4 mul 1 #1 sub #2 mul add def
          /Yc #1 #5 mul 1 #1 sub #3 mul add def
          #5 #3 sub #4 #2 sub atan
             dup  cos \ArrowShift\space mul Xc add
             exch sin \ArrowShift\space mul Yc add)}

\begin{center}
\begin{pspicture}(-0.5,0)(1.5,2)
  \dlineC{0}{0}{0}{2}
  \dlineC{1}{2}{1}{0}
\end{pspicture}
\qquad
\begin{pspicture}(-0.5,0)(2.3,2)
  \dlineC{0}{0}{0.8}{2}
  \dlineC{1.8}{2}{1}{0}
\end{pspicture}
\qquad
\begin{pspicture}(-0.5,0)(2.3,2)
  \dlineC{0.8}{0}{0}{2}
  \dlineC{1}{2}{1.8}{0}
\end{pspicture}
\qquad
\begin{pspicture}(-0.5,0)(2.5,2)
  \dlineC[0.7]{0}{1.5}{2}{1.5}
  \dlineC[0.3]{2}{0.5}{0}{0.5}
\end{pspicture}
\end{center}

% Fourth version
% --------------

\makeatletter

\def\dlineD{\@ifnextchar[{\dlineD at i}{\dlineD at i[0.5]}}

\def\dlineD at i[#1](#2,#3)(#4,#5){%
\qline(#2,#3)(#4,#5)
\psline{->}%
       (! Xc Yc)%
       (! /Xc #1 #4 mul 1 #1 sub #2 mul add def
          /Yc #1 #5 mul 1 #1 sub #3 mul add def
          #5 #3 sub #4 #2 sub atan
             dup  cos \ArrowShift\space mul Xc add
             exch sin \ArrowShift\space mul Yc add)}

\makeatother

\begin{center}
\begin{pspicture}(-0.5,0)(1.5,2)
  \dlineD(0,0)(0,2)
  \dlineD(1,2)(1,0)
\end{pspicture}
\qquad
\begin{pspicture}(-0.5,0)(2.3,2)
  \dlineD(0,0)(0.8,2)
  \dlineD(1.8,2)(1,0)
\end{pspicture}
\qquad
\begin{pspicture}(-0.5,0)(2.3,2)
  \dlineD(0.8,0)(0,2)
  \dlineD(1,2)(1.8,0)
\end{pspicture}
\qquad
\begin{pspicture}(-0.5,0)(2.5,2)
  \dlineD[0.7](0,1.5)(2,1.5)
  \dlineD[0.3](2,0.5)(0,0.5)
\end{pspicture}
\end{center}

% Fifth version
% -------------

\makeatletter

\def\psset at ArrowPos#1{\edef\psk at ArrowPos{#1}}
\psset at ArrowPos{0.5}

\def\dlineE{\pst at object{dlineE}}

\def\dlineE at i(#1){\@ifnextchar({\dlineE at ii(#1)}{\dlineE at ii(0,0)(#1)}}

\def\dlineE at ii(#1,#2)(#3,#4){%
\begin at OpenObj
\qline(#1,#2)(#3,#4)
\psline{->}%
       (! Xc Yc)%
       (! /Xc \psk at ArrowPos\space #3 mul 1 \psk at ArrowPos\space sub
              #1 mul add def
          /Yc \psk at ArrowPos\space #4 mul 1 \psk at ArrowPos\space sub
              #2 mul add def
          #4 #2 sub #3 #1 sub atan
             dup  cos \ArrowShift\space mul Xc add
             exch sin \ArrowShift\space mul Yc add)
\end at OpenObj}

\makeatother

\begin{center}
\begin{pspicture}(-0.5,0)(1.5,2)
  \dlineE[linewidth=2pt](0,2)
  \dlineE[linecolor=blue,arrowscale=3](1,2)(1,0)
\end{pspicture}
\qquad
\begin{pspicture}(-0.5,0)(2.3,2)
  \dlineE(0.8,2)
  \dlineE(1.8,2)(1,0)
\end{pspicture}
\qquad
\begin{pspicture}(-0.5,0)(2.3,2)
  \dlineE(0.8,0)(0,2)
  \dlineE(1,2)(1.8,0)
\end{pspicture}
\qquad
\begin{pspicture}(-0.5,0)(2.5,2)
  \dlineE[linecolor=red,ArrowPos=0.7](0,1.5)(2,1.5)
  \dlineE[linestyle=dashed,ArrowPos=0.3](2,0.5)(0,0.5)
\end{pspicture}
\end{center}

\end{document}

-----------------------------------------------------------------------------
The list interface (subscription, information, access to the archives) is on:
http://www.tug.org/cgi-bin/lwgate/pstricks
Otherway to unsubscribe, send mail to pstricks-request at mail.tug.org
with a blank subject and in body the line unsubscribe <email-address>
-----------------------------------------------------------------------------



More information about the PSTricks mailing list