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 the following graph, i would like the arrows to be originated inside the box and not in the border, possibly aligned and next to the text (for example, right after the word "Pointer:".

image

Here is the code i used:

\usetikzlibrary{shapes.multipart,arrows}
\newcommand{\npd}{\nodepart{two}}
\newcommand{\npt}{\nodepart{three}}
\newcommand{\npq}{\nodepart{four}}
\newcommand{\npc}{\nodepart{five}}

\tikzstyle{fnode}=[rectangle split, rectangle split part align={left,left,left,center,left}, rectangle split parts=5, draw, minimum width =2.75cm, rounded corners]

\begin{tikzpicture}
  \node[fnode] (r1) at (0,0) {Pointer:  \npd Key \npt Text text \npq Pointers \npc text};
  \draw [*->] (r1.one east) to (-2,2);
  \draw [*->] (r1.four east) to (3,0);
  \draw [*->] (r1.four west) to (-3,0);
\end{tikzpicture}

I would also like (if possible) to use a syntax similar to the anchor one. There are no predefined anchors to do this in the manual. What is the easiest way to achive this result?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Here is a possible solution.

You should specify an anchormark on the left or on the right of your pointer by means of \anchormark; its definition is:

\NewDocumentCommand{\anchormark}{O{0.15 cm} m O{0.05}}{%
        \tikz[overlay,remember picture,baseline=-0.5ex,xshift=#1] \node[draw,fill=black,circle,scale=#3] (#2) {};
}

Each anchormark should be uniquely identified (the mandatory argument of the command) and could be shifted (the first argument, optional, of the command) or the circle could be scaled (the third argument, optional again, of the command).

Notice that with this kind of approach you should logically divide the moment in which you deploy the fnode and the markers and the moment in which you create the connections. You should also compile twice.

The complete example:

\documentclass{article}
\usepackage{xparse}
\usepackage{tikz}

\usetikzlibrary{shapes.multipart,arrows}
\newcommand{\npd}{\nodepart{two}}
\newcommand{\npt}{\nodepart{three}}
\newcommand{\npq}{\nodepart{four}}
\newcommand{\npc}{\nodepart{five}}

\NewDocumentCommand{\anchormark}{O{0.15 cm} m O{0.05}}{%
        \tikz[overlay,remember picture,baseline=-0.5ex,xshift=#1] \node[draw,fill=black,circle,scale=#3] (#2) {};
}

\tikzset{fnode/.style={rectangle split, rectangle split part align={left,left,left,center,left}, rectangle split parts=5, draw, minimum width =2.75cm, rounded corners}}

\tikzset{label style/.style={draw, rounded corners}}

\begin{document}
% node deployment
\begin{tikzpicture}
  \node[fnode] (r1) at (0,0) {Pointer:\anchormark{first pointer}[0.075]  \npd Key  \npt Text text \npq \anchormark[-0.15cm]{pointer left}Pointers \anchormark{pointer right}\npc text};  
\end{tikzpicture}
% connection deployment
 \begin{tikzpicture}[remember picture,overlay,-stealth] 
  \draw (first pointer.center) to (2,2.75) node[right,label style]{label};
  \draw (pointer right.center) to (2,1) node[right,label style]{label};
  \draw (pointer left.center) to (-5,1)node[left,label style]{label};
\end{tikzpicture}

\end{document}

The result:

enter image description here


This advanced example shows how to deal with more modules. As said in the comment, by adding a [remember picture] to the tikzpicture where the nodes are deployed, in the connection phase it is possible to access their anchors. Another feature of the example is the use of the calc library to avoid absolute positioning in the connection phase.

\documentclass{article}
\usepackage{xparse}
\usepackage{tikz}

\usetikzlibrary{shapes.multipart,arrows,calc}
\newcommand{\npd}{\nodepart{two}}
\newcommand{\npt}{\nodepart{three}}
\newcommand{\npq}{\nodepart{four}}
\newcommand{\npc}{\nodepart{five}}

\NewDocumentCommand{\anchormark}{O{0.15 cm} m O{0.05}}{%
        \tikz[overlay,remember picture,baseline=-0.5ex,xshift=#1] \node[draw,fill=black,circle,scale=#3] (#2) {};
}

\tikzset{fnode/.style={rectangle split, rectangle split part align={left,left,left,center,left}, rectangle split parts=5, draw, minimum width =2.75cm, rounded corners}}

\tikzset{label style/.style={draw, rounded corners}}

\begin{document}
\begin{tikzpicture}[remember picture]
  \node[fnode] (r1) at (0,0) {Pointer:\anchormark{first pointer}[0.075]  \npd Key  \npt Text text \npq \anchormark[-0.15cm]{pointer left}Pointers \anchormark{pointer right}\npc text};  
  \node[fnode] (r2) at (5,0) {Pointer:\anchormark{second pointer}[0.075]  \npd Key  \npt Text text \npq \anchormark[-0.05cm]{second pointer left}Second pointer\npc text};  
  \end{tikzpicture}
 \begin{tikzpicture}[remember picture,overlay,-stealth] 
  \draw (first pointer.center) to ($(first pointer.center)+(2,1)$) node[right,label style] (mylabel){label};
  \draw (pointer right.center) to (second pointer left);
  \draw (pointer left.center) to ($(pointer left.center)+(-1,1)$)node[left,label style]{label};
  \draw (second pointer.center) |-(mylabel);
  % just to connect r1 and r2
  \draw(r1.south)|- ($(r1.south)!0.5!(r2.south)-(0,1)$)-|(r2.south);
\end{tikzpicture}
\end{document}

The result:

enter image description here

share|improve this answer
    
Thanks for the answer! It is a bit complicated (expecially for the separation between placing nodes and arrows, and the double compilation), but it does exactly what i wanted! Do you think it is possible to use this approach to define a custom type of node with the anchors builted in? This way I could use (for example) r1.FirstPointer without the double recompilation... I will study the manual and let you know if I manage to do it. –  antosecret Oct 31 '12 at 12:48
    
Also, adding [remember picture] after the \begin{tikzpicture} of the node definitions allows me to connect two such nodes. –  antosecret Oct 31 '12 at 13:22
    
@antosecret: answering your first comment. Well, I think this approach isn't so complicated.. at least is less complicated than create a custom node with anchors immediately there: how to deal with the text then? Suppose to change Pointers with Second pointer: with this approach nothing changes, at maximum you fix the distance with the optional parameter, but if you have predefined anchors your text will go over the the circle. Anyway, I'll edit my answer with another example (also to show with two fnodes how to connect them). –  Claudio Fiandrino Nov 2 '12 at 17:24
    
Thank you for the clarification! –  antosecret Nov 2 '12 at 21:51
add comment

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.