有没有更短的代码可以在 tikz 中绘制这个?

有没有更短的代码可以在 tikz 中绘制这个?

是否有可能使用更短的代码来绘制此图,也许使用graphs但它不是正多边形,或者缩短\draw我的代码中的重复:

%% filename henry3.pgf
\begin{tikzpicture}
%%styles and libraries
\usetikzlibrary{decorations.markings, arrows}
\tikzstyle{mdar}=[decoration={markings, mark=at position 0.45 with {\arrow{stealth}}}, postaction={decorate}]
\tikzstyle{mdad}=[decoration={markings, mark=at position 0.3 with {\arrow{stealth}}}, postaction={decorate}]
\tikzstyle{point}=[node distance=2cm, inner sep=0pt, fill=black, circle]
%nodes
\node[point] (p1) {.} ;
\node[point, below of = p1] (p2) {.}; 
\node[point, below of = p2, yshift=0.7cm](p3){.};
\node[point, below of = p3, yshift=0.4cm](p4){.};
\node[point, below of = p4, yshift=0.7cm](p5){.}; 
%%
\node[point, right of=p1, yshift=-1.4cm](p10){.};
\node[point, left of=p1, yshift=-1.4cm](p11){.};
\node[point, right of=p4,  xshift=-0.3cm] (p40) {.};
\node[point,left of=p4, xshift=0.3cm] (p41) {.};
%% there is a way of importing tikz's graph library and making things
%%easier and compressed but importing this does not work on my
 %%computer
%joining nodes
\draw[mdar](p1) -- (p11) ;\draw[mdar](p1) -- (p10);\draw[mdar] (p1) -- (p41);\draw[mdar] (p1) -- (p40);\draw[mdar](p1) -- (p2);
\draw[mdar](p2) -- (p11); \draw[mdar](p2) -- (p3);\draw[mdar](p2) -- (p10); \draw[mdar](p2) -- (p40); \draw[mdar](p2) -- (p41);
\draw[mdad](p11) -- (p3); \draw[mdad](p11) -- (p4); \draw[mdad](p11) -- (p5); \draw[mdar](p11) -- (p41);
\draw[mdar] (p10) -- (p3); \draw[mdad] (p10) -- (p4); \draw[mdad] (p10) -- (p5); \draw[mdar] (p10) -- (p40);
\draw[mdar](p3) -- (p4); \draw[mdar](p3) -- (p41); \draw[mdar](p3) -- (p40);
\draw[mdar](p4) -- (p41); \draw[mdar](p4) -- (p40); \draw[mdar](p4) -- (p5);
\draw[mdar](p40) -- (p5); \draw[mdar](p41) -- (p5);
\end{tikzpicture}

输出: 在此处输入图片描述

答案1

我不太了解图表,所以我不会尝试使用图库,但您可以更有意义地重新编号节点,然后采用典型的邻接矩阵方法。

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings, arrows}
\begin{document}    
\begin{tikzpicture}[mdar/.style={
    decoration={markings, mark=at position 0.45 with {\arrow{stealth}}},
    postaction={decorate}
    },
mdad/.style={decoration={markings, mark=at position 0.3 with {\arrow{stealth}}}, 
             postaction={decorate}},
point/.style={node distance=2cm, inner sep=0pt, fill=black, circle}]

\node[point] (p1) {.} ;
\node[point, left of=p1, yshift=-1.4cm](p2){.};
\node[point, below of = p1] (p3) {.}; 
\node[point, right of=p1, yshift=-1.4cm](p4){.};
\node[point, below of = p3, yshift=0.7cm](p5){.};
\node[point, below of = p5, yshift=0.4cm](p7){.};
\node[point,left of=p7, xshift=0.3cm] (p6) {.};
\node[point, right of=p7,  xshift=-0.3cm] (p8) {.};
\node[point, below of = p7, yshift=0.7cm](p9){.}; 
\def\myadjmat{%
{0,1,1,1,0,1,0,1,0},
{0,0,0,0,1,1,1,0,1},
{0,1,0,1,1,1,0,1,1},
{0,0,0,0,1,0,1,1,1},
{0,0,0,0,0,1,0,1,0},
}

\foreach \x[count=\xi] in \myadjmat{
  \foreach \y[count=\yi] in \x{
    \ifnum1=\y\relax\draw[mdar] (p\xi) -- (p\yi);\fi
  }
}

\end{tikzpicture}
\end{document}

在此处输入图片描述

第五句之后我就觉得无聊了,但你明白我的意思。另外,不要把\usetikzlibrary{...}命令留在画面中。相反,把它们带到你的序言中。最后,你可以考虑跳过\tikzstyle命令,因为它已被弃用应该使用 \tikzset 还是 \tikzstyle 来定义 TikZ 样式?

答案2

这是图形绘制的示例。节点定位并不惊人,但可以相当容易地更改。它可以“照常”使用pdflatex(或xelatex,或latex -> dvipdfm等)来遵守。

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{graphs,decorations.markings}
\tikzgraphsset{on edge arrow/.style={/tikz/every edge/.append style={
  -, draw, postaction={
  decoration={markings, mark=at position \arrowpos with {\arrow{stealth}}},
  decorate}}}, > pos/.store in=\arrowpos, > pos=0.5}
\begin{document}
\begin{tikzpicture}[x=0.75cm,y=0.75cm]
\foreach \p [count=\i] in {(0,0),(-2,-1),(0,-1.75),(2,-1),(0,-3),
    (-1.5,-4.5),(0,-4.5),(1.5,-4.5),(0,-6.25)}
  \node [shape=circle, inner sep=0, minimum size=2,fill] (\i) at \p {};
\graph [use existing nodes, on edge arrow]{
    1 -> {2, 3, 4};
    2 -> {5 -> {6, 7, 8}, 6, 7 -> 9};
    3 -> {2, 4 -> {5, 7, 8}, 5};
    1 ->[> pos=.45] {6, 8};
    3 ->[> pos=.45] {6, 8};
    {2, 4} ->[> pos=0.66] 9;
};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

由于箭头没有规则的图案,因此很难编写真正简洁的代码,因为没有实际的规则来控制箭头应该去往何处等等。所以这个代码比我想要编写的稍长,但它仍然很短。

输出

在此处输入图片描述

代码

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings, arrows}

\tikzset{
    %mdar/.style={very thin,decoration={markings, mark=at position 0.45 with {\arrow{stealth reversed}}}, postaction={decorate}},
    mdad/.style={very thin,decoration={markings, mark=at position 0.45 with {\arrow[scale=.8]{stealth}}}, postaction={decorate}},
    point/.style={inner sep=.5pt, fill=black, circle}
}

\begin{document}
\begin{tikzpicture}

% Nodes
\foreach \points [count=\xi] in {
        (0,0),
        (-1,1),(0,1),(1,1),
        (0,2),
        (-1.2,3.3),(0,3),(1.2,3.3),
        (0,4)}{
    \node[point] (p\xi) at \points {};
}

% Arrows
\foreach \name/\dest in {%  
    2/{1},
    3/{1,2,4},
    4/{1},
    5/{2,3,4},
    6/{1,2,3,5},
    7/{2,4,5,6,8},
    8/{1,3,4,5},
    9/{2,4,6,7,8},
    }{%
    \foreach \ppp in \dest{
        \draw[very thin, mdad] (p\name) -- (p\ppp);
    }
}
\end{tikzpicture}
\end{document}

相关内容