5

Effectively, what I want to do is very simple but I am not sure where to go.

I have several nodes of the following form

\node (1) at (0,0) {text1 $\rightarrow$ text2};
\node (2) at (0,2) {text2 $\rightarrow$ text3};
\node (3) at (0,4) {text3 $\rightarrow$ text4};

I would like to replace the text arrow ($\rightarrow$) with a tikz arrow with a certain decorated arrow head. However, I do not want to specify the start and end of these arrows in each case. Is there a way to say

XXXXXXa (arrow from a.right where a is a character of text, to b.left, where b is also a text character) bXXXXXX

and further do this within the text for a node? I can then move this node wherever and the arrow stays between my two words of text.

I have been looking at nesting tikzenvironments however this seems to be bad practice so am hoping there is another way.

1
4

You can define a macro to do the drawing:

enter image description here

Notes:

  • The length of the arrow is determined by \ArrowLength so that can either be set globally or changed locally with \renewcommand{\ArrowLength}{<length>}.

Code:

\documentclass{article}
\usepackage{tikz}

\newcommand*{\ArrowLength}{2.0em}%
\newcommand*{\MyRightArrow}[1][]{%
    \tikz [-stealth, red, yshift=0.5ex, baseline] 
        \draw [-stealth, #1] (0,0) -- (\ArrowLength,0) ;
}%

\begin{document}
\begin{tikzpicture}
    \node (1) at (0,0) {text1 \MyRightArrow text2};
    \node (2) at (0,1) {text2 \MyRightArrow[blue]text3};
    \node (3) at (0,2) {text3 \MyRightArrow[red, densely dashed]text4};
\end{tikzpicture}
\end{document}
3
  • This is nesting tikz environments though correct? – James Oct 9 '14 at 6:22
  • I am not sure that using an inline \tikz is the equivalent of nesting a tikzpicture within a tikzpicture. The cover picture of the tikz-pgf manual actually uses \tikz within a \tikzpicture so I think it is ok. – Peter Grill Oct 9 '14 at 6:29
  • Ah ok. Maybe a somewhat duplicate question then. I asked separately as was assuming it was nested and there are similar questions about nesting environments etc. – James Oct 9 '14 at 6:43
4

You can use tikzmarks

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\begin{document}
   \begin{tikzpicture}[remember picture, myarrow/.style={->,yshift=.3em,shorten <=3pt,shorten >=3pt}]

     \node (1) at (0,0) {text1\tikzmark{t1} \phantom{$\rightarrow$} \tikzmark{t2}text2};
     \draw[myarrow](pic cs:t1)--(pic cs:t2);

     \node (2) at (0,2) {text3\tikzmark{t3} \phantom{$\rightarrow$} \tikzmark{t4}text4};
     \draw[myarrow](pic cs:t3)--(pic cs:t4);

     \node (3) at (0,4) {text5 $\rightarrow$ text6};
   \end{tikzpicture}
\end{document}

enter image description here

It needs two runs to draw the arrows in the correct positions. You can further tweak the style of the arrow (myarrow in the mwe) if you want to make it exactly the same as $\rightarrow$. I used phantoms to keep the distance between text.

tcolorbox solution

Another possibility is to load tcolorbox and draw an invisible box around the text you want to connect inside a node:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}

\newtcbox{mybox}[1]{
    enhanced jigsaw,
    size=minimal,
    on line,
    opacityback=0,
    opacityframe=0,
    remember as=#1,
}


\begin{document}
   \begin{tikzpicture}[remember picture,myarrow/.style={->,shorten <=3pt,shorten >=3pt}]

    \node (1) at (0,0) {\mybox{b1}{text1} \phantom{$\rightarrow$} \mybox{b2}{text2}};
    \draw[myarrow](b1.east)--(b2.west);
   \end{tikzpicture}
\end{document}

When you have the text boxed you can connect the boxes using standard node anchors (north, south, etc.) as if it were a node inside a node. This spare you from adjusting the xshift of the arrow, since you can anchor it to box.east. It still needs two compilations and the final result is basically the same as before.

1
  • 1
    I feel \tikz is an easier answer. \tikzmarks seem very useful though, thanks, I'll have to check them out. – James Oct 9 '14 at 6:42

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.