How to create and use a new pgf key

In this example, I would like to have a new key NbGrad then I could write :

\begin{tikzpicture}[>=stealth,x=6cm,font=\footnotesize,NbGrad=12]


And use the key as I use \NbGrad or another way if it is better :

\documentclass[10pt,a4paper,french]{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[>=stealth,x=6cm,font=\footnotesize]

\draw [->] (0,0)--(1,0)  ;

\pgfmathsetmacro\result{2 + \x / 10}
\node[above=1pt]  at (\x)%
{\pgfmathprintnumber[precision=1,fixed,use comma]{\result}};

} ;

\end{tikzpicture}
\end{document}


• If I want to add a "noze" to the axis \draw [->] (0,0)--(1+5pt,0) ; doesn't work. How to do that ? – Tarass Feb 15 '14 at 10:36

Another way:

\documentclass[10pt,a4paper]{article}
\usepackage{tikz}

\pgfkeys{/tikz/.cd,
}

\begin{document}

\draw [->] (0,0)--(1,0)  ;

\pgfmathsetmacro\result{2 + \x / 10}
\node[above=1pt,text depth=0.5ex]  at (\x)%
{\pgfmathprintnumber[precision=1,fixed,use comma]{\result}};

} ;

\end{tikzpicture}
\end{document}


As noted by Claudio, we can assign an initial value to NbGrad so that, in case no explicit definition is given afterwards, the document still compiles, like

\pgfkeys{/tikz/.cd,
}

• Thank's to both of you, both answers are acceptable. I pick up the shortest as initial isn't usefull in this case for me, but I note this possibilty for an other case. – Tarass Feb 15 '14 at 9:48
• @karlkoeller Done ;-) I don't now all the rules but obviusly your answer diserve an upvote. And I put a bookmark on it to use it in the future. – Tarass Feb 15 '14 at 10:04
• @Harish: after having stored the value into \NbGrad, you can add an intial value with NbGrad=<something> so that, in case no explicit definition is given afterwards, the document still compiles. – Claudio Fiandrino Feb 15 '14 at 10:13
• @ClaudioFiandrino Nice suggestion. Thank you and added. :) – user11232 Feb 15 '14 at 10:24

You can create a new pgf key with initial value 1

\pgfkeys{/tikz/NbGrad/.initial=1}


and then create a new command \NbGrad that permits you to use its value:

\newcommand{\NbGrad}{\pgfkeysvalueof{/tikz/NbGrad}}


Note that, if you don't specify its value in your tikzpicture, the default value 1 is used.

MWE:

\documentclass[10pt,a4paper,french]{article}
\usepackage{tikz}

\begin{document}

\draw [->] (0,0)--(1,0)  ;

\pgfmathsetmacro\result{2 + \x / 10}
\node[above=1pt]  at (\x)%
{\pgfmathprintnumber[precision=1,fixed,use comma]{\result}};

} ;

\end{tikzpicture}

\bigskip

\begin{tikzpicture}[>=stealth,x=6cm,font=\footnotesize]

\draw [->] (0,0)--(1,0)  ;