在 tikz 图形上标记边缘

在 tikz 图形上标记边缘

这是我的代码。我想将 SYMPTOMS ASSOCIATED WITH PH CHANGES ? 和 HYPERSENSIVE ESOPHAGUS 之间的边标记为“+ve”,将 SYMPTOMS ASSOCIATED WITH PH CHANGES ? 和与酸无关的烧心之间的边标记为“-ve”。我该怎么做?

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\tikzset{
  invisible/.style={opacity=0},
  visible on/.style={alt=#1{}{invisible}},
  alt/.code args={<#1>#2#3}{%
    \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
  },
}
\tikzset{
block/.style = {
  rectangle,
  thick,
  text width=6em,
  align=center,
  rounded corners,
  draw=cyan!40!black,
  fill=cyan!20,
  inner ysep=10pt
  }
}
\node [block] at (0,6) (GERD){GERD};
\node [block] at (-3,4) (Esophagitis){Reflux esophagitis};
\node [block] at (0,4) (NERD){NERD};
\node [block] at (3,4) (Barret){Barret's esophagitis};
\node [block] at (0,2) (Ph){Ph testing};
\node [block] at (-3,0) (positive) {Ph +ve NERD};
\node [block] at (3,0) (Negative){Functional heart burn};
\node[] at (3,-2)(Symptoms) {\footnotesize {Symptoms associated with P\textsuperscript{H} changes?}};
\node [block] at (5, -4)(hypersensitive) {hypersensitive esophagus};
\node[block] at (1,-4)(unrelated){Acid unrelated heart burn};
\begin{scope}[cyan!40!black]
\draw[->] (GERD) -- (Esophagitis);
\draw[->] (GERD) -- (NERD);
\draw[->] (GERD) -- (Barret);
\draw[->] (NERD)--(Ph);
\draw[->](Ph)--(positive);
\draw[->](Ph)--(Negative);
\draw[->](Negative)--(Symptoms);
\draw[->](Symptoms)--(unrelated);
\draw[->](Symptoms)--(hypersensitive);
\end{scope} 
\end{tikzpicture}
\end{document}

答案1

对于标记边缘,新的 TikZ 3.0 提供了更短的语法。quotes为此加载库:

\usetikzlibrary{quotes}

现在您可以轻松地像这样标记您的边缘:

\draw[->](Symptoms) edge ["-ve" left] (unrelated);

它还适用于to

\draw[->](Symptoms) to ["+ve" right] (hypersensitive);

机器人线条改成这样,取出了图片的相关部分:

edges with labels

答案2

例如,您可以使用

\draw[->](Symptoms)--node[anchor=east]{-ve}(unrelated);
\draw[->](Symptoms)--node[anchor=west]{+ve}(hypersensitive);

screenshot

anchor命令控制文本的相对位置;您还可以添加键pos=<value>,其中取和<value>之间的任意数字。01

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
        block/.style = {
            rectangle,
            thick,
            text width=6em,
            align=center,
            rounded corners,
            draw=cyan!40!black,
            fill=cyan!20,
            inner ysep=10pt
        }
    ]
    \node [block] at (0,6) (GERD){GERD};
    \node [block] at (-3,4) (Esophagitis){Reflux esophagitis};
    \node [block] at (0,4) (NERD){NERD};
    \node [block] at (3,4) (Barret){Barret's esophagitis};
    \node [block] at (0,2) (Ph){Ph testing};
    \node [block] at (-3,0) (positive) {Ph +ve NERD};
    \node [block] at (3,0) (Negative){Functional heart burn};
    \node[] at (3,-2)(Symptoms) {\footnotesize {Symptoms associated with P\textsuperscript{H} changes?}};
    \node [block] at (5, -4)(hypersensitive) {hypersensitive esophagus};
    \node[block] at (1,-4)(unrelated){Acid unrelated heart burn};
    \begin{scope}[cyan!40!black]
        \draw[->] (GERD) -- (Esophagitis);
        \draw[->] (GERD) -- (NERD);
        \draw[->] (GERD) -- (Barret);
        \draw[->] (NERD)--(Ph);
        \draw[->](Ph)--(positive);
        \draw[->](Ph)--(Negative);
        \draw[->](Negative)--(Symptoms);
        \draw[->](Symptoms)--node[anchor=east]{-ve}(unrelated);
        \draw[->](Symptoms)--node[anchor=west]{+ve}(hypersensitive);
    \end{scope}
\end{tikzpicture}
\end{document}

作为评论,您可以使用positioning库来构建这样的图表,它允许您nodes使用诸如这样的指令来指定位置below=of...,这样就不必使用坐标指定绝对位置(就像您所做的那样);当然,输出最终才是最重要的,而且这不会有太大变化,但很高兴知道还有其他选择:)

相关内容