Tikz:如何定位从句子发出的箭头?

Tikz:如何定位从句子发出的箭头?

我希望创建下图(箭头可以更短):

在此处输入图片描述

这里,quick 指向 quick,jumps 指向 jumps,lazy 指向 lazy。

但是,下面的代码似乎创建了从句子中心发出的箭头。

在此处输入图片描述

有人可以帮忙吗?

\usepackage{tikz} 
\usetikzlibrary{positioning}
\usetikzlibrary{calc,decorations.pathmorphing,shapes}
\tikzset{txtnode/.style={text width=3cm,outer sep=2pt}}
\begin{tikzpicture}[font=\sffamily]
    \node (A) {The quick brown fox jumps over the lazy dog};
    \node[below left= 1cm and 0.1cm of A,txtnode] (B) {{quick}};
    \node[below = 1 cm of A,txtnode] (C) {\text{jumps}};
    \node[below right= 1cm and -0.4cm of A,txtnode] (D) {\text{lazy}};
    \foreach \n in {B,C,D} \draw[line width=1pt,->] (A) -- (\n);
\end{tikzpicture}   

答案1

目前还不清楚应该用什么来呈现你的形象。比如这样:

在此处输入图片描述

考虑到(现已删除)@carlatex 的回答,你的第一张图片可以通过以下方式复制

\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                positioning}

\begin{document}
    \begin{tikzpicture}[
    node distance = 7mm and 1mm,
every node/.style = {font=\sffamily, outer sep=2pt}
                        ]
\node (A) {The quick brown fox jumps over the lazy dog};
\node (B) [below left=of A]    {quick};
\node (C) [below=of A]         {jumps};
\node (D) [below right=of A]   {lazy};
    \foreach \n in {B,C,D} \draw[line width=1pt,->] (A) -- (\n);
\end{tikzpicture}  
\end{document}

但是,如果您喜欢用箭头连接两条图像线中的相同单词,那么使用包\subnode中定义的tikzmark可能是一个理想的解决方案。

编辑 以下是如何使用库的示例tikzmark

\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                positioning,
                tikzmark}

\begin{document}
\begin{tikzpicture}[remember picture,
    node distance = 7mm and 1mm,
every node/.style = {inner sep=2pt, font=\sffamily},
every edge/.style = {draw, line width=1pt,-Straight Barb}
                        ]
\node (A) {The \subnode{s1}{quick} brown fox \subnode{s2}{jumps} over the \subnode{s3}{lazy} dog};
\node (a) [below left=of A]     {quick};
\node (b) [below=of s2]          {jumps};
\node (c) [below right=of A]    {lazy};
\draw[->]   (s1) edge (a)   (s2) edge (b)   (s3) edge (c);
\end{tikzpicture}
\end{document}

在此处输入图片描述

附录: 关于在环境外的单词、数学变量和符号之间画箭头的简短说明tikzpicture

正如@Andrew Stacey 在他的评论中指出的那样,在这种情况下您应该使用\tikzmark\pgfmark\tikzmarknode\subnode仅供在节点内部使用tizpictures

有关它们的使用 ID 的详细信息,请参阅包/库文档,该文档是 LaTeX 安装的一部分,或者您也可以在加拿大运输安全局

举几个例子:

\documentclass[border=3.141592, varwidth]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                calc,
                positioning,
                quotes,
                tikzmark}
\tikzset{TM/.style = {% TikzMark
    overlay, remember picture,
    node distance = 7mm and 5mm,
every node/.style = {inner sep=2pt, font=\small\sffamily},
every edge/.style = {draw=red, semithick, line width=1pt,-{Straight Barb[scale=0.8]}},
every edge quotes/.append style = {anchor=center}
        }}

\usepackage{lipsum}  
\begin{document}
\vspace*{3\baselineskip} % make space for the tikzpicture

Use of \verb+\pgfmark+ around some in text \pgfmark{As}words\pgfmark{Ae} in text enable draw \pgfmark{Bs}arrows\pgfmark{Be} between them or to their labels:

\begin{tikzpicture}[TM]
\coordinate[above=1ex of $(pic cs:As)!0.5!(pic cs:Ae)$]  (a);
\coordinate[above=1ex of $(pic cs:Bs)!0.5!(pic cs:Be)$]  (b);
%
\draw   (a) edge[pos=1.2, "ABCD"] ++ (0,1)
        (a) edge[bend left, "arrow" above] (b);
\end{tikzpicture}

A node around \tikzmarknode[draw=blue]{a}{some words} in text means that we can
connect selected word in text together as well connect them with elements in maths environment \tikzmarknode[draw=blue]{b}{together}:
\[
\tikzmarknode{c}{\sum_{k=1}^n} k^{\tikzmarknode{d}{2}}
\]
\begin{tikzpicture}[TM]
\draw   (a) edge (b)
        (a) edge (c)
        (c.south) edge[bend right=90, looseness=2] (d);
\end{tikzpicture}

\lipsum[1][1-2]

\[
\tikzmarknode{a}{W} = \tikzmarknode{b}{F} \cdot \tikzmarknode{c}{D}
%
\begin{tikzpicture}[TM]
\coordinate[below left=of a,  label=below:Work]     (A);
\coordinate[below=of b,       label=below:Force]    (B);
\coordinate[below right=of c, label=below:Distance] (C);
%
\path   (a) edge (A)    (b) edge (B)    (c) edge (C);
\end{tikzpicture}
%
\vspace{2\baselineskip} % make space for tikzpicture
\]
\lipsum[1][3-5]
\end{document}

在此处输入图片描述

答案2

具有简单  tabular环境的解决方案pstricks

\documentclass[border=6pt, svgnames]{standalone}
\usepackage{pst-node}

\begin{document}

\centering\sffamily
\begin{tabular}{ccc}
 & The \rnode[lb]{Q}{quick}~brown fox \rnode[b]{J}{jumps}~over the \rnode[rb]{L}{lazy}~dog \\[3ex]
\rnode [rt]{Q2}{quick} &
\hskip1.4em \rnode[t]{J2}{jumps} &
\rnode [lt]{L2}{lazy}
\psset{nodesep=0.5ex, arrowinset=0.12, linejoin=1}
\foreach \s/\t in {Q/Q2,J/J2,L/L2}{\pcline{->, linecolor=IndianRed}(\s)(\t)}
\end{tabular}

\end{document} 

在此处输入图片描述

答案3

不过,似乎用森林 (链接至 CTAN)包。即,

\documentclass[border=3.141592]{standalone}  
\usepackage{forest}

\begin{document}
\begin{forest}
[{The quick brown fox jumps over the lazy dog.},s sep=20mm
[quick] [jumps] [fox]]
\end{forest}
\end{document}

它将生成以下内容: 在此处输入图片描述

这些肯定不是从句子中发出的箭头,但如果箭头部分不是必需的,那么我认为 forest 做得更好;语法要简单得多。同时,forest 基于 TikZ。语言学家用它来表示树(除了文档之外,文档也很棒,例如,这里)。

相关内容