4

How can i avoid the error message ! Package PGF Math Error: I cannot calculate the logarithm of '0' (in ' 0>1 ? ln(0) : 0'). in the following code? If possible, i want to evaluate the statement with one \pgfmathparse{...}.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\def\var{0}
\pgfmathparse { \var>1 ?   ln(\var)  : \var}\pgfmathresult
\end{document}
  • pgfmath evaluates both branches regardless of the condition. – percusse May 16 '16 at 21:44
6

You can add another guard within the ln and give a safe value (1 here) it doesn't really matter what non-error value you use as it will not affect the result of the outer guard

\documentclass{article}
\usepackage{tikz}
\begin{document}
\def\var{0}
\pgfmathparse { \var>1 ?   ln(\var>1 ? \var:1)  : \var}\pgfmathresult

\def\var{1}
\pgfmathparse { \var>1 ?   ln(\var>1 ? \var:1)  : \var}\pgfmathresult


\def\var{2}
\pgfmathparse { \var>1 ?   ln(\var>1 ? \var:1)  : \var}\pgfmathresult

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.