3

I want to design a special axis label style. The x-axis is a straight line, no arrow. Below the end of this line is the actual label, it consists of an arrow with the label in the middle of the arrow. The arrow is exactly as long as the line itself. So far I constructed it like this:

\begin{tikzpicture}
\draw (-.5,0)--(6.5,0) ;
\draw [->,>=stealth] (5.5,-.5) -- (6.5,-.5) node [midway, fill=white, inner sep = 2pt]{$r$};
\end{tikzpicture}

While this looks the way I want it to look I'm not happy with this constructive way, I'd rather have a solution by nodes. However, when I tried to to this with nodes I didn't manage to align everything properly.

How can I achieve this?

Rainer
  • 377
  • Two things I don't understand: 1. "The arrow is exactly as long as the line itself" Which line? The axis line? In your sample code it is not... 2. "I'd rather have a solution by nodes" where do you want those nodes and why do you prefer such a solution? Can you post the code you tried "with nodes" to clarify? – JLDiaz Jul 11 '13 at 12:18
  • To 1: I mean the arrow ends on (6.5,...) and the line above as well at (6.5,...) they have both the same x-component. To 2: I want a node below the endpoint of the first path at (6.5,0). There will be more plots of this kind and I don't want to manually adjust all axis labels. I tried to achieve something like this by \begin{tikzpicture} \draw (-.5,0)--(6.5,0) node [below]{\tikz \draw [->,>=stealth] (5.5,-.5) -- (6.5,-.5) node [midway, fill=white, inner sep = 2pt]{$r$};}; \end{tikzpicture} but that didn't work out. – Rainer Jul 11 '13 at 12:21

1 Answers1

3

A possible solution: it does use nodes (the question too actually) by keys that insert paths (both in horizontal axis horizontal label and vertical axis vertical label way).

The alignment is preserved by accessing the last coordinate of the path thanks to Extract x, y coordinate of an arbitrary point in TikZ. The width/height of the arrow, according to axis horizontal label/axis vertical label selected mode, can be customized through devoted keys as the second example shows.

The code:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

% Code by Peter Grill:
% https://tex.stackexchange.com/a/33706/13304
\newdimen\xval
\newdimen\yval

%\newcommand*{\getxyval}[1]{\path (#1); \pgfgetlastxy{\xval}{\yval};}%

\pgfkeys{/tikz/.cd,
  axis label distance/.initial=0.5,
  axis label distance/.get=\axislabeldist,
  axis label distance/.store in=\axislabeldist,
  axis label width/.initial=1,
  axis label width/.get=\axislabelwd,
  axis label width/.store in=\axislabelwd,
  lb option/.style={midway, fill=white, inner sep = 2pt},
  append horizontal label/.style={
    insert path={
     \pgfextra{
       \pgfgetlastxy{\xval}{\yval};
       \draw[<-,>=stealth] 
        ($(\xval,\yval)-(0,\axislabeldist)$) -- 
        ($(\xval,\yval)-(\axislabelwd,\axislabeldist)$) 
        node [lb option]{$#1$};
      } 
    }
  },
  append horizontal label/.default={},
  append vertical label/.style={
    insert path={
     \pgfextra{
       \pgfgetlastxy{\xval}{\yval};
       \draw[<-,>=stealth] 
        ($(\xval,\yval)-(\axislabeldist,0)$) -- 
        ($(\xval,\yval)-(\axislabeldist,\axislabelwd)$) 
        node [lb option]{$#1$};
      } 
    }
  },
  append vertical label/.default={}
}


\begin{document}
\begin{tikzpicture}
\draw (-.5,0)--(3.5,0)[append horizontal label=r] ;
\end{tikzpicture}

\begin{tikzpicture}[axis label distance=0.25cm,
 axis label width=0.75cm]
\draw (-1.5,-1.5)--(-1.5,3)[append vertical label=y] ;

\draw (-2,-1)--(4,-1)[append horizontal label=x];
\end{tikzpicture}

\end{document}

The result:

enter image description here