# How to find the ratio of a length command (e.g., \textwidth) to a reference value (e.g., 6cm) ?

How to find the ratio of a length command (e.g., \textwidth) to a reference value (e.g., 6cm) ? Actually I want to use the ratio for the \scalebox argument, e.g., \scalebox{<ratio>}.

\documentclass{article}
\usepackage[showframe=true]{geometry}
\usepackage{pst-node}
\usepackage{graphicx}

\usepackage{fp}
\usepackage{pgf}
\pgfmathsetmacro{\ratio}{\the\textwidth/(6cm)}

\makeatletter
\FPdiv\thecm{6}{2.54} %72.27
\FPmul\thecminpoints{\thecm}{72.27}
\FPdiv\thescale{\strip@pt\textwidth}{\thecminpoints}%
\makeatother

\begin{document}
\noindent%
%\scalebox{\thescale}{%
\scalebox{\ratio}{%
\begin{pspicture}(6,4)
\pnode(2,2){A}\psframe(A)
\pnode(4,2){B}\pscircle(B){2}
\end{pspicture}}
\uput[-135](A){$A$}
\rput(B){$B$}
\end{document}


-
@Matthew, thanks for editing. :) –  xport Dec 2 '10 at 16:01
Could you just change \begin{pspicture}(6,4) to \begin{pspicture}(\textwidth) ? –  Will Robertson Dec 2 '10 at 16:57
@Will, you try it first :) –  xport Dec 3 '10 at 3:29
Oops; I did try it but I got a bit confused with what I was seeing with the output. Sorry for the noise. –  Will Robertson Dec 6 '10 at 1:29
No problem Will, it is fun! –  xport Dec 6 '10 at 2:14

TeX can do 64-bit integer mul-divs; the resulting integer can be converted to a decimal number via the sp-to-pt conversion:

\documentclass{minimal}
\begin{document}
\makeatletter
\strip@pt\dimexpr\number\numexpr\number\textwidth*65536/\number\dimexpr6cm\relax\relax sp\relax
\end{document}


Or, more general:

\documentclass{minimal}
\makeatletter
\newcommand*{\DivideLengths}[2]{%
\strip@pt\dimexpr\number\numexpr\number\dimexpr#1\relax*65536/\number\dimexpr#2\relax\relax sp\relax
}
\makeatother
\begin{document}
\DivideLengths{\textwidth - 0.2\textheight}{2cm + 4cm}
\end{document}

-
Thank Philipp, your code scares me. I am not familiar with plain tex. I don't know how to plug it into my problem. –  xport Dec 2 '10 at 16:53
The \DivideLengths macro from the second snipped simply expands to a decimal representation of the ratio. –  Philipp Dec 2 '10 at 18:13
Good solution. –  Yiannis Lazarides Dec 2 '10 at 20:38
thanks. Could you help me to describe how to parse your macro step by step? I don't know how this macro works. What is the \number, \relax, etc for? –  xport Dec 3 '10 at 0:58

You can use \the\textwidth to give you the value of the dimension in points and you can use that to calculate the width in cm (in inch (1 in is 72.27 pt)).

You can automate all these with LaTeX calculations depending for what you need the calculations.

Edit

I would use the fp package for the calculations as follows:

\makeatletter
\FPdiv\thecm{6}{2.54} %72.27
\FPmul\thecminpoints{\thecm}{72.27}
\FPdiv\thescale{\strip@pt\textwidth}{\thecminpoints}%
\thescale
\makeatother

\scalebox{\thescale}{Test}

-
Thank Yiannis. Please see my first edit. :) –  xport Dec 2 '10 at 15:49
@xport There might be shorter and more elegant ways to do it, but I would do it this way. Search for LaTeX3 calculations for other ways on this site. There was also question on strip@pt that explains how this strips the point. –  Yiannis Lazarides Dec 2 '10 at 16:12
Thank again Yiannis. Well done. But \thescale before \makeatother must be removed. –  xport Dec 2 '10 at 16:22
@xport Sure, I put it there so one can see the result of the calculation, you can just comment it out. –  Yiannis Lazarides Dec 2 '10 at 20:31

TeX doesn't have a data type for decimal numbers and can't natively divide dimensions (for the mathematicians: TeX's dimensions form a one-dimensional vector space over the field of rational numbers, but not a field by themselves). You can use pgfmath to do it:

\documentclass{article}
\usepackage{pgf}
\begin{document}

\pgfmathsetmacro{\ratio}{\the\textwidth/(6cm)}
\scalebox{\ratio}{\fbox{stuff}}

\end{document}


But the confused questions and presumptive answers are saying that there might be a better way to get what you really want.

-
thanks. Your solution works. –  xport Dec 2 '10 at 16:41
In fact, TeX dimensions are always integers, which can be multiplied and divided without any external package. –  Philipp Dec 2 '10 at 16:44
@Phillipp: I'm confused. What is the smallest dimension then? –  Matthew Leingang Dec 2 '10 at 16:52
@Philipp: You are right! TeXbook, Chapter 10, p. 58 "TeX represents all dimensions as an integer multiple of the tiny units called sp" which is 1/65536 of a point. But you have still have to extract this multiple if you want to divide them, right? –  Matthew Leingang Dec 2 '10 at 17:16
TeX does the division by 65536 when it has to print the decimal representation of a dimension. So generally you can use integer arithmetic, multiply by 65536 (before division to avoid rounding), have TeX treat the result as a dimension, convert that to the result string, and strip the unit suffix. –  Philipp Dec 2 '10 at 18:16
\resizebox{0.5\textwidth}{!}{...}

@xport Given your latest edit what's wrong with replacing the \scalebox line by \resizebox{\textwidth}{!}{ ? –  Will Robertson Dec 2 '10 at 16:50