使用 pgfmathparse+matrix 繪製圖形

使用 pgfmathparse+matrix 繪製圖形

我想畫一個圖表。我用tikz矩陣繪製頂點,然後想從數組添加邊。我嘗試了以下程式碼:

\documentclass{article}
\usepackage{tikz,pgfmath}
\usetikzlibrary{matrix}
\begin{document}
  \begin{tikzpicture}
    \matrix (w) [nodes={circle,draw,scale=0.66}, matrix of nodes, row sep=2em, column sep=0.5em]{    
      1&2&3\\
      &4&5\\
      &6\\
    };
    \def\V{{"w-1-1","w-1-2","w-1-3","w-2-2","w-2-3","w-3-2"}}
    %\draw[->] (\V[1]) to (\V[2]); %Line A
    %\node at (1,1) {\pgfmathparse{\V[2]}\pgfmathresult}; %Line B
    \draw[->] (\pgfmathparse{\V[1]}\pgfmathresult) to (\pgfmathparse{\V[2]}\pgfmathresult); %Line C
  \end{tikzpicture}
\end{document}

我試過A線首先,它不起作用。事實證明,為了正確存取數組,我必須使用pdf數學解析。但程式碼無法編譯(儘管B線工作正常)。 (看起來輸出pgf數學結果是一個字串,但它不是期望。

PS:最初的動機是為了簡單地說明圖形的鵝卵石形狀。無論如何,我已經附上了最終的程式碼以及結果。

\documentclass{article}
\usepackage{tikz,pgfmath,xstring,algorithm}
\usetikzlibrary{matrix}

\newcommand{\Configuration}[6]{%A fixed graph
  \begin{minipage}{.2\textwidth}%1
    \begin{tikzpicture}
      \matrix (w) [nodes={circle,draw,scale=0.6}, matrix of nodes, row sep=2em, column sep=0.5em,ampersand replacement=\&]{   
    1\&2\&3\\
    \&4\&5\\
    \&6\\
      };
      \def\V{{"w-1-1","w-1-2","w-1-3","w-2-2","w-2-3","w-3-2"}}%the mapping
      \foreach \x/\y/\z in {{1/4/#1},{2/4/#2},{3/4/#3},{3/5/#4},{4/6/#5},{5/6/#6}}%pebbling
    \pgfmathsetmacro{\X}{\V[\x-1]}
    \pgfmathsetmacro{\Y}{\V[\y-1]}
    \draw[->] (\X) to node {\ifthenelse{\z=0}{}{$\bullet$}} (\Y);
    \end{tikzpicture}
  \end{minipage}
}

\begin{document}
  \begin{figure}
    \centering
    \Configuration{0}{0}{0}{0}{0}{0}
    \Configuration{1}{0}{0}{0}{0}{0}
    \Configuration{1}{1}{0}{0}{0}{0}
    \Configuration{1}{1}{1}{0}{0}{0}
    \Configuration{1}{1}{1}{0}{1}{0}
    \Configuration{1}{1}{0}{0}{1}{0}
    \Configuration{1}{0}{0}{0}{1}{0}
    \Configuration{0}{0}{0}{0}{1}{0}
    \Configuration{0}{0}{0}{1}{1}{0}
    \Configuration{0}{0}{0}{1}{1}{1}
    \Configuration{0}{0}{0}{0}{1}{1}
  \end{figure}
\end{document}

在此輸入影像描述

答案1

不確定你想要什麼,但我認為這\pgfmathsetmacro可以解決你的問題:

\documentclass [border=2mm]{standalone}
\usepackage{tikz}
\usepackage{tikz,pgfmath}
\usetikzlibrary{matrix}

\begin{document}
  \begin{tikzpicture}
    \matrix (w) [nodes={circle,draw,scale=0.66}, matrix of nodes, row sep=2em, column sep=0.5em]{    
      1&2&3\\
      &4&5\\
      &6\\
    };
    \def\V{{"w-1-1","w-1-2","w-1-3","w-2-2","w-2-3","w-3-2"}}
    \pgfmathsetmacro{\start}{\V[1]}
    \pgfmathsetmacro{\finish}{\V[2]}
    \draw[->] (\start) to (\finish); %Line C
  \end{tikzpicture}
\end{document}

結果

相關內容