是否可以在 tikzpicture 环境中绘制箭头,以便箭头超出环境范围?这是我的具体场景...
我有一个逐字环境(主要用于程序代码),我想在边缘/侧边栏绘制一个方框文本,并将箭头从该点附加到逐字环境中的一条线上。
答案1
这很容易做到,除了 之外\tikzmark
。您需要在 verbatim 环境中有一种方法可以暂时退出到 LaTeX,以便将 放置\tikzmark
在 verbatim 环境中。下面我使用了 verbatim 环境包裹listings
,并escapeinside
提供执行 LaTeX 宏的方法。
箭:
方框和箭头:
您也可以扩展它来绘制框。我已经在同一个宏中进行了扩展\DrawArrow
(\DrawBoxAndArrow
但也许您需要一个单独的宏):
笔记:
这确实需要两次运行。第一次确定位置,第二次进行绘图。
来自
\tikzmark
在正文旁边添加大括号。为了确保箭头不会越过文本,您需要选择
in=
、 和out=
角度,它们是宏的第一个可选参数\DrawArrow
。使用out=-60
足以让箭头在行末的逐字文本周围弯曲。
代码(箭头):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{listings}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand*{\DrawArrow}[3][]{%
% #1 = draw options
% #2 = left point
% #3 = right point
\begin{tikzpicture}[overlay,remember picture]
\draw [-latex, #1] ($(#2)+(0.1em,0.5ex)$) to ($(#3)+(0,0.5ex)$);
\end{tikzpicture}%
}%
\begin{document}
Here is some text from which I want to draw an arrow from here\tikzmark{start} into the verbatim environment below.
\begin{lstlisting}[escapeinside={(*}{*)}]
verbatim line 1
Here is a verbatim line and I want the arrow to
point here(*\tikzmark{end}*)
verbatim line 3
\end{lstlisting}
\DrawArrow[red, thick, out=-60, in=0]{start}{end}
\end{document}
代码:(方框和箭头):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{listings}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand*{\DrawBoxAndArrow}[4][]{%
% #1 = draw options
% #2 = left point, start of box
% #3 = right point, end of box
% #4 = point where arrow ends
\begin{tikzpicture}[overlay,remember picture]
\draw [#1] ($(#2)-(0.1em,0.25ex)$) rectangle ($(#3)+(0.1em,1.5ex)$);
\draw [-latex, #1] ($(#3)+(0.1em,0.5ex)$) to ($(#4)+(0,0.5ex)$);
\end{tikzpicture}%
}%
\begin{document}
Here is some text from which I want to draw an arrow from \tikzmark{startBox}here\tikzmark{endBox} into the verbatim environment below.
\begin{lstlisting}[escapeinside={(*}{*)}]
verbatim line 1
Here is a verbatim line and I want the arrow to
point here(*\tikzmark{endArrow}*)
verbatim line 3
\end{lstlisting}
\DrawBoxAndArrow[red, thick, out=-60, in=0]{startBox}{endBox}{endArrow}
\end{document}