# Auto-resize text in Node

I am trying to auto-resize some text that appears in a few nodes. Basically if the text is too large (overfull-hbox, etc) then set the font size smaller and continue. Does anyone know how to do this?

Why automatic and not fixed manually? The idea is that this document will be populated automatically and have latex run on it and the resulting documents are displayed. There would be a substantial number of these so it is unfeasable to fix it by hand.

-
–  Martin Scharrer Apr 13 '11 at 10:45
ipavlic answered it but the problem with that is the node hasn't been typeset yet, and I didn't know about boxes. Thanks though –  Trevor A. Apr 15 '11 at 0:29

A sketch of one possible approach is presented below:

\documentclass[12pt]{article}

\usepackage{tikz}
%scalebox needs graphicx
\usepackage{graphicx}
%settototalheight uses calc
\usepackage{calc}
%define some lengths
\newlength\nodeheight
\setlength{\nodeheight}{1cm}

\newlength\nodewidth
\setlength{\nodewidth}{3cm}

\newlength\myboxheight

\begin{document}
%create a box which will contain text you want to put in
\newsavebox{\mybox}
\sbox{\mybox}{\parbox{\nodewidth}{Ceterum censeo Carthaginem esse delendam}}
%measure its height
\settototalheight\myboxheight{\mybox}
%calculate the ratio of box’s height and available space in node
\pgfmathparse{\myboxheight/\nodeheight}

\centering
\begin{tikzpicture}
%resize your box to fit inside node; keep width-to-height ratio using !, and resize height as necessary
\node [rectangle, minimum width=\nodewidth, minimum height=\nodeheight, draw=black] {\resizebox{!}{\pgfmathresult\nodeheight}{\usebox{\mybox}}};
\end{tikzpicture}

\bigskip

%here is the original box, which is too big:
\usebox{\mybox}
\end{document}


## The result:

It is far from ideal -- note that text could be bigger if I were to appropriately resize just the text and not the box containing it.

-
As a side note, tikz nodes automatically expand to accommodate text, do they not? –  ipavlic Apr 13 '11 at 10:10
Thanks, I should be able to get this to work. The nodes would resize like you said (or that is my understanding as well, if they do not have a fixed size). The size of the nodes is important and must be preserved. Thanks Again. –  Trevor A. Apr 13 '11 at 13:26