Tikz 過程/函數像抽象嗎?

Tikz 過程/函數像抽象嗎?

我很新,tikz但是,所以我確實了解基礎知識,但是,我真的很困惑創建像構造這樣的過程的最佳方法是什麼:似乎有很多機制,我無法找出哪一個是最好的。

這是我畫的範例圖。我可以找出 LaTeX 來讓程式碼更加簡潔和可維護,但我不確定哪個是最好的構造。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\begin{document}

\tikzset{%
    body/.style={inner sep=0pt,outer sep=0pt,shape=rectangle,draw,ultra thick,minimum height=0.5cm,anchor=south west},
    dimen/.style={<->,>=latex,thin,
    every rectangle node/.style={fill=white,midway}},
    symmetry/.style={dashed,thin},
}

\begin{tikzpicture}[array/.style={
rectangle split, 
rectangle split horizontal,
rectangle split parts=#1, 
thick,draw, 
anchor=center,
}]]

\node [array=10, align=center,,minimum width=\textwidth] at (0,0) (pool) {
  \nodepart{one} \rotatebox{-90}{\begin{tabular}{ll}\textbf{\texttt{struct}}\\\texttt{Cons}\end{tabular}}
  \nodepart{two} \rotatebox{-90}{\begin{tabular}{ll}\textbf{\texttt{struct}}\\\texttt{Cons}\end{tabular}}
  \nodepart{three} $\cdots\cdots\cdots$
  \nodepart{four} \rotatebox{-90}{\begin{tabular}{ll}\textbf{\texttt{struct}}\\\texttt{Cons}\end{tabular}}
  \nodepart{five} $\cdots\cdots\cdots$
  \nodepart{six} \rotatebox{-90}{\begin{tabular}{ll}\textbf{\texttt{struct}}\\\texttt{Cons}\end{tabular}}
  \nodepart{seven} $\cdots\cdots\cdots$
  \nodepart{eight} \rotatebox{-90}{\begin{tabular}{ll}\textbf{\texttt{struct}}\\\texttt{Cons}\end{tabular}}
  \nodepart{nine} $\cdots\cdots\cdots$
  \nodepart{ten} \rotatebox{-90}{\begin{tabular}{ll}\textbf{\texttt{struct}}\\\texttt{Cons}\end{tabular}}
};
 \draw (pool.south west) -- ++(0,-1.5) coordinate (A1) -- ++(0,-5pt);
 \draw (pool.south east) -- ++(0,-1.5) coordinate (A2) -- ++(0,-5pt);
 \draw [dimen] (A1) -- (A2) node {$2^{\texttt{LG2\_POOLSIZE}} = 2^{14} = 16,384$};
\node (last) at (0,-1.5) {{\Large \emph{Pool of ``\textup{\texttt{\textbf{struct} Cons}}'' Records}}};

\node [body,minimum width=1cm] (last) at (-6,3.5) { \texttt{car} };
 \draw (last.north west) -- ++(0,0.3) coordinate (A1) -- ++(0,5pt);
 \draw (last.north east) -- ++(0,0.3) coordinate (A2) -- ++(0,5pt);
 \draw [dimen] (A1) -- (A2) node {14b};
\draw[->] (last.south) --  (pool.one north);
\node [body,minimum width=1cm] (last) at (-5,2) { \texttt{cdr} };
 \draw (last.north west) -- ++(0,0.3) coordinate (A1) -- ++(0,5pt);
 \draw (last.north east) -- ++(0,0.3) coordinate (A2) -- ++(0,5pt);
 \draw [dimen] (A1) -- (A2) node {14b};
\draw[->] (last.south) --  (pool.two north);
\node [body,minimum width=1cm] (last) at (-4,3.4) { \texttt{cdr} };
 \draw (last.north west) -- ++(0,0.3) coordinate (A1) -- ++(0,5pt);
 \draw (last.north east) -- ++(0,0.3) coordinate (A2) -- ++(0,5pt);
 \draw [dimen] (A1) -- (A2) node {14b};
\draw[latex->] (last.south) --  (pool.four north);
\node [body,minimum width=1cm] (last) at (-2.5,3) { \texttt{cdr} };
 \draw (last.north west) -- ++(0,0.3) coordinate (A1) -- ++(0,5pt);
 \draw (last.north east) -- ++(0,0.3) coordinate (A2) -- ++(0,5pt);
 \draw [dimen] (A1) -- (A2) node {14b};
\draw[->] (last.south) --  (pool.six north);
\node [body,minimum width=1cm] (last) at (0,2.5) { \texttt{car} };
 \draw (last.north west) -- ++(0,0.3) coordinate (A1) -- ++(0,5pt);
 \draw (last.north east) -- ++(0,0.3) coordinate (A2) -- ++(0,5pt);
 \draw [dimen] (A1) -- (A2) node {14b};
\draw[->] (last.south) --  (pool.eight north);
\node [body,minimum width=1cm] (last) at (2,4) { \texttt{cdr} };
 \draw (last.north west) -- ++(0,0.3) coordinate (A1) -- ++(0,5pt);
 \draw (last.north east) -- ++(0,0.3) coordinate (A2) -- ++(0,5pt);
 \draw [dimen] (A1) -- (A2) node {14b};
\draw[->] (last.south) --  (pool.ten north);
\node [body,minimum width=1cm] (last) at (5,2.5) { \texttt{car} };
 \draw (last.north west) -- ++(0,0.3) coordinate (A1) -- ++(0,5pt);
 \draw (last.north east) -- ++(0,0.3) coordinate (A2) -- ++(0,5pt);
 \draw [dimen] (A1) -- (A2) node {14b};
\draw[->] (last.south) --  (pool.eight north);

\end{tikzpicture}
\end{document}

輸出已附上;是的,我知道這不太漂亮,但這就是我現階段能做的。

那麼我的問題是:您會推薦哪一種抽象結構來簡化上述程式碼?更一般地說,tikz/LaTeX 工具箱中有什麼以及何時應該使用這些工具?

上述程式的輸出

答案1

我認為你的問題太模糊(或寬泛),但我想到的可能性是

  • 使用scope環境

  • 使用預先定義或自訂的樣式

\node [body,minimum width=1cm]你的例子,你可以寫七次而不是寫

\begin{tikzpicture}
% …
\begin{scope}[every node/.style={body,minimum width=1cm}]% scope and pre-defined style
\node (last) at (-6,3.5) { \texttt{car} };
  \draw (last.north west) -- ++(0,0.3) coordinate (A1) -- ++(0,5pt);
  \draw (last.north east) -- ++(0,0.3) coordinate (A2) -- ++(0,5pt);
  \draw [dimen] (A1) -- (A2) node {14b};
  \draw[->] (last.south) --  (pool.one north);
\node (last) at (-5,2) { \texttt{cdr} };
  \draw (last.north west) -- ++(0,0.3) coordinate (A1) -- ++(0,5pt);
  \draw (last.north east) -- ++(0,0.3) coordinate (A2) -- ++(0,5pt);
  \draw [dimen] (A1) -- (A2) node {14b};
  \draw[->] (last.south) --  (pool.two north);
\node (last) at (-4,3.4) { \texttt{cdr} };
  \draw (last.north west) -- ++(0,0.3) coordinate (A1) -- ++(0,5pt);
  \draw (last.north east) -- ++(0,0.3) coordinate (A2) -- ++(0,5pt);
  \draw [dimen] (A1) -- (A2) node {14b};
  \draw[latex->] (last.south) --  (pool.four north);
\node (last) at (-2.5,3) { \texttt{cdr} };
  \draw (last.north west) -- ++(0,0.3) coordinate (A1) -- ++(0,5pt);
  \draw (last.north east) -- ++(0,0.3) coordinate (A2) -- ++(0,5pt);
  \draw [dimen] (A1) -- (A2) node {14b};
  \draw[->] (last.south) --  (pool.six north);
\node (last) at (0,2.5) { \texttt{car} };
  \draw (last.north west) -- ++(0,0.3) coordinate (A1) -- ++(0,5pt);
  \draw (last.north east) -- ++(0,0.3) coordinate (A2) -- ++(0,5pt);
  \draw [dimen] (A1) -- (A2) node {14b};
  \draw[->] (last.south) --  (pool.eight north);
\node (last) at (2,4) { \texttt{cdr} };
  \draw (last.north west) -- ++(0,0.3) coordinate (A1) -- ++(0,5pt);
  \draw (last.north east) -- ++(0,0.3) coordinate (A2) -- ++(0,5pt);
  \draw [dimen] (A1) -- (A2) node {14b};
  \draw[->] (last.south) --  (pool.ten north);
\node (last) at (5,2.5) { \texttt{car} };
  \draw (last.north west) -- ++(0,0.3) coordinate (A1) -- ++(0,5pt);
  \draw (last.north east) -- ++(0,0.3) coordinate (A2) -- ++(0,5pt);
  \draw [dimen] (A1) -- (A2) node {14b};
  \draw[->] (last.south) --  (pool.eight north);
\end{scope}
\end{tikzpicture}

或者您可以使用以下方式定義樣式\tikzset

\tikzset{bodynode/.style={body,minimum width=1cm}}% self-defined style

由於您已經使用了\tikzset,因此您可以簡單地將其添加到其他定義中。

\begin{tikzpicture}
% …
\node[bodynode] (last) at (-6,3.5) { \texttt{car} };
% …
\node[bodynode] (last) at (-5,2) { \texttt{cdr} };
% …
\node[bodynode] (last) at (-4,3.4) { \texttt{cdr} };
% …
\node[bodynode] (last) at (-2.5,3) { \texttt{cdr} };
% …
\node[bodynode] (last) at (0,2.5) { \texttt{car} };
% …
\node[bodynode] (last) at (2,4) { \texttt{cdr} };
% …
\node[bodynode] (last) at (5,2.5) { \texttt{car} };
% …
\end{tikzpicture}

透過將它們全部組合起來,您還可以節省重複bodynode添加:

\tikzset{bodynode/.style={body,minimum width=1cm}}% self-defined style
\begin{tikzpicture}
% …
\begin{scope}[every node/.style=bodynode]% scope and pre-defined style calling
                                         % self-defined style
\node (last) at (-6,3.5) { \texttt{car} };
% …
\node (last) at (-5,2) { \texttt{cdr} };
% …
\node (last) at (-4,3.4) { \texttt{cdr} };
% …
\node (last) at (-2.5,3) { \texttt{cdr} };
% …
\node (last) at (0,2.5) { \texttt{car} };
% …
\node (last) at (2,4) { \texttt{cdr} };
% …
\node (last) at (5,2.5) { \texttt{car} };
% …
\end{scope}
\end{tikzpicture}

相關內容