I want to define a piecewise function q(x), and attempted to adapt the solution to this question on using pgfmathdeclarefunction to create a unit pulse function, and this works fine. However, when I attempt to plot q(x+4)+0.5, the resulting graph is not what I would expect. However, applying the same transformation on the unit pulse function from the above mentioned link works fine.
So, is there a better way to define a piecewise defined function?
The MWE below produces the following result.

Note that the graphs on the left are as one would expect for both p(x) and p(x+4)+0.5. The graphs on the right are correct for q(x), but but incorrect for q(x+4)+0.5.
\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\newcommand{\pLabel}{
$p(x)=
\begin{cases}
1 & 0 < x < 1\\
0 & \text{otherwise}
\end{cases}$
}
\newcommand{\qLabel}{
$q(x)=
\begin{cases}
x & 0 < x < 1\\
0 & \text{otherwise}
\end{cases}$
}
\newcommand{\pShiftedLabel}{$p(x+4)+0.5$}
\newcommand{\qShiftedLabel}{$q(x+4)+0.5$}
\pgfmathdeclarefunction{p}{1}{%
\pgfmathparse{(and(#1>0, #1<1))}%
}
\pgfmathdeclarefunction{q}{1}{%
\pgfmathparse{(and(#1>0, #1<1)*x)}%
}
\tikzstyle{MyStyle}=[domain=-5:5, samples=50, ultra thick]
\tikzstyle{pLabelStyle}=[above, yshift=22ex, xshift=-10ex]
\tikzstyle{qLabelStyle}=[below, yshift=-2ex, xshift=-10ex]
\tikzstyle{ShiftedLabelStyle}=[above left, xshift=1ex]
\begin{document}
%------------------ Using \pgfmathdeclarefunction -----------
Plot of $p(x)$ and \pShiftedLabel using PGF Version \pgfversion, followed by a plot of $q(x)$ and \qShiftedLabel
\begin{tikzpicture}
\begin{axis}
\addplot[MyStyle, blue]{p(x)} node [pLabelStyle] {\pLabel};
\addplot[MyStyle, red]{p(x+4)+0.5} node [ShiftedLabelStyle] {\pShiftedLabel};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}
\addplot[MyStyle, blue]{q(x)} node [qLabelStyle] {\qLabel};
\addplot[MyStyle, red]{q(x+4)+0.5} node [ShiftedLabelStyle] {\qShiftedLabel};
\end{axis}
\end{tikzpicture}
% --------------------- Using "declare function" -------------
Using declare function to define localp(x) and localq(x):
\begin{tikzpicture}
[declare function={localp(\t) = and(\t > 0, \t < 1);}]
\begin{axis}
\addplot[MyStyle, blue]{localp(x)} node [pLabelStyle] {\pLabel};
\addplot[MyStyle, red]{localp(x+4)+0.5} node [ShiftedLabelStyle] {\pShiftedLabel};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
[declare function={localq(\t) = (and(\t > 0, \t < 1)*x);}]
\begin{axis}
\addplot[MyStyle, blue]{localq(x)} node [qLabelStyle] {\qLabel};
\addplot[MyStyle, red]{localq(x+4)+0.5} node [ShiftedLabelStyle] {\qShiftedLabel};
\end{axis}
\end{tikzpicture}
\end{document}

\newcommand{\PieceA}{x}and was using that in my original code (I eliminated that for the MWE). So I would have to use\pgfmathparse{(and(#1>0, #1<1)*\PieceA)}. Is there a simple way to change definition of\PieceAto get this to work. I tried the##1trick that is used when anewcommandis defined withinnewcommand, but that didn't work. – Peter Grill May 31 '11 at 08:27\edef\PieceA{\noexpand#1}, but that doesn't work. This should probably go into a new question, I'm sure there are some TeX cracks who know what to do. – Jake May 31 '11 at 08:37\PieceAdependent on the variable, i.e.\newcommands{\PieceA}[1]{#1}? This would allow you to replace\PieceAby some expression like\newcommand{\PieceA}[1]{(#1+4)}AND it would fix your problem because you could write...#1<1)*\PieceA{#1})}. But its just guessing. – Christian Feuersänger Aug 01 '11 at 21:22