# Set picture size to size of a given box

I would like to use the picture mode in latex, but need to set the size of the drawing region to the size of some other box. I would normally do

\begin{picture}(max_hor, max_ver)(ll_hor, ll_ver)
...
\end{picture}


and I want max_hor, max_ver to be computed from the dimensions of a given box.

-

## migrated from stackoverflow.comFeb 7 '13 at 16:03

This question came from our site for professional and enthusiast programmers.

You can access the height, depth and width of a (La)TeX box register using \ht\mybox, \dp\mybox and \dp\mybox, respectively, where \mybox is a box defined using \newsavebox{\mybox}. If you need these dimension as text you can convert them by placing a \the in front of it.

However, with the picture environment you need to state the dimensions as multiple of the \unitlength, so you need to divide this values by that amount, which isn't that easy because TeX doesn't provide a direct way to divide a length by a length. But you could use the internal scaling macro used by graphicx:

\documentclass{article}

\newsavebox{\mybox}
\usepackage{graphicx}
\usepackage{calc}

\makeatletter
\newcommand{\scaletoul}[2]{%
\setlength\@tempdima{#2}%
\Gscale@div{#1}{\@tempdima}{\unitlength}%
}
\makeatother

\begin{document}

\sbox{\mybox}{come content}

\setlength{\unitlength}{1pt}

\scaletoul{\mywidth}{\wd\mybox}
\scaletoul{\mytotalheight}{\ht\mybox+\dp\mybox}
\frame{% just to see the dimensions
\begin{picture}(\mywidth, \mytotalheight)(0,0)
\put (0,0) {\raisebox{\dp\mybox}{\usebox{\mybox}}}
\end{picture}%
}

\end{document}


Please not that the picture environment can be considered outdated. For new diagrams using either TikZ or PSTricks is recommended. There you don't have to define the size yourself and also can provide all dimensions directly without having them to scale to \unitlength. See e.g. Drawing on an image with TikZ for an example.

-
Don't tell David Carlisle that picture is outdated. ;-) The picture package allows for explicit dimensions in the argument of macros in the picture environment. –  egreg Feb 7 '13 at 18:43

The picture package allows for using explicit dimensions in the arguments of picture related macros. Here's an example, using the same code as in Martin's answer, but vastly simplified, as you can see:

\documentclass{article}
\usepackage{picture}

\newsavebox{\mybox}

\begin{document}

\sbox{\mybox}{some content}

\frame{% just to see the dimensions
\begin{picture}(\wd\mybox,\dimexpr\ht\mybox+\dp\mybox\relax)(0,0)
\put (0,0) {\raisebox{\dp\mybox}{\usebox{\mybox}}}
\end{picture}%
}

\end{document}

-