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.

For a trivial example, suppose that \f(x) is a quadratic curve defined as follows

\def\f(#1){-#1*(#1-8)/4}

For each element \ix in {0,1,2,3,4,5,6,7,8} there is a point (\ix,\f(\ix)) that will be the center of a square of 1 cm square.

How to express the bottom-left & top-right points of \psframe in terms of \ix and \f(\ix)?

NOTE: Please don't suggest other simpler solutions such as using \rput to put the squares because I am interested in PSTricks point expression. RPN notation should be avoided if it is possible.

I give you the skeleton (not working) as below to save your time.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}%loads multido automatically

\begin{document}

\def\f(#1){-#1*(#1-8)/4}

\begin{pspicture}
\multido{\ix=0+1}{9}{
% the following expression is wrong!
\psframe[dimen=middle](*\ix-0.5 {\f(x)-0.5})(*\ix+0.5 {\f(x)+0.5})
}
\psplot[algebraic]{0}{8}{\f(x)}
\end{pspicture}

\end{document}

The output I want to get is as follows:

enter image description here

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted
\listfiles
\documentclass{article}
\usepackage{pst-plot}
\SpecialCoor
\begin{document}

\def\f(#1){-#1*(#1-8)/4}

\begin{pspicture}(-1,-2)(10,5)
\psforeach{\ix}{0,1,..,9}{%
  \rput(*\ix\space {\f(x)}){\psframe[dimen=middle](-0.5,-0.5)(0.5,0.5)}}
\psplot[algebraic]{0}{8}{\f(x)}
\end{pspicture}

\begin{pspicture}(-1,-2)(10,5)
\psforeach{\ix}{0,1,..,9}{%
  \psframe[dimen=middle](*{\ix\space 0.5 sub}  {\f(\ix)-0.5})
                        (*{\ix\space 0.5 add} {\f(\ix)+0.5})}
\psplot[algebraic]{0}{8}{ \f(x) }
\end{pspicture}

\end{document}
share|improve this answer
4  
You know how to make it look easy... –  Werner Dec 13 '11 at 16:46
1  
@Herbert and @ CounterTerrorist: Just replace \f(x) with \f(\ix). –  Werner Dec 13 '11 at 16:49
1  
For what it's worth, @CounterTerrorist requested a no-\rput solution, even though it may be simpler. Your previous solution worked fine in that regard. –  Werner Dec 13 '11 at 16:54
1  
that was in pstricks-add and then moved into the core, but the documentation hasn't yet updated (> 400 pages!). The syntax ist for the * version is simple: (*<x value> {<f(x)>}), where f(x) must be described in algebraic notation, e.g. {sin(x)+cos(2*x)/2} –  Herbert Dec 13 '11 at 17:17
1  
See pst-news08.pdf (section 2.12 Special coordinates, p 19). –  Werner Dec 13 '11 at 17:19
show 1 more comment

Use a postscript formulation of your algebraic function, and then reference the coordinates using the "raw PS" notation:

enter image description here

\documentclass{article}
\usepackage{pst-plot}% http://ctan.org/pkg/pst-plot
% The following package (multido) is loaded by pst-plot
%\usepackage{multido}% http://ctan.org/pkg/multido
\begin{document}

\def\f(#1){-#1*(#1-8)/4}% function f(x)=-x*(x-8)/4
% PS definition of f(x): x neg x 8 sub 4 div mul
\def\halfsize{0.5\space}% Modify this for smaller squares

\begin{pspicture}
  \multido{\ix=0+1}{9}{%
    \psframe[linewidth=0.5pt,linecolor=red!50]% Red squares
      (!\ix\space \halfsize sub \ix\space neg \ix\space 8 sub 4 div mul \halfsize sub)% bottom left
      (!\ix\space \halfsize add \ix\space neg \ix\space 8 sub 4 div mul \halfsize add)% upper right
  }
  \psplot[algebraic]{0}{8}{\f(x)}% Plot function
\end{pspicture}

\end{document}

The postscript formulation of \f(x)=-x*(x-8)/4 is

x neg x 8 sub 4 div mul

Now you can specify the node positions using postscript notation/coordinates (!<x> <y>) (excuse the ASCII art):

         (     x - 0.5     ,      - x    *      (x - 8) / 4          - 0.5 )

         |     x - 0.5     |      - x    *      (x - 8) / 4          - 0.5 |
         |     x - 0.5     |      - x    *      (x - 8) / 4        |0.5| - |
         |     x - 0.5     |      - x    |      (x - 8) / 4        |0.5| - |
         |     x - 0.5     |      - x    |      x - 8    |         |0.5| - |
         |    x    |0.5| - |    x    | - |    x    |8| - |4| / | * |0.5| - |
(x,y) ~ (!\ix\space 0.5 sub \ix\space neg \ix\space 8 sub 4 div mul 0.5 sub)

It is required that you use \space after control sequences to introduce a space, otherwise elements are concatenated, leading to incorrect output (or sometimes input that doesn't compile). Also, note that there's no comma , separating the coordinates <x> and <y>; they are merely placed on a stack - first <x> and then <y>.

share|improve this answer
add comment

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.