1

Is there a pgfmath-function for something like

enter image description here

?

\documentclass[]{article}
\usepackage{tikz}
\usepackage{array}

\begin{document}
\def\myarray{{1,0,"-1","a"}}
\foreach \i in {0,...,3}{\pgfmathparse{\myarray[\i]}$\pgfmathresult$~ }

\pgfmathsetmacro\t{"1"}
\pgfmathparse{"\t" > 0 ? "yes" : "\t"}$\pgfmathresult$ \\

\newcolumntype{B}{ >{\bfseries}l  }
\begin{tabular}{B l B  l }
in: &   \verb+\foo{a}+  & out: & $a$ is a string  \\
in: &   \verb+\foo{\frac32}+  & out: & $\frac32$ is a string  \\
in: &   \verb+\foo{-1.2}+  & out: & $-1.2$ is negative  \\
in: &   \verb+\foo{0}+  & out: & $0$ is zero\\
in: &   \verb+\foo{2}+  & out: & $2$ is positive  \\
\end{tabular}
\end{document}

2 Answers 2

2

I may be wrong but I do not think that this is possible using \pgfmath alone. Is using LaTeX3/expl3 an option? If so then you can produce:

enter image description here

using the code:

\documentclass[]{article}
\usepackage{tikz}
\usepackage{expl3}

\ExplSyntaxOn
\newcommand\foo[1]{
  \str_if_eq:onTF {#1} {0} {$0$~is~zero} {
    \regex_match:noTF {^-[0-9]*$ } {#1} {$#1$~is~negative} {
      \regex_match:noTF {^[0-9]*$} {#1} {$#1$~is~positive} {
        #1~is~a~string
      }
    }
  }
}
\cs_generate_variant:Nn \regex_match:nnTF {noTF}
\ExplSyntaxOff

\begin{document}
\def\myarray{{1,0,"-1","a"}}
\foreach \i in {0,...,3}{\pgfmathparse{\myarray[\i]}$\pgfmathresult$~}

\noindent
\foreach \i in {0,...,3}{\pgfmathparse{\myarray[\i]}\foo{\pgfmathresult}\\}

\end{document}

The idea is to use the regular expression matching capabilities of LaTeX3 to recognise integers.

1

A possible pgfmath-solution:

\pgfmathfloatparsenumber{123.52}
\pgfmathfloattomacro{\pgfmathresult}{\F}{\M}{\E}
Flag: \F; Mantissa \M; Exponent \E
$\to$ \pgfmathfloattofixed{\pgfmathresult}  \pgfmathresult

where Flag has the meaning

enter image description here

With the command

\newcommand\foo[1]{
\pgfmathfloatparsenumber{#1}
\pgfmathfloattomacro{\pgfmathresult}{\F}{\M}{\E}
\pgfmathparse{\F==0 ? "$#1$ is zero" : (
\F==1 ? "$#1$ is positive" : (
\F==2 ? "$#1$ is negative" : (
\F==3 ? "$#1$ is a string (NaN)" : (
\F==4 ? "$\infty$" : (
\F==5 ? "$-\infty$" : ("I do not know"
))))))}\pgfmathresult}

we will get

enter image description here

\documentclass[border=3mm, varwidth]{standalone}
\usepackage{tikz, amsmath}
\usepgflibrary{fpu}

% Seems to be needed:
\pgfkeys{/pgf/fpu/handlers/invalid number/.code={%
  \pgfmathfloatparsenumber{3Y0.0e0]}%
  }
}

\begin{document}
$\begin{array}{c | c}
\text{Flag} & \text{Meaning} \\ \hline
0     &  0                       \\
1     &  \text{positive}    \\
2     &  \text{negative}    \\
3     &  \text{NaN}          \\
4     &  \infty                  \\
5     &  -\infty                 \\
\end{array}$

%Example:
%\pgfmathfloatparsenumber{123.52}
%\pgfmathfloattomacro{\pgfmathresult}{\F}{\M}{\E}
%Flag: \F; Mantissa \M; Exponent \E
%$\to$ \pgfmathfloattofixed{\pgfmathresult}  \pgfmathresult

\newcommand\foo[1]{
\pgfmathfloatparsenumber{#1}
\pgfmathfloattomacro{\pgfmathresult}{\F}{\M}{\E}
\pgfmathparse{\F==0 ? "$#1$ is zero" : (
\F==1 ? "$#1$ is positive" : (
\F==2 ? "$#1$ is negative" : (
\F==3 ? "$#1$ is a string (NaN)" : (
\F==4 ? "$\infty$" : (
\F==5 ? "$-\infty$" : ("I do not know"
))))))}\pgfmathresult}

\foreach \Test in {1,-1,0,1.23,a,sin(30)}{\foo{\Test} \\}

But: % \foo{\frac32}  % error
\end{document}

You must log in to answer this question.

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