# Optional args for TikZ style

I have a defined a TikZ style "midcircle" that decorates a path with, say, a small circle in the middle (with the decorations.markings library, setting "pos=0.5"). E.g.

\draw (0,0)edge[midcircle](1,0);


would draw a straight line with a circle in the middle. Now I would like to be able to change the position of the circle by defining another style, say "midcirclepos", such that e.g.

\draw (0,0)edge[midcircle,midcirclepos=0.7](1,0);


would draw the circle at "pos=0.7" instead "pos=0.5". I think I've seen such constructions, but I can't find any more how to implement it.

By the way, I can't use the usual default arguments for TikZ styles, because my "midcircle" already takes other arguments that also have defaults.

• just put a variable in the value that you want to change like pos=#1 in your tikz style definition, then put the value midcircle=0.5, or another value like in this example see how it is defined InLineArrow style, – J Leon V. Sep 5 '18 at 14:15
• @JLeonV.: This doesn't work for me: The thing is that I want to have pos=0.5 as the default value, but I also have 2 other arguments passed to "midcircle" one of which also has a default value. However TikZ cannot handle more than one default value. (See last sentence) – Andi Bauer Sep 5 '18 at 14:20
• My first guess would be that the pos=0.7 still works, because it is a /tikz key and not a /pgf/decoration key. Otherwise you could use \tikzset{midcirclepos/.forward to=/tikz/pos}, but without an MWE who knows for certain. – Max Sep 5 '18 at 14:20
• @AndiBauer, there is a good answer from gernot , and some that I learn too +1 – J Leon V. Sep 5 '18 at 14:23

Define a key midcirclepos that takes as argument a value and stores it in the macro \midcirclepos. Initialize it with 0.5 and use \midcirclepos to access the value.

\tikzset{midcirclepos/.store in=\midcirclepos,midcirclepos=0.5}


Here is an example that also shows that the change to midcirclepos is temporary.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\tikzset{midcirclepos/.store in=\midcirclepos,midcirclepos=0.5}
\begin{document}
\begin{tikzpicture}[
midcircle/.style = {
postaction={decorate},
decoration = {
markings,
mark = at position \midcirclepos
with {\draw (0,0) circle (0.1);}
},
}
]
\draw (0,0) edge [midcircle] (1,0);
\draw (0,-1) edge [midcircle,midcirclepos=0.7] (1,-1);
\draw (0,-2) edge [midcircle] (1,-2);
\end{tikzpicture}
\end{document}


• @marmot What's wrong with macros in the context of a macro machine like TeX? Accessing a macro seems to be lightweight compared to \pgfkeysvalueof. – gernot Sep 5 '18 at 14:55