节点位置与列表覆盖

节点位置与列表覆盖

我正在尝试在代码清单上添加标签。我将有一个标记为“设备 A”的清单和一个标记为“设备 B”的清单。

我希望将标签绘制在列表的某个角落。我尝试使用 tikz 覆盖层执行此操作并成功。

但是,当列表是浮点数,并且太大而无法容纳在文本中时,乳胶会将其移动到另一页,tikz 的位置是错误的,并且节点会被绘制在我的列表最初的位置,我想。

我验证了这是由于乳胶移动列表造成的,因为 pageclear 解决了这个问题。

我该怎么做才能使 tikz 位置正确?

梅威瑟:

\documentclass{article}
\usepackage{tikz}
\usepackage{listings}
\usetikzlibrary{calc}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\begin{document}

text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 

text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 

text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 

%  If you remove this, the label is positioned on page one.
%\clearpage 

\begin{lstlisting}[escapechar= !,frame = single,float]
!\tikzmark{x1}!code

code

code

code

code

code

code

code

code

code

code

code

code

code

code
\end{lstlisting}


\tikz[overlay,remember picture]{
\node [fill=pink] (all) at ([xshift=\textwidth]x1) {Device A};
}
\end{document}

答案1

将 放入环境\tikz[overlay, remember picture]{... {Device A};}lstlisting,在 引入的转义 LaTeX 代码中escapechar,似乎可以实现您想要的效果。我还切换到\usetikzlibrary{tikzmark}以便使用标准\tikzmark命令(否则,使用您的自定义定义,当您开始使用依赖于标准的包时可能会遇到问题\tikzmark)。

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

\begin{document}

text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text

text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text

text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text

\begin{lstlisting}[escapechar=!, frame=single, float]
!\tikzmark{x1}%
 \tikz[overlay, remember picture]{
   \node [fill=pink] (all) at ([xshift=\textwidth]pic cs:x1) {Device A};
 }!code

code

code

code

code

code

code

code

code

code

code

code

code

code

code
\end{lstlisting}

\end{document}

在此处输入图片描述

相关内容