pgfmath 函数在某些情况下会显示问号

pgfmath 函数在某些情况下会显示问号

在此处输入图片描述

pgfmath这里有一个 -函数,用于测试 是否\Phase等于 'gas',然后\Density将其值乘以 1000(否则不等于),这就是DensityShowTest。到目前为止,它有效。

现在存在这样的情况,即\Phase等于“未知”并且\Density等于“-1”(这也意味着未知)。

在这里,我想在 DensityShowTest 中输出一个问号。目前,它输出的是“777”。如果我将 777 替换为“?”,则不起作用。

我需要做什么?

请注意,主文档中已经做了很多工作pgfmath,所以我想找到pgfmath解决方案。

\documentclass[margin=5pt, varwidth]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\pgfmathdeclarefunction{DensityShowTest}{2}{%
\pgfmathparse{dim("#1")==3}%
\ifnum\pgfmathresult=1%
  \pgfmathparse{1000*#2}%
\else%
   \pgfmathparse{#2==-1 ? 777 : #2}% here is a "?" needed instead of 777 !!!
\fi}
% usage:
% \pgfmathparse{DensityShowTest("gas", 0.0008)}\pgfmathresult 

\begin{document}
\xdef\Phase{liquid}  
\xdef\Density{1.23}  
\section{Phase: \Phase, Density is: \Density ~-- works}
Density show: 
\pgfmathparse{DensityShowTest("\Phase", \Density)}\pgfmathresult 

\xdef\Phase{gas}  
\xdef\Density{0.00008}  
\section{Phase: \Phase, Density is: \Density ~-- works too}
Density show: 
\pgfmathparse{DensityShowTest("\Phase", \Density)}\pgfmathresult 

\xdef\Phase{unknown}  
\xdef\Density{-1}% -1 means unknown <--- !  
\section{Phase: \Phase, Density is: \Density ~-- works not with a questionmark}
Density show: 
\pgfmathparse{DensityShowTest("\Phase", \Density)}\pgfmathresult 
\end{document}

答案1

您可以在 中使用字符串pgfmath

\pgfmathparse{#2==-1 ? "?" : #2}

完整 MWE:

\documentclass[margin=5pt, varwidth]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\pgfmathdeclarefunction{DensityShowTest}{2}{%
\pgfmathparse{dim("#1")==3}%
\ifnum\pgfmathresult=1%
  \pgfmathparse{1000*#2}%
\else%
   \pgfmathparse{#2==-1 ? "?" : #2}% here is a "?" needed instead of 777 !!!
\fi}
% usage:
% \pgfmathparse{DensityShowTest("gas", 0.0008)}\pgfmathresult 

\begin{document}
\xdef\Phase{liquid}  
\xdef\Density{1.23}  
\section{Phase: \Phase, Density is: \Density ~-- works}
Density show: 
\pgfmathparse{DensityShowTest("\Phase", \Density)}\pgfmathresult 

\xdef\Phase{gas}  
\xdef\Density{0.00008}  
\section{Phase: \Phase, Density is: \Density ~-- works too}
Density show: 
\pgfmathparse{DensityShowTest("\Phase", \Density)}\pgfmathresult 

\xdef\Phase{unknown}  
\xdef\Density{-1}% -1 means unknown <--- !  
\section{Phase: \Phase, Density is: \Density ~-- works not with a questionmark}
Density show: 
\pgfmathparse{DensityShowTest("\Phase", \Density)}\pgfmathresult 
\end{document}

在此处输入图片描述

答案2

问题是\pgfmathparse想要对数字而不是符号进行操作( )。所以我不得不直接使用来测试与 pgf 外部的?字符串比较。因此我不得不直接将其合并到宏中。1.0\pdfstrcmp\pgfmathresult

照原样,这在 pdflatex 和 xelatex 中有效。对于 lua,您需要 etex 的 lua 等效版本\pdfstrcmp

\documentclass[margin=5pt, varwidth]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\pgfmathdeclarefunction{DensityShowTest}{2}{%
\pgfmathparse{dim("#1")==3}%
\ifnum\pgfmathresult=1%
  \pgfmathparse{1000*#2}\pgfmathresult%
\else%
  \ifnum\pdfstrcmp{#2}{-1.0} = 0?\else\pgfmathparse{#2}\pgfmathresult\fi
\fi}
% usage:
% \pgfmathparse{DensityShowTest("gas", 0.0008)}

\begin{document}
\xdef\Phase{liquid}  
\xdef\Density{1.23}  
\section{Phase: \Phase, Density is: \Density ~-- works}
Density show: 
\pgfmathparse{DensityShowTest("\Phase", \Density)}

\xdef\Phase{gas}  
\xdef\Density{0.00008}  
\section{Phase: \Phase, Density is: \Density ~-- works too}
Density show: 
\pgfmathparse{DensityShowTest("\Phase", \Density)} 

\xdef\Phase{unknown}  
\xdef\Density{-1}% -1 means unknown <--- !  
\section{Phase: \Phase, Density is: \Density ~-- now works with a questionmark}
Density show: 
\pgfmathparse{DensityShowTest("\Phase", \Density)} 
\end{document}

在此处输入图片描述

相关内容