节点位置 TikZ

节点位置 TikZ

我想制作类似下图的东西,但采用水平格式。如何让文本框在易受影响区域和暴露区域之间等距?TikZ 中节点的相对定位使用坐标。有没有办法相对于对象进行操作?

在此处输入图片描述

   \documentclass{article}

%\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc,
arrows,decorations.pathmorphing,
backgrounds,fit,positioning,shapes.symbols,chains}
\usetikzlibrary{decorations.pathreplacing}


\begin{document}



% Define block styles
\tikzstyle{block} = [rectangle, draw, fill=blue!30,
    text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
%\tikzstyle{curve}=[draw,(latex']


\begin{figure}[!ht]
\centering\makebox[\textwidth]{
\begin{tikzpicture}[node distance =4cm, auto,
comment/.style={
rectangle, 
inner sep= 5pt, 
text width=4cm, }]


    % Place nodes
    \node [block] (susceptible) {Susceptible};
    \node [block, right of=susceptible] (exposed) {Exposed};


\draw [decorate,decoration={brace,amplitude=10pt},xshift=0pt,yshift=0pt]
 (3.8,-2)--(-1,-2)  node [black,midway,yshift=-0.6cm] 
{ \emph{Environmental \& Human factors} 
};


    \node [block, right of=exposed] (infectious) {Infectious};

\draw [decorate,decoration={brace,amplitude=10pt},xshift=0pt,yshift=0pt]
(7.9,-2) -- (4.1,-2)  node [black,midway,yshift=-0.6cm] 
{ \emph{Variable immunity} };




   \node [block, right of=infectious] (removed) {Removed};
\draw [decorate,decoration={brace,amplitude=10pt},xshift=0pt,yshift=0pt]
(12,-2) -- (8.2,-2)  node [black,midway,yshift=-0.6cm] 
{ \emph{Control parameters} };


\draw [decorate,decoration={brace,amplitude=10pt},xshift=0pt,yshift=0pt]
(0.1,1) -- (3.9,1) node [black,midway,yshift=0.6cm] 
{\large  Contact parameters};

\draw [decorate,decoration={brace,amplitude=10pt},xshift=0pt,yshift=0pt]
(4.1,1) -- (12,1) node [black,midway,yshift=0.6cm] 
{\large  Disease parameters};


    \path [line] (susceptible) -- (exposed);
    \path [line] (exposed) -- (infectious);
    \path [line] (infectious) -- (removed);

\path[line,dashed] (removed.south) -- + (0,-.35) -- + (-12,-.35) -- (susceptible.south);


\end{tikzpicture}}
\smallskip
\label{fig:flowchart_disease_transmission}
\caption{Flowchart of fundamental disease transmission mechanisms}
\end{figure}


\end{document}

编辑 看起来像这样,花了我 2 个小时(看起来还不错):

在此处输入图片描述 在 powerpoint 中,花了 5 分钟,但看起来很糟糕!

仍然无法弄清楚如何获取下括号中的逐项列表。

答案1

您几乎已经完成了!我只是想对您的代码提出一些修改建议。

如果可以使用相对坐标,则不要使用固定坐标。例如,你可以说

\path[line,dashed] (removed.south) -- + (0,-.35) -- + (-12,-.35) -- (susceptible.south);

因为您知道removedsusceptible节点之间的距离,但如果您更改,node distance则必须也修改此行(和所有braces)。最好使用

\draw [line,dashed] (removed.south) -- ++ (0,-.35) -| (susceptible.south);

removed.south意思是“从0.35 厘米以下的点画一条虚线,移动到那里并从该点画一个矩形角到susceptible.south”。

对于所有braces你可以使用类似的东西:

\draw [decorate,decoration={brace,amplitude=10pt,raise=3mm}]
    (susceptible.85) -- (exposed.95) 
    node [black,midway,yshift=6mm] {\large  Contact parameters};

raise选项从现在的和brace的参考点向上移动。这意味着节点边界上的角度为 85 度的点。susceptible.85exposed.95

对于块下方的括号,您可以使用mirror选项,而不必更改节点顺序。

\draw [decorate,decoration={brace,amplitude=10pt,raise=6mm, mirror}]
      (susceptible.275)--(exposed.265)  
      node  [black,midway,yshift=-1cm,text width=3cm, align=center,anchor=north] (EHF)
     {\emph{Environmental \\ \& Human factors}};

从上一行,你可以看到我把标题从列表中拆分出来了。这可能不太好,但可以帮助放置它们。

节点中的文本类似于parbox如果您修复一些text width,那么您可以使用itemized列表,或者只是设置一个alignment选项。您将在下一个代码中找到两个示例。

\documentclass{article}    
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc,
arrows,decorations.pathmorphing,
backgrounds,fit,positioning,shapes.symbols,chains}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}

% Define block styles
\tikzstyle{block} = [rectangle, draw, fill=blue!30,
    text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']

\begin{figure}[!ht]
\centering\makebox[\textwidth]{
\begin{tikzpicture}[node distance =4cm, auto,
comment/.style={
rectangle, 
inner sep= 5pt, 
text width=4cm, }]

    % Place nodes
    \node [block] (susceptible) {Susceptible};
    \node [block, right of=susceptible] (exposed) {Exposed};
    \node [block, right of=exposed] (infectious) {Infectious};
    \node [block, right of=infectious] (removed) {Removed};

    \draw [line] (susceptible) -- (exposed);
    \draw [line] (exposed) -- (infectious);
    \draw [line] (infectious) -- (removed);

     \draw [line,dashed] (removed.south) -- ++ (0,-.35) -| (susceptible.south);

\draw [decorate,decoration={brace,amplitude=10pt,raise=3mm}]
(susceptible.85) -- (exposed.95) node [black,midway,yshift=6mm] 
{\large  Contact parameters};

\draw [decorate,decoration={brace,amplitude=10pt,raise=3mm},xshift=0pt,yshift=0pt]
(exposed.85) -- (removed.95) node [black,midway,yshift=6mm] 
{\large  Disease parameters};


\draw [decorate,decoration={brace,amplitude=10pt,raise=6mm, mirror}]
 (susceptible.275)--(exposed.265)  node  [black,midway,yshift=-1cm,text width=3cm, align=center,anchor=north] (EHF)
{ \emph{Environmental \\\& Human factors} 
};

\draw [decorate,decoration={brace,amplitude=10pt,raise=6mm, mirror}]
(exposed.275) -- (infectious.265)  node (VI) [black,midway,yshift=-1.5cm] 
{ \emph{Variable immunity} };

\draw [decorate,decoration={brace,amplitude=10pt,raise=6mm, mirror}]
(infectious.275) -- (removed.265) node (CP) [black,midway,yshift=-1.5cm] 
{ \emph{Control parameters} };

\node [text width=4.5cm, align=left, anchor=north, below=1mm of EHF] {Bioaerosol load\\
Surface pathogen loading\\
Surface contact probability}; 

\node [text width=3.5cm, align=left, anchor=north, below=1mm of CP] {\begin{itemize}
\item Detection
\item Quarantine\end{itemize}}; 

\end{tikzpicture}}
\smallskip
\label{fig:flowchart_disease_transmission}
\caption{Flowchart of fundamental disease transmission mechanisms}
\end{figure}

\end{document}

最终结果是:

在此处输入图片描述

答案2

我使用样式稍微清理了一下您的代码。还使用框的锚点来定位括号,而不是绝对坐标。此外,还引入了底部标签的选项,这允许您在其内容中text width使用:imtemize

\usetikzlibrary{decorations.pathreplacing,positioning,arrows}

% Define some styles
\tikzset{
 block/.style = {
    rectangle, draw, fill=blue!30,
    text width=5em, 
    text centered, 
    rounded corners, 
    minimum height=4em},
 line/.style = {
    draw, -latex'},
 my brace/.style = {
   decorate,decoration={brace,amplitude=10pt},
   shorten >=3pt, shorten <=3pt
 },
 bottom label/.style = {
    black, midway, yshift=-3ex, font=\itshape,
    text width = 3cm, text centered,
 },
 top label/.style = {
    black, midway, above, yshift=3ex, font=\large,
 },
}

\begin{figure}[!ht]
\centering\makebox[\textwidth]{
\begin{tikzpicture}[node distance =4cm, auto]

    % Place nodes
    \node [block]                       (susceptible) {Susceptible};
    \node [block, right of=susceptible] (exposed)     {Exposed};
    \node [block, right of=exposed]     (infectious)  {Infectious};
    \node [block, right of=infectious]  (removed)     {Removed};

\coordinate[below=8mm of susceptible] (bottom brace);

\draw [my brace]
      (exposed.south|-bottom brace) -- (susceptible.south west|-bottom brace) 
      node[bottom label] {
        Environmental \& Human factors
        \begin{itemize}
        \item Foo
        \item Bar
        \item Foobar
        \end{itemize}
      };

\draw [my brace]
    (infectious.south|-bottom brace) -- (exposed.south|-bottom brace)
    node [bottom label] { Variable immunity };

\draw [my brace]
    (removed.south east|-bottom brace) -- (infectious.south|-bottom brace)
    node [bottom label] { 
      Control parameters 
        \begin{itemize}
        \item Foo
        \item Bar
        \end{itemize}
     };    

\coordinate[above=3mm of susceptible] (top brace);

\draw [my brace] 
    (susceptible.north|-top brace) -- (exposed.north|-top brace)
    node [top label] {Contact parameters};

\draw [my brace]
    (exposed.north|-top brace) -- (removed.north|-top brace)
    node[top label] {Disease parameters};

\draw[line] (susceptible) -- (exposed)
            (exposed) -- (infectious)
            (infectious) -- (removed);

\path[line,dashed] 
   (removed.south) -- +(0,-.35) -| (susceptible.south);

\end{tikzpicture}}
\smallskip
\label{fig:flowchart_disease_transmission}
\caption{Flowchart of fundamental disease transmission mechanisms}
\end{figure}

结果

相关内容