3

I want to avoid using \usepackage[nomessages]{fp} and

\FPeval\height{round(6*sin(pi/3):6)}%
\begin{pspicture}(6,\height)

just for the sake of getting more compact code, i.e., without using a temporary variable \height. It is not a big problem to use the above approach actually but I am just curios whether or not I can make it more compact.

What I want to achieve is to check whether or not there is a useful package in PGF to let me rewrite the above code as follows.

\begin{pspicture}(6,6*sin(pi/3))

I have seen someone use calc but I have not tried it yet. The complete code is given as follows.

\documentclass[pstricks,border=12pt]{standalone}
\SpecialCoor
\usepackage[nomessages]{fp}

\begin{document}
% not so compact
\FPeval\height{round(6*sin(pi/3):6)}%
\begin{pspicture}(6,\height)
\pspolygon(6,0)(6;60)
\end{pspicture}

% preferred
\begin{pspicture}(6,6*sin(pi/3))
\pspolygon(6,0)(6;60)
\end{pspicture}
\end{document}

Any suggestions are welcome.

4
  • If there is no answer in 7 days, I will delete this question to save more space on the server. Jan 5 '14 at 15:30
  • 2
    I don't think, that only using calc for this is possible, because it only supports +-*/ and no functions like sin. Jan 5 '14 at 15:44
  • @HenriMenke: OK. It means that I need sinful packages. Jan 5 '14 at 15:55
  • If this is acceptable, you can express sine through the first few terms of the taylor series using only +-*/. Nevertheless you will still need a temporary length to store the value. Jan 5 '14 at 16:03
6

Remarks

You can use l3fp which supports many builtin functions. It is automatically loaded by xparse.

Let's look at the definition and elaborate what it does

\DeclareExpandableDocumentCommand{\eval}{m}{ \fp_to_decimal:n {#1} }
  1. \DeclareExpandableDocumentCommand is 'expandable', which allows \eval to be used where TeX 'expects' a number: most document commands should be 'protected', which is what \NewDocumentCommand does.
  2. {\eval} is the name of the new macro.
  3. {m} stands for one mandatory argument (o for optional, s for star).
  4. { \fp_to_decimal:n {#1} } is the definiton of \eval. It calls \fp_to_decimal from l3fp with the first argument #1. The n after the colon stands for the type of value, that is expected by \fp_to_decimal; n stands for a braced token list.

Implementation

\documentclass[pstricks,border=12pt]{standalone}
\SpecialCoor

% FP
\usepackage[nomessages]{fp}

% l3fp
\usepackage{xparse}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\eval}{m}{ \fp_to_decimal:n {#1} }
\ExplSyntaxOff

% PGF
\usepackage{tikz}

\begin{document}
% FP
\FPeval\height{round(6*sin(pi/3):6)}%
\begin{pspicture}(6,\height)
\pspolygon(6,0)(6;60)
\end{pspicture}

% l3fp
\begin{pspicture}(6,\eval{6*sin(pi/3)})
\pspolygon(6,0)(6;60)
\end{pspicture}

% PGF
% don't forget "r" to tell PGF, that the argument is in radians
\pgfmathparse{6*sin(pi/3 r)}
\begin{pspicture}(6,\pgfmathresult)
\pspolygon(6,0)(6;60)
\end{pspicture}
\end{document}
11
  • +1 but I am waiting for someone who can remove \ExplSyntaxOn \NewDocumentCommand{\eval}{m}{ \fp_to_decimal:n {#1} } \ExplSyntaxOff if possible. Jan 5 '14 at 15:35
  • @StiffJokes \edef\eval#1{\expandafter\noexpand\csname\detokenize{fp_to_decimal:n}\endcsname{#1}} :-)
    – Joseph Wright
    Jan 5 '14 at 15:36
  • Now featuring PGF. Jan 5 '14 at 15:39
  • @henrimenke I'd avoid making this unexpandable, as you may well want to to evaluation in an expansion context
    – Joseph Wright
    Jan 5 '14 at 15:39
  • @JosephWright How can I expand the argument #1 first? I don't know how to \expandafter in expl3. Jan 5 '14 at 15:41

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.