Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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]
\def\NbGrad{12}

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

\foreach \x in {0,1,...,\NbGrad} {%
    \coordinate (\x) at (\x/\NbGrad,0) ;
    \draw [very thin] (\x/\NbGrad,+2pt)--(\x/\NbGrad,-2pt) ;

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

    } ;

\end{tikzpicture}
\end{document}

enter image description here

share|improve this question
    
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 at 10:36

2 Answers 2

up vote 9 down vote accepted

Another way:

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

\pgfkeys{/tikz/.cd,
  NbGrad/.store in=\NbGrad,
   }

\begin{document}


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

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

\foreach \x in {0,1,...,\NbGrad} {%
    \coordinate (\x) at (\x/\NbGrad,0) ;
    \draw [very thin] (\x/\NbGrad,+2pt)--(\x/\NbGrad,-2pt) ;

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

enter image description here

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,
  NbGrad/.store in=\NbGrad,
  NbGrad=12,
   }
share|improve this answer
    
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 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 at 10:04
1  
@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 at 10:13
    
@ClaudioFiandrino Nice suggestion. Thank you and added. :) –  Harish Kumar Feb 15 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}

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

\begin{document}


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

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

\foreach \x in {0,1,...,\NbGrad} {%
    \coordinate (\x) at (\x/\NbGrad,0) ;
    \draw [very thin] (\x/\NbGrad,+2pt)--(\x/\NbGrad,-2pt) ;

    \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)  ;

\foreach \x in {0,1,...,\NbGrad} {%
    \coordinate (\x) at (\x/\NbGrad,0) ;
    \draw [very thin] (\x/\NbGrad,+2pt)--(\x/\NbGrad,-2pt) ;

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

    } ;

\end{tikzpicture}

\end{document} 

Output:

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.