With the help of the \catcode trick given by
Stephan_K and the fp package, it is possible to define macros to convert from one unit to another:
Code: Select all
\documentclass[a4paper]{article}
\usepackage{fp}
\newlength{\TOarg} \newlength{\TOunit}
{\catcode`p=12 \catcode`t=12 \gdef\TOnum#1pt{#1}}
\newcommand\TOop[2]{\setlength{\TOarg}{#2}%
\FPdiv\TOres{\expandafter\TOnum\the\TOarg}{\expandafter\TOnum\the\TOunit}%
\FPround\TOres\TOres{#1}}
\newcommand{\TOspace}{\ }
\newcommand\TOpt[2][2]{\setlength{\TOunit}{1pt}\TOop{#1}{#2}\TOres\TOspace pt}
\newcommand\TOin[2][2]{\setlength{\TOunit}{1in}\TOop{#1}{#2}\TOres\TOspace in}
\newcommand\TOcm[2][2]{\setlength{\TOunit}{1cm}\TOop{#1}{#2}\TOres\TOspace cm}
\newcommand\TOmm[2][2]{\setlength{\TOunit}{1mm}\TOop{#1}{#2}\TOres\TOspace mm}
\newcommand\TOem[2][2]{\setlength{\TOunit}{1em}\TOop{#1}{#2}\TOres\TOspace em}
\begin{document}
The width of this document is \TOpt[0]{\textwidth}, that is, \TOcm{\textwidth}, whereas the height is \TOcm[3]{\textheight}, i.e. \TOin{\textheight}. Here we have some equivalences between different units:
\begin{center}
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{r*{4}{@{\ =\ }r}}
1pt & \TOpt[0]{1pt} & \TOin[3]{1pt} & \TOcm{1pt} & \TOmm{1pt} \\
1in & \TOpt{1in} & \TOin[0]{1in} & \TOcm{1in} & \TOmm[1]{1in} \\
1cm & \TOpt{1cm} & \TOin[3]{1cm} & \TOcm[0]{1cm} & \TOmm[0]{1cm} \\
1mm & \TOpt{1mm} & \TOin[3]{1mm} & \TOcm{1mm} & \TOmm[0]{1mm}
\end{tabular}
\end{center}
The em unit depends on which font is active:
\begin{itemize}
\item 1 cm = \TOem{1cm}, 1 em = \TOcm{1em}
\item {\bfseries 1 cm = \TOem{1cm}, 1 em = \TOcm{1em}}
\item {\bfseries \large 1 cm = \TOem{1cm}, 1 em = \TOcm{1em}}
\item {\ttfamily 1 cm = \TOem{1cm}, 1 em = \TOcm{1em}}
\end{itemize}
\end{document}
The optional argument of TOpt, TOcm and like fixes the number of decimal digits shown (2 by default). These macros first convert their mandatory argument to pt (done automatically by TeX) and then the conversion to the corresponding unit.
From the above code, it is easy to add macros for conversion to pc, ex, bp and so on.