# New command in TikZ with pgfkeys

The basic idea is from How can I create new commands in TikZ?

\documentclass[10pt]{article}
\usepackage[a4paper,top=1in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc,arrows.meta,bending,decorations.text,positioning}

\def\centerarct[#1](#2)(#3:#4:#5)% Syntax: [draw options] (center) (start angle:end angle:radius)
{ \path[#1] ($(#2)+({#5*cos(-#3+90)},{#5*sin(-#3+90)})$) arc [start angle={-#3+90}, end angle={-#4+90}, radius=#5)]; }

\pgfkeys{
myvector/.is family,
myvector,
line width/.initial =5mm,
color/.initial=orange,
text align/.initial=center,
text/.initial=Label,
raise/.initial=0mm
}

\newcommand\myvectorset[1]{\pgfkeys{myvector,#1}}

\def\myvector[#1](#2){
\myvectorset{#1,
line width/.get=\mbflinewidth,
color/.get=\mbfcolor,
text align/.get=\mbftextalign,
text/.get=\mbftext,
raise/.get=\mbfraise
}
}

\begin{document}
\begin{tikzpicture}
\myvector[text align=right,raise=-1.25mm](0,0);
\end{tikzpicture}
\end{document}


It works. But unfortunately if I use

\myvector[text align={right,right indent=1cm},raise=-1.25mm](0,0);


the result is

! Package pgfkeys Error: I do not know the key '/pgf/decoration/text align/righ t,right indent=1cm' and I am going to ignore it. Perhaps you misspelled it.

I read about key handlers but I didn't found a way out.

• Have you tried \myvector[{text align={right,right indent=1cm}},raise=-1.25mm](0,0);? – cfr Jul 4 '17 at 12:16
• I tried it just now. No change. – fatts Jul 4 '17 at 12:18
• I guess text align=\mbftextalign will treat \mbftextalign as a key instead of list of keys. I think it is better to use /.style to store list of keys. – Symbol 1 Jul 4 '17 at 12:33
• Why do you need to even create all of those keys? Most of them are just standard keys anyway, including text align, aren't they? – cfr Jul 4 '17 at 12:58
• For the mwe I changed the code to make it more easy. But in the end I want to use also own keys. The main goal is a command \myvector[options] (xcoord,ycoord); and options is key1=value1,key2=value2, ...; keys are standard, some are own and needed to calculate some thing for plotting the vector – fatts Jul 4 '17 at 13:04

This is the case where the .expand once handler is useful. In the myvector macro definition you have somewhere:

text align=\mbftextalign


What you want is to set text align to the expansion of the \mbftextalign macro. You can do this easily with:

text align/.expand once=\mbftextalign


Of course, you should do the same modification with the other keys where expansion is needed.

• This solves it! – berkus Jul 4 '17 at 17:29