4

I'm trying to make the following diagram:

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

The problem I am running into is that the boxes around the *'s are slightly bigger than around the numbers 1,0. How can I insure that all the boxes are the same size?

Also I'd like the variable x to appear above the center of the chain, what is the easiest way to do this? The only way I know right now is node at (x,y) command. I think there should be a way to name a node in the middle of the chain, and define a node to go directly above it, but I don't know the syntax for this.

Mensch
  • 65,388
guest123
  • 103

1 Answers1

1

You can set the minimum size option for the nodes. If this is set to a large enough value, all boxes will be of equal size.

To place x above the center of the chain, there are several options. You could name a node with the syntax

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

(This does the same as \node [name=<name>] {}.) You can then use <name> as a coordinate, and place another node relative to that, with

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

Another way would be to name the first and last node in the chain, and place the new node midway between them, with e.g.

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

To simplify generating diagrams, you can declare your own styles with

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

Add this to the optional argument of the tikzpicture, or in a \tikzset macro if you would like a global definition. This way you could quickly change the size, colour etc. of all the nodes with that style. In the below code I defined a box on chain style.

enter image description here

\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}
Torbjørn T.
  • 206,688