Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

When making boxed nodes in TikZ, a common requirement is that the nodes be of equal size. Because TikZ automatically adjusts the size of nodes to fit the contents of the node, this is usually done by setting a minimum size for the nodes, and setting the inner sep to 0.

How can I do the same thing for the parts of multipart nodes? As the following example shows, setting a minimum size for a multipart node only sets the height (as described in the manual). So how do I make each part be the same size as my non-split nodes?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\usepackage[margin=1in]{geometry}
\parindent=0pt
\begin{document}
\tikzset{
every node/.style={draw, minimum size=4ex,inner sep=0pt},
split/.style={rectangle split, rectangle split parts=2,draw,inner sep=0ex,
   rectangle split horizontal,rectangle split part align=base,minimum size=4ex}}
\begin{tikzpicture}
\node at (0,0) {A} ;
\node at (1,0) {a} ; 
\node at (2,0) {X} ;
\node at (3,0) {j} ;
\node at (4,0) {};
\node[split] at (5,0) {}; 
\node[split] at (6,0) {X\nodepart{two}j};
\end{tikzpicture}

\end{document}

output of code

share|improve this question
    
Not quite a duplicate but closely related: tex.stackexchange.com/questions/54252/… –  Caramdir Jul 1 '12 at 17:52
    
A workaround would be something like \node (n1) at (0,0) {X}; \node[right=-0.4pt of n1] (n2) {j}; (where 0.4pt is the line width and text height/depth are set. –  Caramdir Jul 1 '12 at 18:19

2 Answers 2

up vote 7 down vote accepted

I updated my answer with the idea from Caramdir's comment. This method is used in the tutorial "Diagrams as Simple Graphs" of the pgfmanual.

Update With text width=4ex it's better to use Polgab'method : align=center instead of \hfil ...\hfil

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\usepackage[margin=1in]{geometry}
\parindent=0pt
\begin{document}
\tikzset{
every node/.style={draw, minimum size=4ex,inner sep=0pt,textstyle},
split/.style={rectangle split, rectangle split parts=2,draw,inner sep=0ex,
   rectangle split horizontal,minimum size=4ex},
   textstyle/.style={text height=1.5ex,text depth=.25ex}}

\begin{tikzpicture}
\node at (0,0) {A} ;
\node at (1,0) {a} ; 
\node at (2,0) {X} ;
\node at (3,0) {j} ;
\node at (4,0) {};
\node[split,text width=4ex] at (6,0) {\nodepart{two}}; 
\node[split,text width=4ex] at (8,0) {\hfil X\hfil\nodepart{two}\hfil r\hfil};
\end{tikzpicture}  

\end{document}  

enter image description here

share|improve this answer
    
Thanks, the text width solution a much cleaner one altogether. And it's simple to make a macro to wrap the part contents in \hfils. (We can probably delete the clarifying comments.) –  Alan Munn Jul 1 '12 at 17:21
    
There is a downside of removing the align=base part: now if you have e.g. an X and an r in the two parts they are not aligned with the same baseline. –  Alan Munn Jul 1 '12 at 17:30
    
You have the same problem with aand A in the single nodes –  Alain Matthes Jul 1 '12 at 17:34
    
Yes, but in that case it's much less noticeable. :) –  Alan Munn Jul 1 '12 at 17:36
1  
@Caramdir: You are right. But this is not sufficient: part two does not use text height and text depth! We must add rectangle split part align=base. –  Paul Gaborit Jul 1 '12 at 18:11

Here is my solution with rectangle split part align=base (and using chains just to chain the nodes).

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart,chains}

\begin{document}
\begin{tikzpicture}[start chain=going right,node distance=.5ex]
  \tikzset{
    simple node/.style={
      draw,
      text height=2.8ex,text depth=1.2ex,
      inner sep=0pt,text width=4ex,align=center
    },
    split node/.style={
      simple node,
      rectangle split,rectangle split horizontal,rectangle split parts=2,
      draw,inner sep=0ex,rectangle split part align=base,
    },
  }
  \node[on chain,simple node] {j} ;
  \node[on chain,split node] {j\nodepart{two}A};
  \node[on chain,simple node] {A} ; 
  \node[on chain,simple node] {X} ;
  \node[on chain,split node] {X\nodepart{two}j};
  \node[on chain,simple node] {j} ;
  \node[on chain,split node] {.\nodepart{two}\textbullet}; 
\end{tikzpicture}
\end{document}

enter image description here

Below the result without rectangle split part align=base (note the difference between j on left or on right part of slit node):

enter image description here

share|improve this answer
    
Yes you are right with text width=4ex the correct way is to use align=center. I find my problem, with text height=2.8ex,text depth=1.2ex and rectangle split part align=base, there is no problem to align the characters. –  Alain Matthes Jul 2 '12 at 6:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.