我的代码基于在逐字文本上叠加红色矩形为了抓住在源代码上绘制箭头。
我已成功将解决方案合并到我的代码中,但出现了一个问题:当我在内转义时lstlisting
,它会在源代码中插入一个空格。
LaTeX 源
\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}
结果
问题
a
模板中和之间有两个空格{{
(一个是故意的,还有一个多余的)。- 模板中
}}
的之间有一个空格。.
value
代码后面加双倍空格。
它们为什么会出现?我该如何摆脱它们?
答案1
而不是将其封闭tikzpicture
在宽度为 0 的框中,因为@egreg 建议,使用为此制作的覆盖或使用tizkmark
库可能会更容易。
\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}
答案2
附上 Ti钾零宽度框中的 Z 图片:
\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}