Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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 5: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 7:27
add comment

1 Answer

up vote 5 down vote accepted

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}
share|improve this answer
    
This works great. One minor correction: The \pgfmathparse line requires a trailing % (at least with TexLive 2010) –  Peter Grill Jul 19 '11 at 7: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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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