4

This is the solution to defining a piecewise function for pgfplots.

However, I would like to be able to use a macro to define each of the pieces of the function. It is simple to do this if one is using a local declare function as shown in the MWE below which uses \LocalPieceA and \LocalPieceB as the definition of what the function does in the two separate domains.

But, how do I do modify \PieceA and \PieceB so that I can use them in the \pgfmathdeclarefunction? I attempted to use the ## trick that works when defining macros within macros that need to access the parameters of the outer macro, and Jake tried the \edef\PieceA{\noexpand#1} in the above referenced question and that too did not work.

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

\newcommand{\rLabel}{
$r(x)=
\begin{cases}
    x & 0 < x < 1\\
   -x+2 & 1 < x < 2\\
    0 & \text{otherwise}
\end{cases}$
}

% Want to be able to use \PieceA and \PieceB in the \pgfmathdeclarefunction instead 
% of (#1) and (-#1+2).  With [declare function], this is easy as shown below
\newcommand{\PieceA}{(##1)}
\newcommand{\PieceB}{(-##1+2)}

\pgfmathdeclarefunction{q}{1}{%
  \pgfmathparse{((and(#1>0, #1<1)*#1)+(and(#1>1, #1<4)*(-#1+2)))}%
}

\tikzstyle{MyPlotStyle}=[domain=-5:5, samples=50, ultra thick]
\tikzstyle{rLabelStyle}=[above, yshift=-15ex, xshift=-25ex]

\begin{document}
%------------------ Using \pgfmathdeclarefunction -----------
Plot of $r(x)$ using PGF Version \pgfversion.

\begin{tikzpicture}
  \begin{axis}[clip=false]
    \addplot[MyPlotStyle, red]{q(x)} node [rLabelStyle] {\rLabel};
  \end{axis}
\end{tikzpicture}

% --------------------- Using "declare function" -------------
Using declare function to define localp(x) and localq(x):

\newcommand{\LocalPieceA}{(\x)}% These two works great!!
\newcommand{\LocalPieceB}{(-\x+2)}
\begin{tikzpicture}
[declare function={localr(\x) =  and(\x > 0, \x < 1)*\LocalPieceA+and(\x > 1, \x < 4)*\LocalPieceB;}]
  \begin{axis}[clip=false]
    \addplot[MyPlotStyle, blue]{localr(x)}  node [rLabelStyle] {\rLabel};
  \end{axis}
\end{tikzpicture}
\end{document}
Moriambar
  • 11,466
Peter Grill
  • 223,288
  • 1
    When you define a command inside another command, #1 refers to the first parameter of the outer command and ##1 refers to the first parameter of the inner command. #1 is accessible in both the outer and inner commands, but ##1 is only accessible inside the inner command. – Display Name May 31 '11 at 17:59

1 Answers1

3

Is it what you need?

\newcommand{\PieceA}[1]{(#1)}
\newcommand{\PieceB}[1]{(-#1+2)}

\pgfmathdeclarefunction{q}{1}{%
  \pgfmathparse{((and(#1>0, #1<1)*\PieceA{#1})+(and(#1>1, #1<4)*\PieceB{#1}))}%
}
Display Name
  • 46,933