如何在 tikz 中的节点之间绘制负箭头?

如何在 tikz 中的节点之间绘制负箭头?

在此处输入图片描述

基于我之前的问题关于tikzpicture,如何将图像中的圆圈箭头作为节点之间的箭头,以及符号的名称是什么?我想我仍然在使用,draw但我还没能弄清楚。另外,如果能提供箭头选项列表,我draw将不胜感激。

目前我有以下内容:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\usetikzlibrary{positioning, arrows.meta}
\begin{document}
\begin{tikzpicture}
\node at (0,0) (qob) {Quarter of Birth};
\node[right=1cm of qob] (sch) {Schooling};
\node[right=1cm of sch] (wage) {Wage};
\node[below=1cm of sch] (abi) {Ability $\in \:u$};

\draw[$\nsenwarrows$] (abi) to (sch);
\draw[->, red] (abi) to (sch);
\draw[->] (abi) to (wage);
\draw[->] (qob) to (sch); %here I want to have "relevance" above the arrow
\draw[->] (sch) to (wage); %here i want a "?" above the arrow
\draw[\n$\nsenwarrows$] %here I want to replace the arrow with \nsenwarrows and write "exogeneity" below

\end{tikzpicture} 
\end{document}

答案1

将库decorations.pathreplacing和添加calc到您的序言中,以及以下内容tikzset

\tikzset{nsenwarrows/.style={decorate, decoration={show path construction,
    lineto code={
        \path (\tikzinputsegmentfirst); \pgfgetlastxy{\xstart}{\ystart};
        \path (\tikzinputsegmentlast); \pgfgetlastxy{\xend}{\yend};
        \path ($(0,0)!1.5pt!(\ystart-\yend,\xend-\xstart)$); \pgfgetlastxy{\xperp}{\yperp}; 
        \draw[->, shift={($(-\xperp,-\yperp)$)}, shorten <=1.5pt, shorten >=1.5pt, #1] (\xstart,\ystart)--(\xend,\yend);
        \draw[->, shift={(\xperp,\yperp)}, shorten <=1.5pt, shorten >=1.5pt, #1] (\xend,\yend)--(\xstart,\ystart);
        \draw[#1] ($.5*(\xstart,\ystart)+.5*(\xend,\yend)+4*(\xperp,\yperp)$)--($.5*(\xstart,\ystart)+.5*(\xend,\yend)-4*(\xperp,\yperp)$);
    }}}, nsenwarrows/.default={}
}

nsenwarrows然后,您可以使用语句中的键\draw来生成节点之间的否定箭头。例如:

\draw[nsenwarrows] (abi) to node[below left]{exogeneity} (qob);
\draw[nsenwarrows=red] (abi) to (sch);

在此处输入图片描述

完整代码如下:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, arrows.meta}
\usetikzlibrary{decorations.pathreplacing, calc}

\tikzset{nsenwarrows/.style={decorate, decoration={show path construction,
    lineto code={
        \path (\tikzinputsegmentfirst); \pgfgetlastxy{\xstart}{\ystart};
        \path (\tikzinputsegmentlast); \pgfgetlastxy{\xend}{\yend};
        \path ($(0,0)!1.5pt!(\ystart-\yend,\xend-\xstart)$); \pgfgetlastxy{\xperp}{\yperp}; 
        \draw[->, shift={($(-\xperp,-\yperp)$)}, shorten <=1.5pt, shorten >=1.5pt, #1] (\xstart,\ystart)--(\xend,\yend);
        \draw[->, shift={(\xperp,\yperp)}, shorten <=1.5pt, shorten >=1.5pt, #1] (\xend,\yend)--(\xstart,\ystart);
        \draw[#1] ($.5*(\xstart,\ystart)+.5*(\xend,\yend)+4*(\xperp,\yperp)$)--($.5*(\xstart,\ystart)+.5*(\xend,\yend)-4*(\xperp,\yperp)$);
    }}}, nsenwarrows/.default={}
}

\begin{document}
\begin{tikzpicture}
\node at (0,0) (qob) {Quarter of Birth};
\node[right=1cm of qob] (sch) {Schooling};
\node[right=1cm of sch] (wage) {Wage};
\node[below=1cm of sch] (abi) {Ability $\in \:u$};

\draw[nsenwarrows=red] (abi) to (sch);
\draw[->] (abi) to (wage);
\draw[->] (qob) to (sch); %here I want to have "relevance" above the arrow
\draw[->] (sch) to (wage); %here i want a "?" above the arrow
\draw[nsenwarrows] (abi) to node[below left]{exogeneity} (qob); %here I want to replace the arrow with \nsenwarrows and write "exogeneity" below

\end{tikzpicture} 
\end{document}

另外,使用 会更简单tikz-cd。定义一个宏\nsenwarrows,该宏接受一个必需参数(方向)和一个可选参数(标签)。例如,

\nsenwarrows[\textup{\scriptsize exogeneity}]{ul}

将使用给定的标签从当前单元格左上方绘制箭头。

在此处输入图片描述

\documentclass{article}
\usepackage{tikz-cd}

\newcommand{\nsenwarrows}[2][]{\arrow[#2, shift left=2pt, shorten=2mm, "|"marking, "#1", outer sep=1mm]\arrow[#2, shift right=2pt, <-, shorten=2mm, "|"marking]}

\begin{document}
\begin{tikzcd}[column sep=1.5cm, row sep=1cm]
\textup{Quarter of Birth}\arrow[r, "\textup{\scriptsize relevance}"] & \textup{Schooling}\arrow[r, "?"] & \textup{Wage}\\
 & \textup{Ability}\in u\arrow[ur]\arrow[u, red]\nsenwarrows[\textup{\scriptsize exogeneity}]{ul}
\end{tikzcd} 
\end{document}

答案2

符号的名称是\nsenwarrows,可以在fdsymbolMnSymbol包中找到。要将符号放在节点之间,只需加载包含该符号的包,并将其作为节点插入到您想要的节点之间。您可以从以下代码中获得一些初步想法:

\documentclass[12pt]{article}
\usepackage[a4paper, total={180mm,257mm},left=15mm,top=20mm]{geometry}
\usepackage{tikz,fdsymbol,xcolor}
\begin{document}
    \begin{tikzpicture}
        \node at (0,0) {Quarter of Birth};
        \draw[very thick,->] (2,0) -- (4,0) node[above,midway] {\footnotesize relevance};
        \node at (5,0) {Schooling};
        \draw[very thick,->] (6,0) -- (8,0) node[above,midway] {?};
        \node at (9,0) {Wage};
        \node at (3,-0.5) {\Large $\nsenwarrows$};\node at (3,-1) {\footnotesize exogeneity};
        \node at (7,-0.5) {\color{red}\large $\uparrow$}; \node at (7,-1) {Ability $\in u$};
        \draw[very thick,->] (8.1,-0.9) -- (9,-0.3);
    \end{tikzpicture}
\end{document}

相关内容