# Pgfplots: picture moves with sample size!

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},
]
\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.

• 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. Sep 25, 2014 at 15:41
• You have more than one question here (all of them good). Better to ask them separately. Sep 25, 2014 at 16:15
• For plotting piecewise linear functions, see tex.stackexchange.com/questions/63024/… Sep 25, 2014 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}. Sep 25, 2014 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. Sep 25, 2014 at 16:46

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
]
\end{axis}
\end{tikzpicture}}

\begin{document}

\g{10}

\g{11}

\g{20}

\end{document}


• 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. Sep 25, 2014 at 16:42
• @Dominique pgfmathdeclarefunction is described in the pgf manual, on page 948 of the 3.0.0 version Sep 25, 2014 at 16:47