tikz 节点图

tikz 节点图

我有一张大图表

\begin{center}
\resizebox{\textwidth}{!}{%
\begin{tikzpicture} [
  line/.style = {draw, thick, -latex},
  dot/.style = {draw, fill=black},
%      scale=0.2
  ]
         \coordinate (0) at (0cm, 0cm);

         \coordinate (1) at (-20cm, 9cm);
         \coordinate (9) at (20cm, 9cm);
  \coordinate (2) at (-40cm, 30cm);
  \coordinate (8) at (40cm, 30cm);
  \coordinate (3) at (-40cm, 50cm);
  \coordinate (7) at (40cm, 50cm);
        \coordinate (4) at (-30cm, 70cm);
        \coordinate (6) at (30cm, 70cm);
        \coordinate (5) at (0cm, 85cm);
        \coordinate (10) at (0cm, 5cm);
         \coordinate (11) at (-15cm, 15cm);
         \coordinate (16) at (15cm, 15cm);
  \coordinate (12) at (-10cm, 22cm);
              \coordinate (15) at (10cm, 22cm);
  \coordinate (13) at (-25cm, 26cm);
        \coordinate (14) at (25cm, 26cm);
  \coordinate (22) at (-30cm, 57cm);
  \coordinate (18) at (30cm, 57cm);
        \coordinate (19) at (0cm, 43cm);
        \coordinate (20) at (0cm, 55cm);
       \coordinate (21) at (-20cm, 45cm);
        \coordinate (17) at (20cm, 45cm);
         \coordinate (23) at (-12cm, 40cm);
  \coordinate (24) at (12cm, 40cm);
  \coordinate (25) at (8cm, 51cm);
        \coordinate (26) at (-30cm, 34cm);
        \coordinate (27) at (30cm, 34cm);

  \draw (0) -- (1);
  \draw (0) -- (9);
  \draw (0) -- (10);
  \draw (0) -- (16);
  \draw (0) -- (11);

  \draw (1) -- (10);
  \draw (1) -- (2);
  \draw (1) -- (11);


  \draw (2) -- (4);
  \draw (2) -- (3);
  \draw (2) -- (22);


  \draw (3) -- (4);
  \draw (3) -- (22);


  \draw (4) -- (5);
  \draw (4) -- (6);
 \draw (4) -- (20);
  \draw (4) -- (22);


  \draw (5) -- (6);
  \draw (5) -- (20);


  \draw (6) -- (20);
    \draw (6) -- (7);
  \draw (6) -- (18);
   \draw (6) -- (8);

    \draw (7) -- (18);
  \draw (7) -- (8);

        \draw (9) -- (8);

        \draw (9) -- (16);
        \draw (9) -- (10);

\draw (16) -- (15);


  \draw (10) -- (11);
    \draw (10) -- (16);
  \draw (11) -- (12);
   \draw (12) -- (13);

  \draw (12) -- (27);
  \draw (12) -- (23);
   \draw (13) -- (27);
  \draw (13) -- (23);


  \draw (14) -- (26);
  \draw (14) -- (24);
   \draw (14) -- (15);
  \draw (15) -- (26);
  \draw (15) -- (24);

   \draw (17) -- (18);
  \draw (17) -- (25);
  \draw (17) -- (27);

  \draw (18) -- (25);
  \draw (18) -- (27);

   \draw (19) -- (20);
  \draw (19) -- (24);
  \draw (19) -- (23);
  \draw (21) -- (25);
  \draw (21) -- (26);

        \draw (22) -- (25);
  \draw (22) -- (26);

  \draw (21) -- (22);
        \draw (27) -- (23);
  \draw (24) -- (26);

  \draw (26) -- (25);
  \draw (27) -- (25);

  \draw (27) -- (13);
  \draw (27) -- (12);


  \draw (9) -- (8);
  \draw (3) -- (2);

  \draw (24) -- (26);
  \draw (27) -- (23);

   \draw (19) -- (20);
  \draw (20) -- (4);
   \draw (20) -- (6);
  \draw (20) -- (5);

        \draw (4) -- (6);

         \draw (25) -- (18);
  \draw (25) -- (17);
   \draw (25) -- (22);
  \draw (25) -- (21);

        \fill [dot] (0) circle (4.0mm);
        \fill [dot] (1) circle (4.0mm);
        \fill [dot] (2) circle (4.0mm);
        \fill [dot] (3) circle (4.0mm);
        \fill [dot] (4) circle (4.0mm);
        \fill [dot] (5) circle (4.0mm);
        \fill [dot] (6) circle (4.0mm);
        \fill [dot] (7) circle (4.0mm);
        \fill [dot] (8) circle (4.0mm);
        \fill [dot] (9) circle (4.0mm);
        \fill [dot] (10) circle (4.0mm);
        \fill [dot] (11) circle (4.0mm);
        \fill [dot] (12) circle (4.0mm);
        \fill [dot] (13) circle (4.0mm);
        \fill [dot] (14) circle (4.0mm);
        \fill [dot] (15) circle (4.0mm);
        \fill [dot] (16) circle (4.0mm);
        \fill [dot] (17) circle (4.0mm);
        \fill [dot] (18) circle (4.0mm);
        \fill [dot] (19) circle (4.0mm);
        \fill [dot] (20) circle (4.0mm);
        \fill [dot] (21) circle (4.0mm);
        \fill [dot] (22) circle (4.0mm);
        \fill [dot] (23) circle (4.0mm);
        \fill [dot] (24) circle (4.0mm);
        \fill [dot] (25) circle (4.0mm);
        \fill [dot] (26) circle (4.0mm);
        \fill [dot] (27) circle (4.0mm);
 \node (a) at (0cm, 0cm) {$u_1$};
 \node (b) at (0cm, 10cm) {$u_2$};

\end{tikzpicture}
}
\end{center}

我尝试使用 \node 来连接坐标,但我犯了一个错误。我可能哪里错了。你能帮我吗?你能帮我把这张图上的任意一条线画成蓝色吗?谢谢。

答案1

您询问如何更改其中一条线的颜色。以下是一种简化的方法:

% arara: pdflatex
% arara: pdflatex
% arara: open
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\def\myunit{*4pt}
\makeatletter
\def\mycolor{}
\def\myextractinfo{\@ifnextchar*{\ae@extract@star}{\ae@extract}}
\def\ae@extract@star*#1;{%%
  \def\mycolor{blue}%%
  \def\mycoor{#1}}
\def\ae@extract#1;{%%
  \def\mycoor{#1}}
\makeatother


\begin{document}

\begin{tikzpicture} [
  line/.style = {draw, thick, -latex},
  dot/.style = {draw, fill=black},
%      scale=0.2
  ]


  \coordinate (0)  at (0cm, 0cm);
  \coordinate (1)  at (-20cm, 9cm);
  \coordinate (2)  at (-40cm, 30cm);
  \coordinate (3)  at (-40cm, 50cm);
  \coordinate (4)  at (-30cm, 70cm);
  \coordinate (5)  at (0cm, 85cm);
  \coordinate (6)  at (30cm, 70cm);
  \coordinate (7)  at (40cm, 50cm);
  \coordinate (8)  at (40cm, 30cm);
  \coordinate (9)  at (20cm, 9cm);
  \coordinate (10) at (0cm, 5cm);
  \coordinate (11) at (-15cm, 15cm);
  \coordinate (12) at (-10cm, 22cm);
  \coordinate (13) at (-25cm, 26cm);
  \coordinate (14) at (25cm, 26cm);
  \coordinate (15) at (10cm, 22cm);
  \coordinate (16) at (15cm, 15cm);
  \coordinate (17) at (20cm, 45cm);
  \coordinate (18) at (30cm, 57cm);
  \coordinate (19) at (0cm, 43cm);
  \coordinate (20) at (0cm, 55cm);
  \coordinate (21) at (-20cm, 45cm);
  \coordinate (22) at (-30cm, 57cm);
  \coordinate (23) at (-12cm, 40cm);
  \coordinate (24) at (12cm, 40cm);
  \coordinate (25) at (8cm, 51cm);
  \coordinate (26) at (-30cm, 34cm);
  \coordinate (27) at (30cm, 34cm);
  \foreach \myc/\myd in 
           {0/{1,10,11,16,9},
            1/{10,11,2},
            2/{22,3,4},
            3/{22,4},
            4/{20,22,5,6},
            5/{20,6},
            6/{18,*20,7,8},
            7/{8,18},
            8/{9},
            9/{10,16},
            10/{11,16},
            11/{12},
            12/{13,23,27},
            13/{23,27},
            14/{15,24,26},
            15/{16,24,26},
            17/{18,25,27},
            18/{25,27},
            19/{20,23,24},
            21/{22,25,26},
            22/{25,26},
            23/{27},
            24/{26},
            25/{26,27}%%
           }
    {
      \fill[dot] (\myc) circle (4.0mm);
      \foreach \myo in \myd
      {
       \expandafter\myextractinfo\myo;
       \draw[\mycolor] (\myc) -- (\mycoor);
      }
    }

 \node (a) at (0cm, 0cm) {$u_1$};
 \node (b) at (0cm, 10cm) {$u_2$};

 \draw[orange] (a) -- (b);
\end{tikzpicture}

\end{document}

在此处输入图片描述

您的节点在那里,但由于比例(通过设置厘米坐标),文本与其他所有内容相比显得很小。

在上面的代码中,我允许你在坐标前加上一个前缀,*然后对其进行解析以设置该特定线的颜色blue。这是一种相当粗糙的方法。

你可以用以下几行来做一些更复杂的事情:

% arara: pdflatex
% arara: pdflatex
% arara: open
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\def\myunit{*4pt}
\makeatletter
\def\myparam{}
\def\myextractinfo{\@ifnextchar[%]
  {\ae@extract@star}{\ae@extract}}
\def\ae@extract@star[#1]#2;{%%
  \def\myparam{[#1]}%%
  \def\mycoor{#2}}
\def\ae@extract#1;{%%
  \def\mycoor{#1}}
\makeatother


\begin{document}

\begin{tikzpicture} [
  line/.style = {draw, thick, -latex},
  dot/.style = {draw, fill=black},
%      scale=0.2
  ]


  \coordinate (0)  at (0\myunit, 0\myunit);
  \coordinate (1)  at (-20\myunit, 9\myunit);
  \coordinate (2)  at (-40\myunit, 30\myunit);
  \coordinate (3)  at (-40\myunit, 50\myunit);
  \coordinate (4)  at (-30\myunit, 70\myunit);
  \coordinate (5)  at (0\myunit, 85\myunit);
  \coordinate (6)  at (30\myunit, 70\myunit);
  \coordinate (7)  at (40\myunit, 50\myunit);
  \coordinate (8)  at (40\myunit, 30\myunit);
  \coordinate (9)  at (20\myunit, 9\myunit);
  \coordinate (10) at (0\myunit, 5\myunit);
  \coordinate (11) at (-15\myunit, 15\myunit);
  \coordinate (12) at (-10\myunit, 22\myunit);
  \coordinate (13) at (-25\myunit, 26\myunit);
  \coordinate (14) at (25\myunit, 26\myunit);
  \coordinate (15) at (10\myunit, 22\myunit);
  \coordinate (16) at (15\myunit, 15\myunit);
  \coordinate (17) at (20\myunit, 45\myunit);
  \coordinate (18) at (30\myunit, 57\myunit);
  \coordinate (19) at (0\myunit, 43\myunit);
  \coordinate (20) at (0\myunit, 55\myunit);
  \coordinate (21) at (-20\myunit, 45\myunit);
  \coordinate (22) at (-30\myunit, 57\myunit);
  \coordinate (23) at (-12\myunit, 40\myunit);
  \coordinate (24) at (12\myunit, 40\myunit);
  \coordinate (25) at (8\myunit, 51\myunit);
  \coordinate (26) at (-30\myunit, 34\myunit);
  \coordinate (27) at (30\myunit, 34\myunit);
  \foreach \myc/\myd in 
           {0/{1,10,11,16,9},
            1/{10,11,2},
            2/{22,3,4},
            3/{22,4},
            4/{20,22,5,6},
            5/{20,6},
            6/{18,[{blue,dashed}]20,7,8},
            7/{8,18},
            8/{9},
            9/{10,16},
            10/{11,16},
            11/{12},
            12/{13,23,27},
            13/{23,27},
            14/{15,24,26},
            15/{16,24,26},
            17/{18,25,27},
            18/{25,27},
            19/{20,23,24},
            21/{22,25,26},
            22/{25,26},
            23/{27},
            24/{26},
            25/{26,27}%%
           }
    {
      \fill[dot] (\myc) circle (4.0pt);
      \foreach \myo in \myd
      {
       \expandafter\myextractinfo\myo;
       \expandafter\draw\myparam (\myc) -- (\mycoor);
      }
    }

 \node (a) at (0\myunit, 0\myunit) {$u_1$};
 \node (b) at (0\myunit, 10\myunit) {$u_2$};

 \draw[orange] (a) -- (b);
\end{tikzpicture}

\end{document}

在此处输入图片描述

这次我允许你在第二个坐标前加上一个[....]块。但是,如果这个块包含逗号,则需要将其内容进一步括起来,以免被误解\foreach

相关内容