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.

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.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.

Code:

\documentclass{article}
\usepackage{xstring}
\usepackage{tikz}

\newcommand*{\IfIsInString}[4]{%
    \StrPosition{#1}{#2}[\Position]%
    \IfEq{\Position}{0}{#4}{#3}%
}%

\newcommand*{\TickSize}{2pt}%
\newcommand*{\CoordinateAxisX}[5][]{%
    % [<label-style>]{<x-axis-label>}{XAxisMin}{XAxisMax}{tick marks}
    \edef\TickMarks{#5}
    \draw [gray, thin, -latex] (#3,0) -- (#4,0) node[right] {#2};%
    \IfIsInString{,#1,}{,above,}{%
        \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$};
        }%  
    }%
}%


\newcommand*{\XMin}{-3}%
\newcommand*{\XMax}{3}%
\newcommand*{\XAxisLabel}{$x$}
\begin{document}
\begin{tikzpicture}
\CoordinateAxisX{\XAxisLabel}{\XMin}{\XMax}{-2,...,2}
\end{tikzpicture}

\bigskip
\begin{tikzpicture}
\CoordinateAxisX[above,red]{\XAxisLabel}{\XMin}{\XMax}{-2,...,2}
\end{tikzpicture}

\bigskip
\begin{tikzpicture}
\CoordinateAxisX[below,blue]{\XAxisLabel}{\XMin}{\XMax}{-2,...,2}
\end{tikzpicture}
\end{document}
share|improve this answer
    
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

 
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.