關於鏈條的一些問題

關於鏈條的一些問題

我正在嘗試製作下圖:

\begin{tikzpicture}[
  start chain=1 going right,node distance=-0.15mm]
\node [on chain=1] at (-1.5,-.4) {f(};  

 \node [draw,on chain=1] {*};  
 \node [draw,on chain=1] {*};
 \node [draw,on chain=1] {*};
 \node [draw,on chain=1] {0};
 \node [draw,on chain=1] {0};
 \node [draw,on chain=1] {*};
 \node [draw,on chain=1] {0};
 \node [draw,on chain=1] {1};
 \node [draw,on chain=1] {*};
 \node [draw,on chain=1] {*};
 \node [draw,on chain=1] {0};

 \node [name=r,on chain=1] {$) = 0$}; 
\end{tikzpicture}

我遇到的問題是 * 周圍的框比數字 1,0 周圍的框稍大。如何確保所有盒子的尺寸都相同?

另外我希望變數 x 出現在鏈的中心上方,最簡單的方法是什麼?我現在知道的唯一方法是節點 at (x,y) 指令。我認為應該有一種方法可以命名鏈中間的節點,並定義一個直接位於其上方的節點,但我不知道其語法。

答案1

您可以設定minimum size節點的選項。如果將其設為足夠大的值,則所有框都將具有相同的大小。

要放置x在鏈的中心上方,有多種選擇。您可以使用下列語法命名節點

\node (<name>) [<options>] {<node label>};

(這與 相同\node [name=<name>] {}。)然後您可以將其用作<name>坐標,並相對於該坐標放置另一個節點,使用

\node [above=of <name>] {$x$};

另一種方法是命名鏈中的第一個和最後一個節點,並將新節點放置在它們之間,例如

\path (startnode) -- node[above] {$x$} (endnode);

為了簡化生成圖表,您可以聲明自己的樣式

<style name>/.style={<options>}

如果您想要全域定義,請將其新增至 , 的可選參數tikzpicture或巨集中。\tikzset這樣您就可以快速變更具有該樣式的所有節點的大小、顏色等。在下面的程式碼中我定義了一個box on chain樣式。

在此輸入影像描述

\documentclass[border=2mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{chains}
\begin{document}

\begin{tikzpicture}[
  start chain=1 going right,node distance=-0.15mm,
  box on chain/.style={draw, on chain=1,minimum size=.5cm}]
\node [on chain=1] at (-1.5,-.4) {f(};  

 \node(start) [box on chain] {*};  
 \node [box on chain] {*};
 \node [box on chain] {*};
 \node [box on chain] {0};
 \node [box on chain] {0};
 \node (mid) [box on chain] {*};
 \node [box on chain] {0};
 \node [box on chain] {1};
 \node [box on chain] {*};
 \node [box on chain] {*};
 \node(end) [box on chain] {0};

 \node [name=r,on chain=1] {$) = 0$}; 

 % place new node above node called mid
 \node [above=of mid] {\( x \)};

% different method: place node in the middle between start and end, 2ex above
%  \path (start) -- node[above=2ex]{\( x \)} (end);
\end{tikzpicture}

\end{document}

相關內容