I want to draw a macro for typeseting axis tick marks and labels. However, the following

\newcommand\obrPopisX[2][below]{\foreach \x/\xtext in {#2} 
    \draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[#1] {$\xtext$};}

does not produce good results of the optional parameter is "above" - the labels are too close, since they are attached to the point 2pt below the axis. As a partial workaround I can have

\newcommand\obrPopisX[2][below]{\foreach \x/\xtext in {#2} 
  {\draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt);
  \draw [shift={(\x,0)}] (0,0) node[#1] {$\xtext$};};}

but this sets labels relatively to the point on the axis, not the end of the tickmark. I wish to define macro, which sets the labels relatively to the whole line, i.e. behaves like

\draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node ...;

or like

\draw[shift={(0,\y)}] (-2pt,0pt) -- (2pt,0pt) node  ....;

depending on the fact if the keyword "above" is one of the optional parameters of the macro obrPopisX. Any idea? Probably I want to attach the label to the whole line rather than to the end point of the line. Thank you.


Here is one way to define the macro CoordinateAxisX which accepts paramters:

  • [<label-style>] = style applied to the tick labels
  • {<x-axis-label>} = x-axis label
  • {XAxisMin} = xmin for drawing the axis
  • {XAxisMax} = xmax for drawing the axis
  • {tick marks} = list of tick locations

The first one is the default setting, the second one is with the [above,red] options specified, and the third one is with the [below,blue] options specified:

enter image description here

Further Enhancements:

  • Note that this requires that there not be spacing around the above option. So using it with ..., above,..., or ...,above ,... will fail. This can easily be remedied with further use of the xstring package, but since it appears that the desire is not to use additional pacakegs, I have not added that enhancement.
  • This method also does not take into account the possibility that the below option can be specified via above,below. In this case the above option will be applied to the axis labels, even though as far as pgfkeys is concerned, the below option is the one that is active.




    % [<label-style>]{<x-axis-label>}{XAxisMin}{XAxisMax}{tick marks}
    \draw [gray, thin, -latex] (#3,0) -- (#4,0) node[right] {#2};%
        \foreach \x in \TickMarks {%
            \draw [gray, thin] (\x, -\TickSize) -- (\x,\TickSize)
                node [above, black, #1] {$\x$};
        \foreach \x in \TickMarks {%
            \draw [gray, thin] (\x, \TickSize) -- (\x,-\TickSize)
                node [below, black, #1] {$\x$};



  • Great, thanks. This is exactly what I want, but .... Is it possible to solve the problem inside tikz package, without xparse? I have to compile a lot of documents and I would like to avoid unnecessary packages. I also observed the difference between "[above,red]" and "[red, above]" but this is a minor problem for me just now. I also wonder if it is possible to skip zero in -4,...,4, but this is probably another question. – robert.marik.cz Nov 26 '12 at 8:45
  • Oh, I meant also without xstring .... – robert.marik.cz Nov 26 '12 at 9:25
  • @robert.marik.cz: Have eliminated the xparse package. I am only using xstring to be able to determine if the option above was specified. Any other method to determine that should work -- ideal would be to use pgfkeys to determine if that option is active. – Peter Grill Nov 26 '12 at 23:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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