使用 yshift 在 tikz 中绘制一条线

使用 yshift 在 tikz 中绘制一条线

我试图从一个点到该点上方 1cm 的位置画一条线,但以下代码似乎不起作用。我本以为 yshift 可以解决问题,但没有出现任何线。

\documentclass[11pt]{book}
\usepackage[utf8]{inputenc}
\usepackage{pifont}
\usepackage{setspace}
\usepackage{needspace}
\usepackage[A4paper, portrait, margin=1in]{geometry}
\usepackage{qtree}
\usepackage{tipa}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{tikz-qtree-compat} %This seems to be necessary with covington
\usepackage{tikz} 
\usepackage{tikz-qtree}
\newsavebox{\partbox}
\usepackage{wrapfig}
\usepackage{multicol}
%\usepackage{makecell}
\usepackage{kotex}
\usepackage{covington}
\usepackage{fancybox}
\usepackage{makecell}
\usepackage[normalem]{ulem}

\newcommand\tikznode[3][]%
{\tikz[remember picture,baseline=(#2.base)]
    \node[minimum size=0pt,inner sep=0pt,#1](#2){#3};%
}
    
\begin{document}
        \begin{itemize}
        \item[] \tikznode{CP1}[\textsubscript{CP} John wonders \tikznode{CP2}[\textsubscript{CP} \tikznode{where}where Mary bought \tikznode{what}what ]] 
    \end{itemize}
    \begin{tikzpicture}[remember picture,overlay,rounded corners,>=stealth,shorten > =1pt,shorten <=1pt,thick]
        \draw[red,dashed,->] (what) -- +(0,+0.6) -| (CP1);
        \draw[double] ([yshift=1cm] where) -- (where);
    \end{tikzpicture}
\end{document}

答案1

使用tikzmarkTiZ 库:

\documentclass[11pt]{book}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\begin{document}
%    \begin{itemize}
%\item[] 
    \tikzmarknode{CP1}{[}\textsubscript{CP} John wonders [\textsubscript{CP} 
    \tikzmarknode{where}{where} Mary bought 
    \tikzmarknode{what}{what}.
%\end{itemize}
\begin{tikzpicture}[remember picture,overlay,
                    rounded corners, >=stealth, 
                    shorten >=1pt,shorten <=1pt,
                    thick]
\draw[red,dashed,->] (what.north) -- +(0,+0.6) -| (CP1);
\draw[double] (where.north) ++ (0,1.2) -- (where);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

附录: 如果在上面的 MWE 中替换代码行

\draw[double] (where.north) ++ (0,1.2) -- (where);

\draw[double] (where.north) ++ (0,1.1) -- + (0,-1);

然后(根据我的品味)生成的图像会更好看一点:

在此处输入图片描述

答案2

您使用\tikznode错了。它有两个参数,一个节点名称和一个要放入节点的文本,以及一个可选参数,该参数可以将选项传递给节点。您的用法完全很奇怪 --- 例如,第一个\tikznode参数为CP1[,第二个参数为CP2[,第三个参数为wherew,我想这不是您的意思(我错了,没有嵌套tikz调用!)

但是,主要问题[yshfit=1cm]是,由于 在您的代码中不起作用,所以(where)不是坐标,而是节点名称。您必须在此处传递一个坐标:

\draw[double] ([yshift=1cm] where.north) -- (where);

请注意,第二个(where)是可以的 --- 一个平面(node)被边框锚点自动替换 --- 即连接线的起点和位于边框上的节点中心的点。我为节点添加了可见边框以显示它(请注意,它不会触及选项的边框shorten),并在节点中只放置了“哪里”... 但我不知道这是什么意思!

顺便说一句:感谢马库斯· G.清除示例中不相关的包。

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{graphicx}
\usepackage{tikz} 
\usetikzlibrary{calc}

\newcommand\tikznode[3][]%
{\tikz[remember picture,baseline=(#2.base)]
    \node[minimum size=0pt,inner sep=0pt,#1](#2){#3};%
}

\begin{document}
    \begin{itemize}
        \item[] \tikznode{CP1}{\textsubscript{CP}} John wonders
             \tikznode{CP2}{\textsubscript{CP}}
             \tikznode[draw, blue]{where}{where} 
             Mary bought \tikznode{what}{what}
    \end{itemize}
    \begin{tikzpicture}[remember picture,overlay,rounded corners,>=stealth,shorten > =1pt,shorten <=1pt,thick]
        \draw[red,dashed,->] (what) -- +(0,+0.6) -| (CP1);
        \draw[double] ([yshift=1cm] where.north) -- (where);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

也许这个使用 tikzlibrary 的解决方案calc可以解决您的问题:

\documentclass[11pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{graphicx}
\usepackage{tikz} 
\usetikzlibrary{calc}

\newcommand\tikznode[3][]%
{\tikz[remember picture,baseline=(#2.base)]
    \node[minimum size=0pt,inner sep=0pt,#1](#2){#3};%
}

\begin{document}
    \begin{itemize}
        \item[] \tikznode{CP1}[\textsubscript{CP} John wonders \tikznode{CP2}[\textsubscript{CP} \tikznode{where}where Mary bought \tikznode{what}what ]] 
    \end{itemize}
    \begin{tikzpicture}[remember picture,overlay,rounded corners,>=stealth,shorten > =1pt,shorten <=1pt,thick]
        \draw[red,dashed,->] (what) -- +(0,+0.6) -| (CP1);
        \draw[double] ($(where)+(0,1cm)$) -- (where);
    \end{tikzpicture}
\end{document}

相关内容