6

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}[caption={Original code}]
  1 $\tikzmark{L1line1}$
  2
  3
  4 $\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] \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?

4
  • 3
    Just add -> to the options to the draw commands at the end. That is, change color=red to color=red, ->. Same for the other. Jan 18, 2012 at 14:56
  • Adding -> to the \draw[color=red,->] command in \tikz[overlay,remember picture] \draw[color=red] ($(L1line1)+(0pt,0.7ex)$) -- ($(L2line1)+(0pt,0.7ex)$); should do the trick.
    – cjorssen
    Jan 18, 2012 at 14:57
  • 1
    I simplified your code to make the question more widely applicable. Please have a look at meta.tex.stackexchange.com/questions/228/… so you can do that yourself for your next questions. Note that the code probably still isn't a MWE because listings isn't even necessary for the issue with arrowheads. Reducing your code not only helps other users here who might help you, but also helps you understand your problem better.
    – doncherry
    Jan 18, 2012 at 15:52
  • @wh1t3: I think your comment is an answer- I would vote for it
    – cmhughes
    Jan 18, 2012 at 16:30

1 Answer 1

7

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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .