14

Is there some intelligent way to have several tikz nodes have the same height and width? Consider the following example with output below.

I'd like every node in each row to have the same height equal to the height of the tallest node and every node in each column to have the same width equal to the width of the widest node.

In the example below each node in the third row has a different height. In the first two rows the heights are forced to be equal using \vphantom. There must be a more intelligent way to do this. Any ideas? What about the columns (they contain identical nodes, but they would not normally)?

I don't want to hard-code a minimum width+height.

Thank you!

J.

\documentclass{minimal}

\usepackage[svgnames]{xcolor}
\usepackage{tikz}


\begin{document}
\begin{tikzpicture}
[every node/.style={anchor=base}]
\matrix [draw=red]
{
\node[fill=Orange] {a\vphantom{Xg}}; & \node[fill=Green] {X\vphantom{ag}}; & \node[fill=LightGreen] {\vphantom{aX}g}; \\
\node[fill=Yellow] {a\vphantom{Xg}}; & \node[fill=Cyan] {X\vphantom{ag}}; & \node[fill=Magenta] {\vphantom{aX}g}; \\
\node[fill=Purple] {a}; & \node[fill=LightBlue] {X}; & \node[fill=Aquamarine] {g}; \\
};
\end{tikzpicture}
\end{document}

Output

1

2 Answers 2

20

Using text depth and text height, you can control the total height for the nodes; text width gives you control over the width (there's also a minimum size key which could be used):

\documentclass{article}    
\usepackage[svgnames]{xcolor}
\usepackage{tikz}


\begin{document}
\begin{tikzpicture}[every node/.style={anchor=base,text depth=.5ex,text height=2ex,text width=1em}]
\matrix [draw=red]
{
\node[fill=Orange] {a}; & \node[fill=Green] {X}; & \node[fill=LightGreen] {g}; \\
\node[fill=Yellow] {a}; & \node[fill=Cyan] {X}; & \node[fill=Magenta] {g}; \\
\node[fill=Purple] {a}; & \node[fill=LightBlue] {X}; & \node[fill=Aquamarine] {g}; \\
};
\end{tikzpicture}
\end{document}

enter image description here

As percusse mentions in his comment, a matrix of nodes (requires the matrix library) could be more useful here; it simplifies the code and also gives you a nodes in empty cells key to deal with the case of cells without explicit text:

\documentclass{article}    
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}[every node/.style={anchor=base,text depth=.5ex,text height=2ex,text width=1em}]
\matrix [matrix of nodes,draw=red,nodes in empty cells]
{
|[fill=Orange]| a & |[fill=Green]| X & |[fill=LightGreen]| \\
|[fill=Yellow]| a & |[fill=Cyan]| & |[fill=Magenta]| g \\
|[fill=Purple]| & |[fill=LightBlue]| X & |[fill=Aquamarine]| g \\
};
\end{tikzpicture}
\end{document}

enter image description here

10
  • 1
    I think this begs for matrix of nodes for reduced input
    – percusse
    Jul 12, 2014 at 22:25
  • @percusse You're right. I added a remark to my answer. Jul 12, 2014 at 22:28
  • Thank you, but (as mentioned in the question) I don't want to hard-code the width and height. I want it to be automatically determined and set equal to the maximum sizes in each row and column. (And the nodes need not contain text.)
    – JPi
    Jul 12, 2014 at 22:31
  • 1
    @JPi For nodes without text, there's a nodes in empty cells key in a matrix of nodes. For the other part, this could perhaps be done, but I'm not sure it it's worth the effort. Jul 12, 2014 at 22:35
  • It is worth the effort for me (but thanks!). (I meant it could contain an object other than text.)
    – JPi
    Jul 12, 2014 at 22:35
5

This does what I want, but it's admittedly not pretty. It's motivated by Dependent node size in TikZ .

I'm puzzled why using

column 1/.style={Minimum Width=c1} 

etcetera doesn't work the same way as the row style commands. Thanks all.

\documentclass[12pt]{article}

\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{positioning,node-families,matrix}

\begin{document}



\begin{tikzpicture}[every node/.style={anchor=base}, 
row 1/.style={Minimum Height=r1,Text Height=th1,Text Depth=td1},
row 2/.style={Minimum Height=r2,Text Height=th2,Text Depth=td2},
row 3/.style={Minimum Height=r3,Text Height=th3,Text Depth=td3}
]
\matrix [matrix of nodes,draw=Red,nodes in empty cells]
{
|[fill=Orange,Minimum Width=col1]| a & |[fill=Green,Minimum Width=col2]| X & |[fill=LightGreen,Minimum Width=col3]| \\
|[fill=Yellow,Minimum Width=col1]| a & |[fill=Cyan,Minimum Width=col2]| & |[fill=Magenta,Minimum Width=col3]| g \\
|[fill=Purple,Minimum Width=col1]| & |[fill=LightBlue,Minimum Width=col2]| X & |[fill=Aquamarine,Minimum Width=col3]| \tikz\draw[fill=Black]  (0,0) circle(1cm); \\
};
\end{tikzpicture}

\end{document}
1
  • 1
    I'm puzzled, too, but you can do column 1/.style={nodes={Minimum Width=col1}} and so on. Jun 3, 2015 at 19:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .