如何在两条文本行之间画箭头?

如何在两条文本行之间画箭头?

我想画这个图形。可以吗?
在此处输入图片描述

我编写了以下代码来绘制该图形:

    \documentclass[11pt,twoside,a4paper]{book}

\usepackage{xypic,wrapfig}
\begin{document}


\xymatrix{  
\verb|\begin{wrapfigure}|[12]{r}[34pt]{5cm} &  <figure> &  \verb|\end{wrapfigure}|\\


 & [<Number of narrow lines>]{<placement>}[<overhang>]{<width>}&
}

\end{document}

但失败了!


我已整理@Gonzalo Medina 的回答。

\documentclass{article}
\usepackage{tikz}

\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay]\node[inner ysep=4pt] (#1) {};}

\def\n#1{{\normalfont\ttfamily\small#1}}
\def\cs#1{{\normalfont\ttfamily\small\char`\\#1}}
\def\all{{\normalfont\ttfamily\small\char`\{}}
\def\cll{{\normalfont\ttfamily\small\char`\}}}
\def\aop#1{{\normalfont\ttfamily\small\char`\[#1\char`\]}}
\def\aob#1{{\normalfont\ttfamily\small\char`\{#1\char`\}}}
\def\gr#1{{\normalfont\ttfamily\small<\,#1\,>}}
\def\env#1{\all\n{#1}\cll}

\begin{document}

\hfill\cs{begin}\env{wrapfigure}\aop{\tikzmark{nla}12}%
\aob{\tikzmark{pla}r}\aop{\tikzmark{ova}34pt}\aob{\tikzmark{wia}5cm} %
\gr{figure} \cs{end}\env{wrapfigure}

\vskip20pt

\noindent\aop{\gr{number of \tikzmark{nlb}narrow lines}}%
\aob{\gr{\tikzmark{plb}placement}}\aop{\gr{\tikzmark{ovb}overhang}}%
\aob{\gr{\tikzmark{wib}width}}

\begin{tikzpicture}[remember picture,overlay]
\draw[->] ([xshift=15pt,yshift=3pt]nlb.north) -- ([xshift=5pt]nla.south);
\draw[->] ([xshift=15pt,yshift=3pt]plb.north) -- (pla.south);
\draw[->] ([xshift=15pt,yshift=3pt]ovb.north) -- ([xshift=8pt]ova.south);
\draw[->] ([xshift=15pt,yshift=3pt]wib.north) -- ([xshift=8pt]wia.south);
\end{tikzpicture}

\end{document}  

但这给出了:

在此处输入图片描述

答案1

一种选择是使用无处不在的\tikzmark命令放置一些标记,然后使用这些标记绘制箭头:

\documentclass{article}
\usepackage{tikz}

\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay]\node[inner ysep=4pt] (#1) {};}

\def\n#1{{\normalfont\ttfamily\small#1}}
\def\cs#1{{\normalfont\ttfamily\small\char`\\#1}}
\def\all{{\normalfont\ttfamily\small\char`\{}}
\def\cll{{\normalfont\ttfamily\small\char`\}}}
\def\aop#1{{\normalfont\ttfamily\small\char`\[#1\char`\]}}
\def\aob#1{{\normalfont\ttfamily\small\char`\{#1\char`\}}}
\def\gr#1{{\normalfont\ttfamily\small<\,#1\,>}}
\def\env#1{\all\n{#1}\cll}

\begin{document}

\hfill\cs{begin}\env{wrapfigure}\aop{\tikzmark{nla}12}%
\aob{\tikzmark{pla}r}\aop{\tikzmark{ova}34pt}\aob{\tikzmark{wia}5cm} %
\gr{figure} \cs{end}\env{wrapfigure}

\vskip20pt

\noindent\aop{\gr{number of \tikzmark{nlb}narrow lines}}%
\aob{\gr{\tikzmark{plb}placement}}\aop{\gr{\tikzmark{ovb}overhang}}%
\aob{\gr{\tikzmark{wib}width}}

\begin{tikzpicture}[remember picture,overlay]
\draw[->] ([xshift=15pt,yshift=3pt]nlb.north) -- ([xshift=5pt]nla.south);
\draw[->] ([xshift=15pt,yshift=3pt]plb.north) -- (pla.south);
\draw[->] ([xshift=15pt,yshift=3pt]ovb.north) -- ([xshift=8pt]ova.south);
\draw[->] ([xshift=15pt,yshift=3pt]wib.north) -- ([xshift=8pt]wia.south);
\end{tikzpicture}

\end{document}

在此处输入图片描述

定义命令\n\cs\all、...\env是为了方便编写类似代码的语法,而无需使用\verb

上述代码至少需要运行两次才能稳定。

我个人认为,没有必要使用这些箭头,我宁愿使用类似

\documentclass{article}
\usepackage[margin=3cm]{geometry}
\usepackage{listings}

\lstset{
  basicstyle=\small\ttfamily,
  breaklines=true
}

\begin{document}

\begin{lstlisting}
\begin{wrapfigure}[<narrow lines>]{<placement>}[<overhang>]{<width>}
(figure code)
\end{wrapfigure}
\end{lstlisting}
Where \lstinline{<narrow lines>} must be a positive integer,...

\end{document}

在此处输入图片描述

相关内容