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}

相关内容