5

In the next code I would like to use three or four arguments. I tried

[add/.style args={#1 and #2 with #3}{to path={% ($(\tikztostart)!-#1!(\tikztotarget)$)--($(\tikztotarget)!-#2!(\tikztostart)$)% \tikztonodes coordinate[pos=-.25](#3)}}]

but I'd like the with #3 part to be optional.

\documentclass{standalone} 
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document} 

\begin{tikzpicture}[add/.style args={#1 and #2}{to path={%
 ($(\tikztostart)!-#1!(\tikztotarget)$)--($(\tikztotarget)!-#2!(\tikztostart)$)%
  \tikztonodes coordinate[pos=-.25](x)}}]

\coordinate (A) at (0,0);
\coordinate (B) at (3,2);
\draw[add=.5 and .5](A) to (B);
\foreach \point in {A,B,x}
\fill [red,opacity=.5] (\point) circle (2pt);

\end{tikzpicture}
\end{document}

enter image description here

Alain Matthes
  • 95,075

1 Answers1

5

You may want to try out a slightly different method. Just use one argument, which however is a list of keys. That way the syntax will always be the same even if you later decide to add some options/keys. These keys can have default/initial values, which can be changed at will. You can store these keys in a directory (I chose Alain here but you will of course change that), so you never run out of name space.

\documentclass{standalone} 
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{tikzpicture}[ add/.style={ Alains settings={#1}, to path={% ($(\tikztostart)!-\pv{first}!(\tikztotarget)$)--($(\tikztotarget)!-\pv{second}!(\tikztostart)$)% \tikztonodes coordinatepos=\pv{pos}% } }, Alains settings/.code={ \tikzset{Alain/.cd,#1} \def\pv##1{\pgfkeysvalueof{/tikz/Alain/##1}} }, Alain/.cd, first/.initial=0.5, second/.initial=0.5, coord/.initial=x, pos/.initial=-0.25 ] \coordinate (A) at (0,0); \coordinate (B) at (3,2); \drawadd to (B); \foreach \point in {A,B,x} {\fill [red,opacity=.5] (\point) circle[radius=2pt] node[above]{\point};}

\begin{scope}[xshift=5cm] \coordinate (A) at (0,0); \coordinate (B) at (3,2); \drawadd={first=0.3,second=0.8,coord=y,pos=-0.4} to (B); \foreach \point in {A,B,y} {\fill [red,opacity=.5] (\point) circle[radius=2pt] node[above]{\point};} \end{scope} \end{tikzpicture} \end{document}

enter image description here

In the left part the keys take their initial/default values but in the right part they get changed.

I personally find the usage also easier to remember.

Martin
  • 114
  • Thanks ! another example to study! I've never used \pgfkeysvalueof this will be an opportunity to understand how it works. I saw from your answers that you like this method. – Alain Matthes Mar 12 '20 at 16:57
  • 2
    @AlainMatthes You're welcome! In a way this just copies what TikZ does all along. For instance, in decorations you can say \draw[decorate,decoration={brace,mirror,raise=5pt},blue] ..., i.e. pass multiple optional keys to the decoration, and if you don't, it will use the initial/default values. And yes, I do like this kind of usage a lot, I am even using it now for non-TikZ-related problems because you can always upgrade your macros while maintaining backwards compatibility, and IMHO the usage is easier to remember. –  Mar 12 '20 at 17:35