Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to animate PSTricks diagram, so looping is intensively used. I have 3 global variables \START, \STOP, \DELTA that should be declared in the preamble. The remaining variables in the body will make use of these 3 global variables.

The looping macro \multido needs an integer that represents how many times it must repeat the given job. Unfortunately, this value is not given explicitly but It can be obtained from a simple formula \STOP-\START divided by \DELTA. I need a simple way to find this number in integer rather than in floating point number.

Finding \INITIAL variable by taking advantage of \dimexpr and \strip@pt is also not so natural I think.

The following coding flow looks too complicated, I need more fluid approach.

\documentclass{minimal}
\parindent=0pt
\usepackage{multido}

\newcommand\START{-1}
\newcommand\STOP{1}
\newcommand\DELTA{0.01}


% to do division only, but the result in floating point
\pstFPdiv\TempTimes
{
        \the
        \dimexpr
        \STOP pt
        -\START pt
        \relax
}
{\DELTA}

\makeatletter
\newcommand\INITIAL
{
        \strip@pt
        \dimexpr
        \START pt
        +\DELTA pt
        \relax
}

% to make it an integer
\newcommand\TIMES
{
        \strip@pt
        \dimexpr
        \TempTimes pt
        \relax
}
\makeatother

\begin{document}
\multido
{
        \nx=\START+\DELTA,
        \ny=\INITIAL+\DELTA
}
{\TIMES}
{
    (\nx,\ny)\endgraf
}
\end{document}
share|improve this question
3  
Perhaps add a little more context, or even comment your code. What is it that you're after? Merely reproducing your output with different/simpler code is possible. –  Werner Jul 24 '11 at 15:42
    
Your division is a subtraction. Putting everything on a line of its own makes it hard to read. –  Martin Scharrer Jul 24 '11 at 15:55
1  
@Werner: She only wants a very simple for-loop. But the code is awful. –  Leo Liu Jul 24 '11 at 15:59
    
@Leo: Thanks, I now see the edit... –  Werner Jul 24 '11 at 16:00

4 Answers 4

up vote 3 down vote accepted

A solution without loading fp. If you do not need exactly two digits then you do not need \StripDecimals

\documentclass{minimal}
\parindent=0pt
\usepackage{pst-func,multido}
\def\StripDecimals#1{\expandafter\SD#10!}
\def\SD#1.#2#3#4!{%
  \ifx\relax#2\relax#1.\else#1.#2#3\fi}

\newcommand\START{-1.00}
\newcommand\STOP{1.00}
\newcommand\DELTA{0.01}

\pstFPsub\Temp\STOP\START 
\pstFPDiv\TIMES\Temp\DELTA
\pstFPadd\Temp\START\DELTA
\pstFPstripZeros\Temp\INITIAL

\begin{document}

\multido{\rx=\START+\DELTA,
         \ry=\INITIAL+\DELTA}{\TIMES}
  {(\StripDecimals{\rx0},\StripDecimals{\ry0})\endgraf}
\end{document}
share|improve this answer

I'm not particularly familiar with PStricks' floating point operations (via pst-func), so my preference is to use the fp package.

\documentclass{minimal}
\parindent=0pt
\usepackage{multido}
\usepackage[nomessages]{fp}%

\newcommand\START{-1}
\newcommand\STOP{1}
\newcommand\DELTA{0.01}
\FPupn\INITIAL{\START{} \DELTA{} add 2 trunc}% floating point truncation
\FPupn\TIMES{\STOP{} \START{} sub \DELTA{} div 0 trunc}% integer truncation

\begin{document}
\multido
{
  \nx=\START+\DELTA,
  \ny=\INITIAL+\DELTA
}
{\TIMES}
{
  (\nx,\ny)\endgraf
}
\end{document}

In performing calculations that require rounding you can use n round where n denotes the number of digits after the decimal.

share|improve this answer
    
I think infix syntax (by \FPeval) is preferred as a 'simpler way'. –  Leo Liu Jul 24 '11 at 16:02
    
@Leo: True. I am familiar with using RPN since PStricks requires this in its postscript node computations: \pnode(!ps){...}, and perhaps the OP is as well. But I agree - it is easier to read. –  Werner Jul 24 '11 at 16:06
    
@Werner: I can parse RPN. :-) –  xport Jul 24 '11 at 16:10
1  
@Werner: \pnode(* arithmetic){...} is also possible –  Herbert Jul 24 '11 at 20:25
  1. Don't add units after pure numeric values.
  2. fp package provides full ability to do complex arithmetic calculations.

The code is in fact not PSTricks related:

\documentclass{minimal}
\usepackage{fp}
\usepackage{multido}

\newcommand\START{-1}
\newcommand\STOP{1}
\newcommand\DELTA{0.01}
\FPeval\INITIAL{round((START + DELTA):2)}
\FPeval\TIMES{round((STOP-START)/DELTA:0)}
\parindent=0pt
\begin{document}
\multido{\nx=\START+\DELTA, \ny=\INITIAL+\DELTA}{\TIMES}
{
  (\nx,\ny)\endgraf
}
\end{document}
share|improve this answer

I apologize to all but I would like to resurrect this old thread. I was a bit surprised to see answers to original question focusing on LaTeX's fp package. There are good reasons TeX can not do floating point arithmetics but PostScript is definitely capable of doing it.

http://www.tug.org/TUGboat/tb23-3-4/tb75beccreal.pdf

My understanding of pstricks is that is a user friendly way to get raw PostScript code into TeX file (Note, I am just a very low level pstricks user). Here is one not so user friendly way to do the same.

The following line inputs the picture into TeX code with horizontal offset of 72 and a vertical offset of -150. The reason for verbose horizontal and vertical output is that command special puts the picture right after the text in the box of default size 0.

\special{psfile=my_PostScript_file.ps hoffset=72 voffset=-150 hscale=90 
vscale=90}
\bye

The my_PostScript_file.ps is of course programmed by hand. For example as

%!PS-Adobe-2.0 EPSF-2.0
%%Title: tata.fig
%%Creator: Predrag
%%CreationDate: Sun Dec 25 23:29:18 2011
%%For: predrag@oko.bagdala2.net (Predrag Punosevac)
%%BoundingBox: 0 0 110 101
%Magnification: 1.0000
%%EndComments
%%BeginProlog
%%EndProlog
/Times_Roman findfont
15 scalefont

setfont
50 50 moveto
(Some text)show
newpath
5 5 moveto
10 45 lineto
stroke
showpage
%EOF

It is now clear that it would be "easy" to do any animation involving floating point arithmetics by programming in raw PostScript.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.