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]あなたの例では、7回書く代わりに、

\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}

関連情報