8

I have attempted to do as follows, but it does not work.

\documentclass{minimal}
\usepackage{pstricks-add}

\psset
{
    xunit=\dimexpr1cm/\psPi\relax,
    yunit=1cm
}

\begin{document}
\begin{pspicture}[showgrid](0,-1)(\psPi,1)
    \psplot[plotpoints=300,algebraic]{0}{\psPi}{sin(2*x)}
\end{pspicture}
\end{document}

How to divide a length-valued macro by a dimensionless-valued macro?

  • I just noticed, this issue will disappear if I use an integer dimensionless value. But I need 3.141592654 rather than 3. – xport Jul 23 '11 at 10:19
7

You can't do that directly. In TeX, a length can be only divided by integers. eTeX's \dimexpr and \numexpr can do nothing but what Knuth TeX's primitives can do. Thus the calculation 1cm/3.14 is not supported.

There is a trick to do this kind of calculation in TeX:

\documentclass{minimal}
\def\psPi{3.1415926}
\newcount\tmpcntA
\newdimen\tmpdimA
\newcount\tmpcntB
\newdimen\tmpdimB

\begin{document}

\tmpdimA=2cm
\tmpcntA=\tmpdimA % 3729359, in sp
\tmpdimB=\psPi pt
\tmpcntB=\tmpdimB % 205887, in sp
\divide\tmpcntA by \tmpcntB % 3729359 / 205887 = 2cm / 3.1416926pt
\tmpdimA=\tmpcntA pt % 2cm / 3.1415926pt * 1pt
                     % = 2cm / 3.1415926

\the\tmpdimA % 2cm/3.1415926 = 18.0pt

\end{document}

But it's not precise because of truncation. The approch cannot be used in many situations.


The better way is to use other method to do the calculation: fp package, pgf's math engine, raw PostScript calculation, etc.

I'm not very familar with PSTricks, here is the pst-fp solution:

\documentclass{minimal}
\usepackage{pstricks-add}
\pstFPdiv\result{2}{\psPi} % pst-fp is used

\psset
{
  xunit=\result cm, % 0.636619773... cm
  yunit=1cm
}

\begin{document}
\begin{pspicture}[showgrid](0,-1)(\psPi,1)
  \psplot[plotpoints=300,algebraic]{0}{\psPi}{sin(2*x)}
\end{pspicture}
\end{document}

enter image description here

  • @Leo:In the ps-bundle exist a package called pst-fp. So you don't need a package like fp. The package pst-fp defines the command \pstFPdiv which is equivalent to \FPdiv. – Marco Daniel Jul 23 '11 at 10:50
  • @Marco: Thank you. I knew pst-fp, but I thought it has only \FPadd and \FPminus (in fact it also has multiply and divide, but no more). And pst-fp is just a copy of fp. – Leo Liu Jul 23 '11 at 10:54
  • '@Leo: Of course: In fact you don't need any further packages :-) because pst-fp is loaded automatically. I didn`t math the two packages ;-) – Marco Daniel Jul 23 '11 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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