5

I've been able to put a table of trig values together using expl3. However, I would like to pad zeros to the end so that all values have four decimal places. Is there a straight-forward way to do this in expl3 or do I need to build my own macro to handle this?

This is related to a similar question I just posted: Is there a simple way of improving the precision of pgfmath for trig functions?

Here's my MWE:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\def\mynum{0}
\def\myvoffset{0pt}

\usepackage{xparse}
\ExplSyntaxOn
\def\aetan#1{\fp_eval:n { round ( tand ( #1 ), 4 )}}
\def\aesin#1{\fp_eval:n { round ( sind ( #1 ), 4 )}}
\def\aecos#1{\fp_eval:n { round ( cosd ( #1 ), 4 )}}
\def\aemod#1{\fp_eval:n { #1 - round0 ( #1 / 5 , 0 ) * 5}}
\ExplSyntaxOff

\usepackage[margin=0.5in]{geometry}

\begin{document}

Using \LaTeX3

\begin{tikzpicture}

  \coordinate(UL) at (0,0);

  \node at (UL) {Degrees};
  \node[anchor=west] at ($(UL.west)+(1cm,0)$) {$\sin$};
  \node[anchor=west] at ($(UL.west)+(2.75cm,0)$) {$\cos$};
  \node[anchor=west] at ($(UL.west)+(4.50cm,0)$) {$\tan$};


  \foreach \myn in {1,2,3,...,45}
  {
    \ifnum\aemod{(\myn-1)}=0\relax
      \xdef\myvoffset{\dimexpr\myvoffset+1.350\baselineskip}%%
    \else
      \xdef\myvoffset{\dimexpr\myvoffset+1.00\baselineskip}%%
    \fi

    \coordinate (DEG/\myn)   at ($(UL.west)-(0,\myvoffset)$);
    \coordinate (DEG/S/\myn) at ($(DEG/\myn)+(1cm,0)$);
    \coordinate (DEG/C/\myn) at ($(DEG/S/\myn)+(1.75cm,0)$);
    \coordinate (DEG/T/\myn) at ($(DEG/C/\myn)+(1.75cm,0)$);

    \node[anchor=east] at (DEG/\myn)   {$\myn^\circ$};
    \node[anchor=west] at (DEG/S/\myn) {\texttt{\aesin{\myn}}};
    \node[anchor=west] at (DEG/C/\myn) {\texttt{\aecos{\myn}}};
    \node[anchor=west] at (DEG/T/\myn) {\texttt{\aetan{\myn}}};

  }

\end{tikzpicture}

\end{document}

enter image description here

1
  • If I remember correctly, we've deprecated round0 in favor of trunc (and similarly round- in favor of floor and round+ in favor of ceil). May 30, 2014 at 0:21

1 Answer 1

4

I've found something of a solution, though it's not an expl3 solution. I use the siunitx package as follows:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{siunitx}
\sisetup{add-decimal-zero,
         round-precision=4,
         round-mode=places,
         round-integer-to-decimal,
         detect-all}

\def\mynum{0}
\def\myvoffset{0pt}

\usepackage{xparse}
\ExplSyntaxOn
\def\aetan#1{\num{\fp_eval:n { round ( tand ( #1 ), 4 )}}}
\def\aesin#1{\num{\fp_eval:n { round ( sind ( #1 ), 4 )}}}
\def\aecos#1{\num{\fp_eval:n { round ( cosd ( #1 ), 4 )}}}
\def\aemod#1{\fp_eval:n { #1 - round0 ( #1 / 5 , 0 ) * 5}}
\ExplSyntaxOff

\usepackage[margin=0.5in]{geometry}

\begin{document}

Using \LaTeX3

\begin{tikzpicture}

  \coordinate(UL) at (0,0);

  \node at (UL) {Degrees};
  \node[anchor=west] at ($(UL.west)+(1cm,0)$) {$\sin$};
  \node[anchor=west] at ($(UL.west)+(2.75cm,0)$) {$\cos$};
  \node[anchor=west] at ($(UL.west)+(4.50cm,0)$) {$\tan$};


  \foreach \myn in {1,2,3,...,45}
  {

    \ifnum\aemod{(\myn-1)}=0\relax
      \xdef\myvoffset{\dimexpr\myvoffset+1.350\baselineskip}%%
    \else
      \xdef\myvoffset{\dimexpr\myvoffset+1.00\baselineskip}%%
    \fi

    \coordinate (DEG/\myn)   at ($(UL.west)-(0,\myvoffset)$);
    \coordinate (SIN/\myn) at ($(DEG/\myn)+(1cm,0)$);
    \coordinate (COS/\myn) at ($(SIN/\myn)+(1.75cm,0)$);
    \coordinate (TAN/\myn) at ($(COS/\myn)+(1.75cm,0)$);

    \node[anchor=east] at (DEG/\myn)   {$\myn^\circ$};
    \node[anchor=west] at (SIN/\myn) {\texttt{\aesin{\myn}}};
    \node[anchor=west] at (COS/\myn) {\texttt{\aecos{\myn}}};
    \node[anchor=west] at (TAN/\myn) {\texttt{\aetan{\myn}}};

  }

\end{tikzpicture}

\end{document}

enter image description here

1
  • 3
    Indeed, the fp module is meant for arithmetic operations, and zero-filling is more of a 'pretty-printing' requirement.
    – Joseph Wright
    Mar 22, 2014 at 8:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .