Graphics, Figures & TablesPSTricks syntax

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

PSTricks syntax

Post by svend_tveskaeg »

Hi all.

Consider the following code, with which Herbert Voß helped me over at {TeX} SX:

Code: Select all

\documentclass{article}

\usepackage{pst-solides3d}
\usepackage{expl3}

%% Parameters
% Windings
\def\lWind{20}
\def\rWind{10}
% Radii
\def\rHelix{1.11}
\def\rWire{0.015}

% Floating point calculations
\ExplSyntaxOn
  \cs_new_eq:NN
    \calc
  \fp_eval:n
\ExplSyntaxOff
% Constants
\def\factor{80} % \factor > \lWind,\rWind
\def\nLeft{\calc{2*\lWind/\factor} }
\def\nRight{\calc{2*\rWind/\factor} }

%% Colours
\colorlet{wireColor}{red!60}
\colorlet{coreColor}{cyan!50}
%% Wire
\newpsobject{wire}{psSolid}{%
  object=courbe,
   % Choose max(\nRight,\nLeft) -- the fewer windings, the larger
   % the constant multiplied by max(\nRight,\nLeft)
  ngrid=970 \nLeft mul cvi 15,
  r=\rWire,
  fillcolor=wireColor,
  incolor=wireColor
}

\pagestyle{empty}

\begin{document}

\begin{figure}[htbp]
 \centering
  \begin{pspicture}(-5,-5)(5,5)
   \psset{%
     algebraic,
     viewpoint=20 5 10 rtp2xyz,
     lightsrc=20 60 60 rtp2xyz,
     Decran=30,
     solidmemory,
     action=none,
     grid=false
   }
   %%--------- Core ----------
   \psSolid[object=anneau,h=1.0,R=4,r=2.5,ngrid=4,RotZ=90,RotY=45,RotX=90,
     fillcolor=coreColor,name=core]
   %%--------- Wire ----------
   % Left
   \defFunction{heliceA}(t){\rHelix*cos(\factor*t)}{\rHelix*sin(\factor*t)}%
     {t/\nLeft}
   \wire[function=heliceA,range=0 Pi \nLeft mul,name=wireA](0,-2.25,-1.5)
   % Right
   \defFunction{heliceB}(t){\rHelix*cos(\factor*t)}{-\rHelix*sin(\factor*t)}%
     {t/\nRight}
   \wire[function=heliceB,range=0 Pi \nRight mul,name=wireB](0,2.25,-1.5)
   %%------- Assembly --------
   \psSolid[object=fusion,base=core wireA wireB,action=draw**]
  \end{pspicture}
\end{figure}

\end{document}
I have a rather embarrassing problem; how do I write the calculations for \nLeft and \nRight if I want PSTricks to do them without activating the L3 fp syntax? I have tried

Code: Select all

\def\nLeft{2 \lWind mul \factWind div }
\def\nRight{2 \rWind mul \factWind div }
but that leads to an error.

Thank you in advance!
Last edited by svend_tveskaeg on Sat Jan 05, 2013 7:36 pm, edited 4 times in total.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)

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

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

PSTricks syntax

Post by Stefan Kottwitz »

Those values are constants, you could calculate them in another way, such as with the fp package. Also pgf provides floating point calculation, you could even use just the calculation but do the graphic with PSTricks. But I remember you don't use pgf/TikZ.

Stefan
LaTeX.org admin
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: PSTricks syntax

Post by svend_tveskaeg »

I know of the fp package but can it not be done without loading this? I have a feeling that it should be easy and that I am just "blind" at the moment.

I cannot even see why my try shouldn't work. :(
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
CrazyHorse
Posts: 351
Joined: Sat Aug 02, 2008 8:47 am

PSTricks syntax

Post by CrazyHorse »

svend_tveskaeg wrote:

Code: Select all

\def\nLeft{2 \lWind mul \factWind div }
\def\nRight{2 \rWind mul \factWind div }
but that leads to an error.
define

Code: Select all

\pstVerb{
  /nLeft 2 \lWind mul \factWind div def
  /nRight 2 \rWind mul \factWind div def
}
then you can use nLeft and nRight as PostScript variables in your other macros.
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

PSTricks syntax

Post by svend_tveskaeg »

@Herbert:
I am not exactly sure how to do this. If I try

Code: Select all

\documentclass{article}

\usepackage{pst-solides3d}

%% Parameters
% Windings
\def\lWind{20}
\def\rWind{10}
% Radii
\def\rHelix{1.11}
\def\rWire{0.015}

% Constants
\def\factor{80} % \factor > \lWind,\rWind
\pstVerb{
  /nLeft 2 \lWind mul \factor div def
  /nRight 2 \rWind mul \factor div def
}

%% Colours
\colorlet{wireColor}{red!60}
\colorlet{coreColor}{cyan!50}
%% Wire
\newpsobject{wire}{psSolid}{%
  object=courbe,
   % Choose max(\nRight,\nLeft) -- the fewer windings, the larger
   % the constant multiplied by max(\nRight,\nLeft)
  ngrid=970 \nLeft mul cvi 15,
  r=\rWire,
  fillcolor=wireColor,
  incolor=wireColor
}

\pagestyle{empty}

\begin{document}

\begin{figure}[htbp]
 \centering
  \begin{pspicture}(-5,-5)(5,5)
   \psset{%
     algebraic,
     viewpoint=20 5 10 rtp2xyz,
     lightsrc=20 60 60 rtp2xyz,
     Decran=30,
     solidmemory,
     action=none,
     grid=false
   }
   %%--------- Core ----------
   \psSolid[object=anneau,h=1.0,R=4,r=2.5,ngrid=4,RotZ=90,RotY=45,RotX=90,
     fillcolor=coreColor,name=core]
   %%--------- Wire ----------
   % Left
   \defFunction{heliceA}(t){\rHelix*cos(\factor*t)}{\rHelix*sin(\factor*t)}%
     {t/\nLeft}
   \wire[function=heliceA,range=0 Pi \nLeft mul,name=wireA](0,-2.25,-1.5)
   % Right
   \defFunction{heliceB}(t){\rHelix*cos(\factor*t)}{-\rHelix*sin(\factor*t)}%
     {t/\nRight}
   \wire[function=heliceB,range=0 Pi \nRight mul,name=wireB](0,2.25,-1.5)
   %%------- Assembly --------
   \psSolid[object=fusion,base=core wireA wireB,action=draw**]
  \end{pspicture}
\end{figure}

\end{document}
, I get this following error:

Code: Select all

! Undefined control sequence.
\@tempb ->t/\nLeft 
                   
l.56      {t/\nLeft}
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

PSTricks syntax

Post by cgnieder »

I believe you should use it as

Code: Select all

t/nLeft
i.e. without the backslash.

Regards
site moderator & package author
CrazyHorse
Posts: 351
Joined: Sat Aug 02, 2008 8:47 am

PSTricks syntax

Post by CrazyHorse »

svend_tveskaeg wrote:

Code: Select all

\pstVerb{
  /nLeft 2 \lWind mul \factor div def
  /nRight 2 \rWind mul \factor div def
}

\newpsobject{wire}{psSolid}{%
  object=courbe,
   % Choose max(\nRight,\nLeft) -- the fewer windings, the larger
   % the constant multiplied by max(\nRight,\nLeft)
  ngrid=970 \nLeft mul cvi 15,
, I get this following error:

Code: Select all

! Undefined control sequence.
\@tempb ->t/\nLeft 
                   
l.56      {t/\nLeft}
Only in \pstVerb with a backslash.

ngrid=970 nLeft mul cvi 15,
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

PSTricks syntax

Post by svend_tveskaeg »

Thank you, Herbert and Clemens!

Can you tell me when to use \def and when to use \pstVerb?

Update
And thank you, Stefan. :)
Last edited by svend_tveskaeg on Sun Jan 06, 2013 8:52 am, edited 1 time in total.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
CrazyHorse
Posts: 351
Joined: Sat Aug 02, 2008 8:47 am

PSTricks syntax

Post by CrazyHorse »

svend_tveskaeg wrote: Can you tell me when to use \def and when to use \pstVerb?
\def defines a TeX macro, used as \...
\pstVerb defines PostScript functions, used _only_ in PS expressions

Code: Select all

\pstVerb{ /ePow { 2.728 exch exp } def }
now you can use something like

Code: Select all

ngrid = 1 3 ePow,
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: PSTricks syntax

Post by svend_tveskaeg »

I see. Thank you very much.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
Post Reply