4

I am using this method for Defining a Piecewise Function for PGFplots and want to specifically define the function value at the endpoint to be 1, not 0. When I change the < in the and to <= I get Illegal unit of measure (pt inserted)

This MWE produces this output but should have the black circle at (1,1) when it works properly.

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}

\newcommand{\pLabel}{
$p(x)=
\begin{cases}
    x & 0 < x {\textcolor{red}{{}\leq{}}} 1\\
    0 & \text{otherwise}
\end{cases}$
}
\tikzstyle{MyStyle}=[domain=-0.5:1.5, samples=100, ultra thick,blue]
\tikzstyle{pLabelStyle}=[above, yshift=25ex, xshift=-28ex]


\pgfmathdeclarefunction{p}{1}{%
  \pgfmathparse{(and(#1>0, #1<1)*#1)}% This compiles with the "<"
  %\pgfmathparse{(and(#1>0, #1<=1)*#1)}% This is what I want: note the "<="
}

% Compute point of interest
\newcommand*{\XValue}{1}%
\pgfmathsetmacro{\QatXValue}{p(\XValue)}

\begin{document}
\begin{tikzpicture}
  \begin{axis}
    \addplot[MyStyle]{p(x)} node [pLabelStyle] {\pLabel};
    \fill (axis cs: \XValue,\QatXValue)  circle (4pt)
        node [right, yshift=1.0ex] {$(1,p(1))$};
\end{axis}
\end{tikzpicture}
\end{document}

Is there a simple fix?

Moriambar
  • 11,466
Peter Grill
  • 223,288
  • I'm not a heavy pgfmath user (used it once), but did you try using ">" and "not"? (I know, I know, this is trivial, but maybe this helps...?) – mbork Jul 19 '11 at 05:51
  • @mbork: I did not think to try that, but it appears that the solution was pretty much along those lines. Thanks for the suggestion. – Peter Grill Jul 19 '11 at 07:27

1 Answers1

5

This problem is similar to the one described in Using ifthenelse in pgfmath: It's an issue with the fpu library. As a workaround, you can define a new function that just negates the less function. You can assign this new function to an operator, e.g. !> for "not greater" (which is the same as "less or equal"):

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}

\newcommand{\pLabel}{
$p(x)=
\begin{cases}
    x & 0 < x {\textcolor{red}{{}\leq{}}} 1\\
    0 & \text{otherwise}
\end{cases}$
}
\tikzstyle{MyStyle}=[domain=-0.5:1.5, samples=100, ultra thick,blue]
\tikzstyle{pLabelStyle}=[above, yshift=25ex, xshift=-28ex]

\pgfmathdeclarefunction{floatnotgreater}{2}{%
    \pgfmathparse{!(#1>#2)}%
}

\pgfmathdeclareoperator{!>}{floatnotgreater}{2}{infix} {250}

\pgfmathdeclarefunction{p}{1}{%
  \pgfmathparse{(and(#1>0, #1!>1)*#1)}%
}

% Compute point of interest
\newcommand*{\XValue}{1}%
\pgfmathsetmacro{\QatXValue}{p(\XValue)}

\begin{document}
\begin{tikzpicture}
  \begin{axis}
    \addplot[MyStyle]{p(x)} node [pLabelStyle] {\pLabel};
    \fill (axis cs: \XValue,\QatXValue)  circle (4pt)
        node [right, yshift=1.0ex] {$(1,p(1))$};
\end{axis}
\end{tikzpicture}
\end{document}
David Carlisle
  • 757,742
Jake
  • 232,450
  • This works great. One minor correction: The \pgfmathparse line requires a trailing % (at least with TexLive 2010) – Peter Grill Jul 19 '11 at 07:26
  • You're right, I missed the % when I cleaned up the code and removed the comment line. Fixed now. – Jake Jul 19 '11 at 11:55