4

As I was intent on moving function declarations further up, so as to use them in multiple files, I noticed something unexpected. The first code plots a thin black line. The second code plots thick blue dots. I asked a well-known LLM and got a plausible explanation together with a hallucination. Do you have a suggestion to declare functions higher up while preserving the style? See a motivation further down.

Example 1

\documentclass[border=3pt,tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\tikzset{define functions/.style={%
    declare function={
    A = 1.0;
    a = 0.5;
    f(\x) = A*\x^a;
    },
  },
}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    domain=0:100,
    samples=100,
  ]
    \addplot[define functions] {f(x)};
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Example 2

\documentclass[border=3pt,tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\tikzset{define functions/.style={%
    declare function={
    A = 1.0;
    a = 0.5;
    f(\x) = A*\x^a;
    },
  },
}

\pgfplotsset{
  every axis plot/.append style={define functions},
}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    domain=0:100,
    samples=100,
  ]
    % [define functions] not used here
    \addplot {f(x)};
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

LLM talking now:

Example 1

\addplot[define functions] {f(x)};

You explicitly attach the define functions style only to that plot.

No other styles are affected.

PGFPlots uses the default plot style, which is a thin black line (black, line width 0.4pt).

Example 2

\pgfplotsset{every axis plot/.append style={define functions}}
\addplot {f(x)};

You append the define functions style to every plot.

But here’s the subtlety: every axis plot applies before the default plot style is applied, and declare function is a key that is not strictly separate from plotting keys.

What happens internally:

PGFPlots interprets your appended style as including both the function declaration and the style from the first plot in your style list.

Because of key expansion order, the default line style is overridden.

The plot falls back to the first "unknown" style PGFPlots sees for drawing, which is often the blue mark=* style (used for points), producing thick blue dots.

Why this happens

every axis plot is applied before \addplot options are merged.

declare function is technically a "style key" in the PGFPlots system.

PGFPlots sees a style (from every axis plot) that contains no explicit draw key, so it falls back to its default marker style (mark=*, thick, blue).

When you attach [define functions] directly to \addplot, the default line style is preserved, so you get a thin black line.

How to fix it

You need to combine your define functions style with the standard line style:

if you want to keep PGFPlots defaults but still append:

\pgfplotsset{
  every axis plot/.append style={define functions, /pgfplots/line}
}

Me speaking again: No this doesn't work and nor do the several other suggestions I received.

Motivation: I would save different parameter values in distinct files and load them with, say \input{func1.tex}, so I can share code across multiple figures. The syntax used in declare function is natural, unlike an alternative like \pgfmathdeclarefunction{f}{1}{\pgfmathparse{1.0*#1^(0.5)}}. I'm not averse to trying something radically different from my current approach, including using LuaTeX or Python.

16
  • 1
    asking ai about tex code is only useful if you want to prove it may be taking over worlds, but it has yet to make inroads into this one. hallucination is common. and nothing in the provided explanation explains why pgfplots should apply the default style for marks to a plot. but what happens if you use define functions, line width=0.4pt, draw=black? I'm guessing you may still get the blue marks? Commented Nov 1 at 1:07
  • you're right! it has no effect. If there isn't a simple way, I'll stick with the first version, which can be made to work in my setup. Commented Nov 1 at 1:13
  • 1
    either I misunderstand the manual or this just doesn't work. \pgfplotsset{every axis plot/.append style={thin},} makes subsequent plots use thick, blue marks, too. cf. page 43 ... Commented Nov 1 at 2:07
  • 1
    @Jasper the problem is to make the function definition available globally - or within a local group, say - without turning all the plots into thick blue dots. at least, that's how I understand it. PatrickT will hopefully correct me if I've misunderstood. Commented Nov 1 at 16:45
  • 1
    @cfr Okay, I've tested it. apparently the declare function key is causing the line to be drawn in a different aesthetic style than if it were drawn without it. As such, I upvoted the question because I am curious as to why - this is not what I would expect. Commented Nov 1 at 17:09

2 Answers 2

4

Using [] (or any key) for \addplot overrides the blue dots, yielding a regular thin black curve composed on line segments.

Your issue is produced also with this MWE. Try removing the empty [] key set from \addplot and see the difference.

I didn't check the source code, but I presume that this is because the optional parameter has a default set of key(s).

\documentclass[border=3pt,tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
    \addplot[]{sqrt(x)};
  \end{axis}
\end{tikzpicture}
\end{document}
3
  • 2
    Oh that's nice and simple, thank you @Jasper! Commented Nov 1 at 22:16
  • did you figure out why? Commented Nov 1 at 23:52
  • 1
    @cfr I have no canonical answer, but perhaps addplot has a default optional parameter possibly. Commented Nov 2 at 0:19
3

I am not sure if I understand the problem... maybe you can shift the "locality" of function definitions to the tikzpicture environment?

\documentclass[border=3pt,tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\tikzset{define functions/.style={%
    declare function={
    A = 1.0;
    a = 0.5;
    f(\x) = A*\x^a;
    },
  },
}


\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        domain=0:100,
        samples=100,
        ]
        % [define functions] not used here
        \addplot {x};
    \end{axis}
\end{tikzpicture}

\begin{tikzpicture}[define functions]
    \begin{axis}[
        domain=0:100,
        samples=100,
        ]
        % [define functions] used here: they are local to the tikzpicture now
        \addplot {f(x)};
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Regarding the "blue dots", this is standard addplot behavior. If you do not put any option (not even a void []), the plot options are taken from the default "cycle list":

enter image description here

As soon as any option is added, the style starts from the default (black thick line etc), unless you use \addplot+, in which case all the options are added to the default cycle list.

The rationale is, I suppose, to be able to simply say

\addplot {x};
\addplot {x*x};
\addplot {x*x*x};

...and have visually different lines with minimum effort (you can set up the cycle list if you want to change the defaults).

Nothing on this is related to the functions, though. However, they must be defined outside the axis environment, as that environment is often executed multiple times, which can lead to unexpected errors. Try to surrond the addplot with \begin{scope}[declare functions] and see it exploding...

5
  • Thanks for your interest @Rmano. So indeed I was looking to move it higher up in the preamble. Also this solution produces blue dots instead of black lines, as mentioned in the two examples I was comparing. Do you happen to know why? Commented Nov 1 at 9:44
  • this seems even weirder @PatrickT? the code doesn't even append to the style and you still get the thick blue marks. Commented Nov 1 at 16:42
  • 1
    as I understand it, the problem is precisely the change to the default plot style shown in your output i.e. all plots are now thick blue dots ... Commented Nov 1 at 16:47
  • 1
    @cfr, yes I agree. Perhaps I'll just give up on the plan altogether and be content with the first option! Commented Nov 1 at 22:18
  • @PatrickT I added some reference to the seemingly strange addplot behavior. Commented Nov 2 at 10:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.