4

I'm quite new to pgfplots and I must be doing something wrong. I'm plotting a piecewise function using the following code:

\documentclass{amsbook}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}

\newcommand{\DELTA}{.3}
\pgfmathdeclarefunction{fdelta}{1}{%
    \pgfmathparse{%
      and(#1 >= 1-\DELTA/2, #1 <= 1+\DELTA/2) * (1 - 2 * abs(#1 - 1) / \DELTA)
    }
 }

\begin{tikzpicture}
  \begin{axis}[
      title={$f_{\delta}(x)$},
      xmin=0, xmax=2,
      xtick={0, .85, 1.15, 2},
      xticklabels={0, \(1-\frac{1}{2}\delta\), \(1+\frac{1}{2}\delta\), 2},
      ytick={0, 1},
    ]
    \addplot[domain=0:2, ultra thick]{fdelta(x)};
  \end{axis}
\end{tikzpicture}

\end{document}

Now if I add the samples=11 option to \addplot, the picture changes position in the resulting PDF file! If I keep increasing the sample size, the picture eventually moves outside of the margins of the document...

In addition to the moving plot issue, finding an appropriate sample size for a piecewise linear function is a bit frustrating because the samples often "miss" the junction points. Is there a way to set samples piecewise as well?

As a secondary question, is it possible to set the values of xtick using \DELTA, as in the function definition?

Thanks!

Note: my example uses amsbook, but I observe the same behavior with standalone.

Dominique
  • 1,369
  • Maybe there is a spurious space somewhere, which accumulates with each evaluation of your function. Maybe you could try adding % signs at the end of every line in your function definition. – Fritz Sep 25 '14 at 15:41
  • 2
    You have more than one question here (all of them good). Better to ask them separately. – Matthew Leingang Sep 25 '14 at 16:15
  • For plotting piecewise linear functions, see http://tex.stackexchange.com/questions/63024/what-is-the-clearest-way-to-graph-a-piecewise-function – Matthew Leingang Sep 25 '14 at 16:18
  • For variable tick locations, you can use \pgfmathsetmacro{\tickone}{1-\DELTA/2}\pgfmathsetmacro{\ticktwo}{1+\DELTA/2} then xtick={0,\tickone,\ticktwo,2}. – Matthew Leingang Sep 25 '14 at 16:25
  • @MatthewLeingang Thanks for the tick trick! And sorry for the multiple questions. I figured it would be ok since they all related to the same plot, but I realize it would be better to ask separate questions. – Dominique Sep 25 '14 at 16:46
  • And just to answer the sampling question, I find it much more accurate to add a \addplot command per piece than to define a piecewise function. – Dominique Sep 25 '14 at 17:36

1 Answers1

4

I was able to eliminate the behavior by eliminating the space after \pgfmathparse.

\documentclass{article}
\pagestyle{empty}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}


\newcommand{\DELTA}{.3}
\pgfmathdeclarefunction{fdelta}{1}{%
    \pgfmathparse{%
      and(#1 >= 1-\DELTA/2, #1 <= 1+\DELTA/2) * (1 - 2 * abs(#1 - 1) / \DELTA)%
    }%
 }

\newcommand{\g}[1]{
\begin{tikzpicture}[scale=0.75]
  \begin{axis}[
      title={$f_{\delta}(x)$},
      xmin=0, xmax=2,
      xtick={0, .85, 1.15, 2},
      xticklabels={0, \(1-\frac{1}{2}\delta\), \(1+\frac{1}{2}\delta\), 2},
      ytick={0, 1},
      samples=#1
    ]
    \addplot[domain=0:2, ultra thick]{fdelta(x)};
  \end{axis}
\end{tikzpicture}}

\begin{document}

\g{10}

\g{11}

\g{20}


\end{document}

sample code output

Matthew Leingang
  • 44,937
  • 14
  • 131
  • 195
  • Interesting. Thanks! Could this be anything else than a bug? pgfmathdeclarefunction doesn't seem to appear in the PGFPlots manual I got with TeXLive 2014. – Dominique Sep 25 '14 at 16:42
  • @Dominique pgfmathdeclarefunction is described in the pgf manual, on page 948 of the 3.0.0 version – darthbith Sep 25 '14 at 16:47