是否有类似这样的 pgfmath 函数
?
\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}
答案1
我可能错了,但我不认为\pgfmath
单独使用是可以实现的。使用LaTeX3/expl3一个选项?如果是的话,那么你可以制作:
使用代码:
\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}
这个想法是使用正则表达式匹配功能LaTeX3识别整数。
答案2
一种可能的 pgfmath 解决方案:
\pgfmathfloatparsenumber{123.52}
\pgfmathfloattomacro{\pgfmathresult}{\F}{\M}{\E}
Flag: \F; Mantissa \M; Exponent \E
$\to$ \pgfmathfloattofixed{\pgfmathresult} \pgfmathresult
哪里Flag
有意义
使用命令
\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}
我们将得到
\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}