2

I'm basing my code on Overlay red rectangles on top of verbatim text in order to grab draw arrows over the source code.

I've successfully incorporated the solution into my code, however one problem appears: when I escape within lstlisting, it inserts a space in the source code.

LaTeX source

\begin{lstlisting}
@Template(`This is a /@\tikz[remember picture] \node [] (view-left) {};@/{{ value }}/@\tikz[remember picture] \node [] (view-right) {};@/.`)
class Component {
  value/@\tikz[remember picture] \node [] (code-right) {};@/ = 21
}
\end{lstlisting}
\begin{tikzpicture}[remember picture, overlay]
  \node (view) at ($(view-left)!.5!(view-right)$) {};
  \draw (view.south) edge [bend left] (code-right.south east);
\end{tikzpicture}

Result

The rendered result which shows additional spaces where escaping was done.

Problem

  • There are two spaces between a and {{ in the template (one was intentional, there's an extra one).
  • A space between }} in . in template.
  • Double space after value in the code.

Why do they appear and how can I get rid of them?

4

Instead of enclosing the tikzpicture in a box of width 0, as @egreg suggests, it is probably easier to work with overlay, which is made for this, or to use the tizkmark library.

\documentclass{article}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}

\lstset{
  basicstyle=\ttfamily\small,
  columns=fullflexible,
  escapeinside={/@}{@/}
}


\begin{document}

\begin{lstlisting}
@Template(`This is a /@\tikz[remember picture,overlay] \node [] (view-left) {};@/{{ value }}/@\tikz[remember picture,overlay] \node [] (view-right) {};@/.`)
class Component {
  value/@\tikz[remember picture,overlay] \node [] (code-right) {};@/ = 21
}
\end{lstlisting}
\begin{tikzpicture}[remember picture, overlay]
  \node (view) at ($(view-left)!.5!(view-right)$) {};
  \draw (view.south) edge [bend left] (code-right.south east);
\end{tikzpicture}

\begin{lstlisting}
@Template(`This is a /@\tikzmark{view-left}@/{{ value }}/@\tikzmark{view-right}@/.`)
class Component {
  value/@\tikzmark{code-right}@/ = 21
}
\end{lstlisting}
\begin{tikzpicture}[remember picture, overlay]
  \node (view) at ($(pic cs:view-left)!.5!(pic cs:view-right)$) {};
  \draw (view.south) to[bend left] ([yshift=-2mm]pic cs:code-right);
\end{tikzpicture}

\end{document}

enter image description here

3

Enclose the TikZ picture in a zero width box:

\documentclass{article}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{calc}

\lstset{
  basicstyle=\ttfamily,
  columns=fullflexible,
  escapeinside={/@}{@/},
}
\newcommand{\foo}[1]{%
  \makebox[0pt]{\begin{tikzpicture}[remember picture]
  \node [] (#1) {};
  \end{tikzpicture}}%
}

\begin{document}

\begin{lstlisting}
@Template(`This is a /@\foo{view-left}@/{{ value }}/@\foo{view-right}@/.`)
class Component {
  value/@\foo{code-right}@/ = 21
}
\end{lstlisting}
\begin{tikzpicture}[remember picture, overlay]
  \node (view) at ($(view-left)!.5!(view-right)$) {};
  \draw (view.south) edge [bend left] (code-right.south east);
\end{tikzpicture}

\end{document}

enter image description here

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.