[Tex/LaTex] Adding Arrows to Lines Drawn in TikZ

arrowstikz-pgf

I am using the markup listing from Draw comparison lines between two listings.

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{tikz}% http://ctan.org/pkg/pgf
\usetikzlibrary{calc}

% https://tex.stackexchange.com/questions/1559/adding-a-large-brace-next-to-a-body-of-text/1570#1570
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

\lstset{
    frame=single,
    mathescape % Allows escaping to (La)TeX mode within $..$
}

\begin{document}

\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}
  1 $\tikzmark{L1line1}$
  2
  3
  4 $\tikzmark{L1line4}$
\end{lstlisting}
  \end{minipage}\hfill
  \begin{minipage}{.5\textwidth}
\begin{lstlisting}
 $\tikzmark{L2line1}$ 86
 $\tikzmark{L2line4}$ 4
\end{lstlisting}
   \end{minipage}

\tikz[overlay,remember picture] \draw[color=red] ($(L1line1)+(0pt,0.7ex)$) -- ($(L2line1)+(0pt,0.7ex)$);
\tikz[overlay,remember picture] \draw[color=blue] ($(L1line4)+(0pt,0.7ex)$) -- ($(L2line4)+(0pt,0.7ex)$);
\end{document}

This results in the following:

I want to add arrowheads on the end of each line – 4 in this case. I've attempted to edit the markup with the help of Drawing Arrows in Tables but haven't been able to progress much. Could someone please show me how arrowheads could be plotted in this case?

Best Answer

A @whlt3 mentioned in the comments, adding the -> option to the draw command will produce an arrow. There are different arrow option available, and below I have used -latex as I think that looked better in this case. In the linked question Drawing Arrows in Tables the arrow style -triangle 45 was used so may not have been obvious. A few of the arrow types are mentioned at Graph into graph with arrows, but many more are available -- see the TikZ/PGF documentation for more arrow options.

However, in this case I would also recommend that you also adapt a slightly different syntax so that you can specify the out and in angle for the line so that provide flexibility in case the straight line would overlap some other code to yield:

enter image description here

Notes:

  • This requires two runs. Once to determine the begin and end point of the lines, and the second to draw them.

Code:

\documentclass[border=5pt]{standalone}
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{tikz}% http://ctan.org/pkg/pgf
\usetikzlibrary{calc}

% https://tex.stackexchange.com/questions/1559/adding-a-large-brace-next-to-a-body-of-text/1570#1570
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

\lstset{
    frame=single,
    mathescape % Allows escaping to (La)TeX mode within $..$
}

\begin{document}

\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}[caption={Original code}]
  1 code $\tikzmark{L1line1}$
  2 some longer code here
  3 some longer code here
  4 more code$\tikzmark{L1line4}$
\end{lstlisting}
  \end{minipage}\hfill
  \begin{minipage}{.5\textwidth}
\begin{lstlisting}[caption={Code after insertion of detour}]
 $\tikzmark{L2line1}$ 86
 $\tikzmark{L2line4}$ 4
\end{lstlisting}
   \end{minipage}

\tikz[overlay,remember picture,-latex] \draw[color=red,thick,out=0,in=160] ($(L1line1)+(0pt,0.7ex)$) to ($(L2line1)+(0pt,0.7ex)$);
\tikz[overlay,remember picture,-latex] \draw[color=blue,thick,out=0,in=200] ($(L1line4)+(0pt,0.7ex)$) to ($(L2line4)+(0pt,0.7ex)$);
\end{document}