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 using labelled nodes (e.g. for a flow chart), their sizes are automatically chosen. One can enforce the same size, which must be known a priori, for multiple nodes by using "large enough" values for minimum height and minimum width.

In the following example the node width is restricted for spacial reasons. This leads to the boxes containing different numbers of lines and therefore different box sizes.

    \documentclass{beamer}
    \usepackage{tikz}
    \usetikzlibrary{positioning}

    \begin{document}
    \begin{frame}
    \begin{tikzpicture}[node distance=2cm]
        \node[rectangle,text width=5em,draw=black] (leftnode) {Short text};
        \node[rectangle,text width=5em,draw=black,right=of leftnode] (rightnode) {A text longer than the other one};
    \end{tikzpicture}
    \end{frame}
    \end{document}

Given two strings, that shall be written in two separate nodes of equal size: Is it possible to let LaTeX calculate the minimal node height that is large enough for both cases and use it as a parameter for the nodes?

share|improve this question
add comment

2 Answers 2

up vote 10 down vote accepted

Identical Heights:

When you specify the text width, the text is placed in a \parbox of the width provided by text width. So, a simple fix is to use a \vphantom{} in the shorter node to ensure that it is of the same height:

enter image description here

Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\newcommand*{\TextWidth}{5em}%
\newcommand*{\StringA}{Short}%
\newcommand*{\StringB}{A text longer than the other one}%

\newcommand{\VPhantom}{\vphantom{\parbox{\TextWidth}{\StringB}}}%

\begin{document}
\begin{tikzpicture}[node distance=0.1cm]
    \node[rectangle,text width=\TextWidth,draw=black, red] (leftnode) {\VPhantom\StringA};
    \node[rectangle,text width=\TextWidth,draw=black, blue, right=of leftnode] (rightnode) {\StringB};
\end{tikzpicture}
\end{document}

Identical Widths

You can use the \widthof{} to compute the length and then chose the maximum width as the minimum length:

enter image description here

Note

  • The 2*\InnerSep adjusts for the value of inner sep that is on both sides of the node, so if that value is changed this formula will need to be adjusted as well.

Code:

\documentclass{article}
\usepackage{tikz}

\newcommand*{\InnerSep}{0.333em}%
\newcommand*{\StringA}{short}%
\newcommand*{\StringB}{a longer string}%

\begin{document}

\pgfmathsetmacro{\MinimumWidth}{%
    max(\widthof{\StringA},\widthof{\StringB})+2*\InnerSep%
}%

\begin{tikzpicture}[minimum width=\MinimumWidth]
    \node [rectangle, draw=red]  at (0,0) {\StringA};
    \node [rectangle, draw=blue] at (0,1) {\StringB};
\end{tikzpicture}
\end{document}
share|improve this answer
    
Turns out, I have not been specific enough. From the idea this is exactly, what I am looking for, but I want to control the height as well. Using \heightof alone does not do the trick, because the macro may not contain `\`. Any ideas? –  Tim Sep 12 '12 at 19:30
    
@Tim: I am not sure what you mean by "may not contain a `\`. Best if you update the question and compose a fully compilable MWE that illustrates the exact problem. –  Peter Grill Sep 12 '12 at 19:46
    
What I meant was newline `\`, which gives an error when it is used inside your example. I changed the question now. –  Tim Sep 12 '12 at 20:37
    
Have updated the solution to provide the same height nodes. –  Peter Grill Sep 12 '12 at 21:36
    
Thank you very much! –  Tim Sep 12 '12 at 22:47
add comment

The answer above works well if the smaller string only uses one line. For multiple lines in both nodes I came to the following solution (combining both approaches given above and using the calc package).

    \documentclass{beamer}
    \usepackage{calc}
    \usepackage{tikz}
    \usetikzlibrary{positioning}

    \newcommand*{\TextWidth}{5em}
    \newcommand*{\Innersep}{0.33em}
    \newcommand{\StringA}{Short multiline text}
    \newcommand{\StringB}{A text longer than the other one}
    \newlength\myheight
    \setlength{\myheight}{\totalheightof{\parbox{\TextWidth}{\StringB}}+Innersep*2}

    \begin{document}
    \begin{frame}
    \begin{tikzpicture}[node distance=2cm]
        \node[rectangle,text width=\TextWidth,inner sep=\Innersep,minimum height=\myheight,draw=black] (leftnode) {\StringA};
        \node[rectangle,text width=\TextWidth,inner sep=\Innersep,minimum height=\myheight,draw=black,right=of leftnode] (rightnode) {\StringB};
    \end{tikzpicture}     
    \end{frame}     
    \end{document}
share|improve this answer
add comment

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.