1

What is wrong with my use of the samples parameter in combination with \pgfmathdeclarefunction?

A minimal offending example is as follows:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\pgfmathdeclarefunction{myfunc}{1}{\pgfmathparse{ sin(#1) } }
\begin{tikzpicture}[]
\begin{axis}[ domain=0:360, samples=100]
  \addplot +[thick] { myfunc(x) };
\end{axis}
\end{tikzpicture}
\end{document}

The crazy thing is that changing the value of the samples parameter shifts the picture to the right.

enter image description here

If I edit the minimal example to samples=10 I get

enter image description here

And now, even more astonishing: If I remove the \pgfmathdeclarefunction declaration of the function and draw using sine directly, as in

 \addplot +[thick] { sin(x) };

the shifting effect on samples is gone.

Of course it seems superficial to use a function declaration in this simple case - but this is just the minimal offending example. The original document is much larger, containing some 15 function declarations, and does not offer the luxury of a direct plot.

Currently I have a workaround by placing a

 \hspace*{-10cm}

before the tizpicture, scaling the space according to the setting of the samples parameter.

However, I suppose that this rather is indicative of some peculiar bug in the tikz parser (or some peculiar misunderstanding of mine).

Any ideas how to solve this in a better manner?

Additional Observation: With further experimenting on my original document it looks like every function declaration which is invoked in the process of the drawing adds some additional horizontal space. Since in the original document this nesting is highly dynamic, it essentially makes the use of Tikz unfeasible, since I do not know the required horizontal corrections in advance.

First solution It looks like one partial workaround is to take out any additional blanks in the body of \pgfmathdeclarefunction. The solution also confirms that there is some bug in the parser.

7
  • Why exactly are you declaring the function like that and not via the declare function={myfunc(\x)=sin(\x);} option for the tikzpicture environment. – daleif Sep 2 '20 at 13:55
  • The original document has highly nested function declarations which change from plot to plot, so there it is an absolute necessity to keep things modular and flexible. Here I only present a minimal example of the bug. For the example given here, my approach seems unnecessary. – Nobody-Knows-I-am-a-Dog Sep 2 '20 at 14:26
  • 2
    Try removing the spaces from the function declaration – daleif Sep 2 '20 at 14:46
  • Yes, that helps. As a work around. Of course it is probably not the way it should be :-) So I'll bring it to the attention of the maintainers of the package. – Nobody-Knows-I-am-a-Dog Sep 2 '20 at 17:15
  • I still don't understand the need to define the function like this. Can't you combine declare function and \tikzset to get it globally – daleif Sep 2 '20 at 17:17
2

\pgfmathdeclarefunction{myfunc}{1}{\pgfmathparse{ sin(#1) } } contains blanks inside of the body of \pgfmathdeclarefunction It looks like these blanks are somehow responsible for the shift. The connection with the samples attribute is coincidental, as it affects the number of times the definition is called. The blanks inside of \pgfmathparse do not seem to influence the position.

Workaround: Removing the blanks.

Fix: Bug issue sent to tikz maintainers at github.

2
  • Very low quality issue reporting. Not appreciated. – Henri Menke Sep 2 '20 at 21:28
  • @HenriMenke: Everybody does things according to their own understandings of things. Sorry for not living up to your expectations. – Nobody-Knows-I-am-a-Dog Sep 3 '20 at 22:13
1

In the Git version of PGF (future 3.1.6) you can set /pgf/declare function/ignore spaces=true which will ignore all spaces in the body of a function defined with \pgfmathdeclarefunction. I don't recommend enabling this globally or when you do, immediately turn it off again, because this has the potential of breaking other things.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\pgfkeys{/pgf/declare function/ignore spaces=true}
\pgfmathdeclarefunction{myfunc}{1}{\pgfmathparse{ sin(#1) } }
\pgfkeys{/pgf/declare function/ignore spaces=false}

\begin{tikzpicture}[]
\begin{axis}[ domain=0:360, samples=100]
  \addplot { myfunc(x) };
\end{axis}
\end{tikzpicture}
\tracingnone
\end{document}
1
  • Looks like a reasonable approach for solving the issue with the path dependency which seems to be connected with the earlier problem of the tikz parser. It would be cool if in the future blanks in a function definition will have no impact on the formatting, this being an orthogonal concept. I do appreciate picking up the suggestion to find a path for repairing this. Thank you. – Nobody-Knows-I-am-a-Dog Sep 3 '20 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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