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}
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}
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.




pgfplotsshould apply the default style for marks to a plot. but what happens if you usedefine functions, line width=0.4pt, draw=black? I'm guessing you may still get the blue marks?\pgfplotsset{every axis plot/.append style={thin},}makes subsequent plots use thick, blue marks, too. cf. page 43 ...declare functionkey 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.