2

enter image description here

I have a pgfmath-function here, that tests whether the \Phase is equal to 'gas', then the value \Density has to be multiplicated by 1000 (else not) and that's the DensityShowTest. That works so far.

Now there is the case, that the \Phase equal to 'unknown' and the \Density is equal to '-1' (which means unknown as well).

Here I would like to have a question mark as output for the DensityShowTest. At the moment he puts out '777'. If I replaced 777 by "?" it does not work.

What do I have to do?

Note that a lot has been done with pgfmath in the main document, so I would like to work with a pgfmath solution.

\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}

2 Answers 2

3

You can use strings in pgfmath.

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

Full 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}

enter image description here

3
  • Normally yes, but in this case, he does not accept that.... try this syntax out.
    – cis
    May 29, 2021 at 20:12
  • @cis Added full MWE and screenshot. May 29, 2021 at 20:18
  • Works, TYVM. .......................................
    – cis
    May 29, 2021 at 22:26
4

Problem is \pgfmathparse wants to operate on numbers, not symbols (?). So I had to directly test for a string compare to 1.0 outside of pgf, using \pdfstrcmp. I therefore had to incorporate \pgfmathresult directly into the macro.

As is, this works in pdflatex and xelatex. For lua, you need the lua equivalent of etex's \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}

enter image description here

2
  • Ah, very nice. Helps me good.
    – cis
    May 29, 2021 at 20:01
  • I will use it this way, for the normal pgfmathparse pgfmathresult syntax: \pgfmathdeclarefunction{DensityShowTest}{2}{% \pgfmathparse{dim("#1")==3}% \ifnum\pgfmathresult=1% \pgfmathparse{1000*#2}% \else% \ifnum\pdfstrcmp{#2}{-1.0}=0 \pgfmathparse{"?"} \else\pgfmathparse{#2} \fi \fi}
    – cis
    May 29, 2021 at 20:18

You must log in to answer this question.

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