代码缩短

代码缩短

我创建了一条路径图,并试图将伽马标签放入线内,但收效甚微。

到目前为止,我最多取得了以下成就:

\documentclass[12pt]{report}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,arrows} 
\begin{document}
\begin{tikzpicture}[auto,node distance=.5cm,
latent/.style={circle,draw,very thick,inner sep=0pt,minimum size=30mm,align=center},
manifest/.style={rectangle,draw,very thick,inner sep=0pt,minimum width=45mm,minimum height=10mm},
paths/.style={->, >=stealth'},
]

\node [manifest] (B1) at (0,0) {Business Licencing};
\node [manifest] (C1) [below=of B1] {Courts};
\node [manifest] (C2) [below=of C1] {Crime};
\node [manifest] (C3) [below=of C2] {Customs Regulation};
\node [manifest] (E1) [below=of C3] {Electricity};
\node [manifest] (F1) [below=of E1] {Finance};
\node [manifest] (I1) [below=of F1] {Informal Economy};
\node [manifest] (L1) [below=of I1] {Labour Education};
\node [manifest] (L2) [below=of L1] {Labour Regulation};
\node [manifest] (L3) [below=of L2] {Land};
\node [manifest] (P1) [below=of L3] {Political Instability};
\node [manifest] (T1) [below=of P1] {Tax Administration};
\node [manifest] (T2) [below=of T1] {Tax Rates};
\node [manifest] (T3) [below=of T2] {Transportation};

\node [latent] (l) [left=7.5cm of L1] {$\tilde{\theta_i}$};


\draw[paths] (l.east) -- node {$\gamma_1$} (B1.west);
\draw[paths] (l.east) -- node {$\gamma_2$} (C1.west);
\draw[paths] (l.east) -- node {$\gamma_3$} (C2.west);
\draw[paths] (l.east) -- node {$\gamma_4$} (C3.west);
\draw[paths] (l.east) -- node {$\gamma_5$} (E1.west);
\draw[paths] (l.east) -- node {$\gamma_6$} (F1.west);
\draw[paths] (l.east) -- node {$\gamma_7$} (I1.west);
\draw[paths] (l.east) -- node {$\gamma_8$} (L1.west);
\draw[paths] (l.east) -- node {$\gamma_9$} (L2.west);
\draw[paths] (l.east) -- node {$\gamma_{10}$} (L3.west);
\draw[paths] (l.east) -- node {$\gamma_{11}$} (P1.west);
\draw[paths] (l.east) -- node {$\gamma_{12}$} (T1.west);
\draw[paths] (l.east) -- node {$\gamma_{13}$} (T2.west);
\draw[paths] (l.east) -- node {$\gamma_{14}$} (T3.west);

\end{tikzpicture}
\end{document}

这是相应的输出:

在此处输入图片描述

有人能告诉我如何将这些伽马标签放置在每个路径箭头的中线吗?

答案1

我认为,如果线条在圆形节点的中心相交,而不是在l.east圆上的点相交,看起来会更友好。在下面的例子中,线条从 开始l.center,但裁剪阻止它们显示在圆内:

\documentclass[12pt]{report}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,arrows}
\begin{document}
\begin{tikzpicture}[
  auto,
  node distance=.5cm,
  latent/.style={
    circle,
    draw,
    very thick,
    inner sep=0pt,
    minimum size=30mm,
    align=center,
  },
  manifest/.style={
    rectangle,
    draw,
    very thick,
    inner sep=0pt,
    minimum width=45mm,
    minimum height=10mm,
  },
  paths/.style={
    ->,
    >=stealth',
  },
]

\node [manifest] (B1) at (0,0) {Business Licencing};
\node [manifest] (C1) [below=of B1] {Courts};
\node [manifest] (C2) [below=of C1] {Crime};
\node [manifest] (C3) [below=of C2] {Customs Regulation};
\node [manifest] (E1) [below=of C3] {Electricity};
\node [manifest] (F1) [below=of E1] {Finance};
\node [manifest] (I1) [below=of F1] {Informal Economy};
\node [manifest] (L1) [below=of I1] {Labour Education};
\node [manifest] (L2) [below=of L1] {Labour Regulation};
\node [manifest] (L3) [below=of L2] {Land};
\node [manifest] (P1) [below=of L3] {Political Instability};
\node [manifest] (T1) [below=of P1] {Tax Administration};
\node [manifest] (T2) [below=of T1] {Tax Rates};
\node [manifest] (T3) [below=of T2] {Transportation};

\node [latent] (l) [left=7.5cm of L1] {$\tilde{\theta_i}$};

\begin{scope}
  \clip
    (current bounding box.south west) rectangle
    (current bounding box.north east)
    (l) circle[radius=15mm]
  ;
  \def\x#1#2{
    \path
      (l.center)
      -- node[
        anchor=center,
        circle,
        pos=.55,
      ] (tmp) {$\gamma_{#1}$}
      (#2.west)
    ;
    \draw[paths] (l) -- (tmp) -- (#2.west);
  }
  \x{1}{B1}
  \x{2}{C1}
  \x{3}{C2}
  \x{4}{C3}
  \x{5}{E1}
  \x{6}{F1}
  \x{7}{I1}
  \x{8}{L1}
  \x{9}{L2}
  \x{10}{L3}
  \x{11}{P1}
  \x{12}{T1}
  \x{13}{T2}
  \x{14}{T3}
\end{scope}
\end{tikzpicture}
\end{document}

结果

答案2

您可以auto在设置中看到tikzpicture。该设置的目的是将节点放置在线的旁边,而不是线上。因此,通过删除它并添加every node/.style={fill=white}样式paths,您可以得到以下效果:

在此处输入图片描述

\documentclass[12pt,border=5mm]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,arrows} 
\begin{document}
\begin{tikzpicture}[
  node distance=.5cm,
  latent/.style={circle,draw,very thick,inner sep=0pt,minimum size=30mm,align=center},
  manifest/.style={rectangle,draw,very thick,inner sep=0pt,minimum width=45mm,minimum height=10mm},
  paths/.style={->, >=stealth', every node/.style={fill=white}},
]

\node [manifest] (B1) at (0,0) {Business Licencing};
\node [manifest] (C1) [below=of B1] {Courts};
\node [manifest] (C2) [below=of C1] {Crime};
\node [manifest] (C3) [below=of C2] {Customs Regulation};
\node [manifest] (E1) [below=of C3] {Electricity};
\node [manifest] (F1) [below=of E1] {Finance};
\node [manifest] (I1) [below=of F1] {Informal Economy};
\node [manifest] (L1) [below=of I1] {Labour Education};
\node [manifest] (L2) [below=of L1] {Labour Regulation};
\node [manifest] (L3) [below=of L2] {Land};
\node [manifest] (P1) [below=of L3] {Political Instability};
\node [manifest] (T1) [below=of P1] {Tax Administration};
\node [manifest] (T2) [below=of T1] {Tax Rates};
\node [manifest] (T3) [below=of T2] {Transportation};

\node [latent] (l) [left=7.5cm of L1] {$\tilde{\theta_i}$};


\draw[paths] (l.east) -- node {$\gamma_1$} (B1.west);
\draw[paths] (l.east) -- node {$\gamma_2$} (C1.west);
\draw[paths] (l.east) -- node {$\gamma_3$} (C2.west);
\draw[paths] (l.east) -- node {$\gamma_4$} (C3.west);
\draw[paths] (l.east) -- node {$\gamma_5$} (E1.west);
\draw[paths] (l.east) -- node {$\gamma_6$} (F1.west);
\draw[paths] (l.east) -- node {$\gamma_7$} (I1.west);
\draw[paths] (l.east) -- node {$\gamma_8$} (L1.west);
\draw[paths] (l.east) -- node {$\gamma_9$} (L2.west);
\draw[paths] (l.east) -- node {$\gamma_{10}$} (L3.west);
\draw[paths] (l.east) -- node {$\gamma_{11}$} (P1.west);
\draw[paths] (l.east) -- node {$\gamma_{12}$} (T1.west);
\draw[paths] (l.east) -- node {$\gamma_{13}$} (T2.west);
\draw[paths] (l.east) -- node {$\gamma_{14}$} (T3.west);

\end{tikzpicture}
\end{document}

代码缩短

这可能很有趣,也可能没意思,但使用 a 和 a 循环可以使您的代码更加紧凑chain。这里我还稍微缩小了尺寸,以便它实际上可以放在一页上report

\documentclass[12pt,border=5mm]{report}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,arrows,chains} 
\begin{document}
\begin{tikzpicture}[
  latent/.style={circle,draw,very thick,inner sep=0pt,minimum size=30mm,align=center},
  manifest/.style={rectangle,draw,very thick,inner sep=0pt,minimum width=45mm,minimum height=8mm,font=\small},
  paths/.style={>=stealth'},
]

\begin{scope}[
   start chain=list going below,
   every node/.style={manifest,on chain},
   node distance=4mm,
   local bounding box=list
]
  \node {Business Licencing};
  \node {Courts};
  \node {Crime};
  \node {Customs Regulation};
  \node {Electricity};
  \node {Finance};
  \node {Informal Economy};
  \node {Labour Education};
  \node {Labour Regulation};
  \node {Land};
  \node {Political Instability};
  \node {Tax Administration};
  \node {Tax Rates};
  \node {Transportation};
\end{scope}

\node [latent] (l) [left=5cm of list] {$\tilde{\theta_i}$};

\foreach \i in {1,...,14}
   \draw [paths] (l.east) --node[font=\scriptsize,fill=white,inner sep=2pt] {$\gamma_{\i}$} (list-\i.west);

\end{tikzpicture}
\end{document}

答案3

您可以设置anchor=center并使用midwaypos=0.5来定位。要填充背景,请使用fill=color。可以使用选项 来设置在标签周围添加的空间量inner sep

  • 标签锚点:anchor=center
  • 标签定位:midwaypos=0.5pos=0.1, ...
  • 白色背景:fill=white

在此处输入图片描述

梅威瑟:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows}
\begin{document}

\begin{tikzpicture}[auto,node distance=.5cm,
latent/.style={circle,draw,very thick,inner sep=0.5pt,minimum size=30mm,align=center},
manifest/.style={rectangle,draw,very thick,inner sep=0.5pt,minimum width=45mm,minimum height=10mm},
paths/.style={->, >=stealth'},
]

\node [manifest] (B1) at (0,0) {Business Licencing};
\node [manifest] (C1) [below=of B1] {Courts};
\node [manifest] (C2) [below=of C1] {Crime};
\node [manifest] (C3) [below=of C2] {Customs Regulation};
\node [manifest] (E1) [below=of C3] {Electricity};
\node [manifest] (F1) [below=of E1] {Finance};
\node [manifest] (I1) [below=of F1] {Informal Economy};
\node [manifest] (L1) [below=of I1] {Labour Education};
\node [manifest] (L2) [below=of L1] {Labour Regulation};
\node [manifest] (L3) [below=of L2] {Land};
\node [manifest] (P1) [below=of L3] {Political Instability};
\node [manifest] (T1) [below=of P1] {Tax Administration};
\node [manifest] (T2) [below=of T1] {Tax Rates};
\node [manifest] (T3) [below=of T2] {Transportation};

\node [latent] (l) [left=7.5cm of L1,inner sep=0.5pt] {$\tilde{\theta_i}$};


\draw[paths] (l.east) -- node[midway,anchor=center,fill=white,inner sep=0.5pt] {$\gamma_1$} (B1.west);
\draw[paths] (l.east) -- node[midway,anchor=center,fill=white,inner sep=0.5pt] {$\gamma_2$} (C1.west);
\draw[paths] (l.east) -- node[midway,anchor=center,fill=white,inner sep=0.5pt] {$\gamma_3$} (C2.west);
\draw[paths] (l.east) -- node[midway,anchor=center,fill=white,inner sep=0.5pt] {$\gamma_4$} (C3.west);
\draw[paths] (l.east) -- node[midway,anchor=center,fill=white,inner sep=0.5pt] {$\gamma_5$} (E1.west);
\draw[paths] (l.east) -- node[midway,anchor=center,fill=white,inner sep=0.5pt] {$\gamma_6$} (F1.west);
\draw[paths] (l.east) -- node[midway,anchor=center,fill=white,,,inner sep=0.5pt] {$\gamma_7$} (I1.west);
\draw[paths] (l.east) -- node[midway,anchor=center,fill=white,inner sep=0.5pt] {$\gamma_8$} (L1.west);
\draw[paths] (l.east) -- node[pos=0.1,anchor=center,fill=white,inner sep=0.5pt] {$\gamma_9$} (L2.west);
\draw[paths] (l.east) -- node[pos=1,anchor=center,fill=white,inner sep=0.5pt] {$\gamma_{10}$} (L3.west);
\draw[paths] (l.east) -- node[pos=0.75,anchor=center,fill=white,inner sep=0.5pt] {$\gamma_{11}$} (P1.west);
\draw[paths] (l.east) -- node[pos=0.5,anchor=center,fill=white,inner sep=0.5pt] {$\gamma_{12}$} (T1.west);
\draw[paths] (l.east) -- node[pos=0.25,anchor=center,fill=white,inner sep=0.5pt] {$\gamma_{13}$} (T2.west);
\draw[paths] (l.east) -- node[pos=0,anchor=center,fill=white,inner sep=0.5pt] {$\gamma_{14}$} (T3.west);

\end{tikzpicture}

\end{document}

答案4

使用库chainsquotes,并使用命名链并使用此名称进行自动节点命名,例如A-1A-2等,您可以编写图表代码如下:

\documentclass[12pt,border=5mm]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, chains, positioning, quotes}

\begin{document}
\begin{tikzpicture}[
  node distance = 3mm and 77mm,
    start chain = A going below,
  latent/.style = {circle, draw, very thick, minimum size=30mm},
manifest/.style = {rectangle, draw, very thick,
                   minimum width=45mm, minimum height=10mm,
                   on chain},
every edge quotes/.style={fill=white, inner sep=2pt, font=\footnotesize},
                   ]
    \begin{scope}[every node/.style={manifest}]
\node  {Business Licencing};        % A-1
\node  {Courts};
\node  {Crime};
\node  {Customs Regulation};
\node  {Electricity};               % A-5
\node  {Finance};
\node  {Informal Economy};
\node  {Labour Education};
\node  {Labour Regulation};
\node  {Land};                      % A-10
\node  {Political Instability};
\node  {Tax Administration};
\node  {Tax Rates};
\node  {Transportation};            % A-14            
    \end{scope}
\node (L) [latent, left=of A-7] {$\tilde{\theta_i}$};
%
\foreach \i in {1,...,14}
    \draw[-Stealth] (L) edge ["$\gamma_{\i}$"] (A-\i.west);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容