% printlen.sty  Print lengths in a variety of units
%
% Author: Peter Wilson, Herries Press
% Maintainer: Will Robertson (will dot robertson at latex-project dot org)
% Copyright 2001 Peter R. Wilson
% Released under the LaTeX Project Public License 
%
% Extensions courtesy of Harald Harders (h.harders@tu-bs.de)
%
% Usage instructions are at the end of this file.
% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{printlen}[2009/09/03 v1.1a print lengths with units]
%
% \uselengthunit{<unit>} sets \l@nunits to the value of <unit>
% and \l@nunitperpt to the number of <unit> in 1pt.
\newcommand{\uselengthunit}[1]{%
  \def\l@nunitperpt{1.0}\def\l@nunits{pt}%
  \def\l@nta{#1}\def\l@ntb{pt}%
  \ifx \l@nta\l@ntb
    \def\l@nunitperpt{1.0}\def\l@nunits{pt}%
  \else
    \def\l@ntb{pc}%
    \ifx \l@nta\l@ntb
      \def\l@nunitperpt{0.083333}\def\l@nunits{pc}%
    \else
      \def\l@ntb{in}%
      \ifx \l@nta\l@ntb
        \def\l@nunitperpt{0.013837}\def\l@nunits{in}%
      \else
        \def\l@ntb{mm}%
        \ifx \l@nta\l@ntb
          \def\l@nunitperpt{0.351459}\def\l@nunits{mm}%
        \else
          \def\l@ntb{cm}%
          \ifx \l@nta\l@ntb
            \def\l@nunitperpt{0.0351459}\def\l@nunits{cm}%
          \else
            \def\l@ntb{bp}%
            \ifx \l@nta\l@ntb
              \def\l@nunitperpt{0.996264}\def\l@nunits{bp}%
            \else
              \def\l@ntb{dd}%
              \ifx \l@nta\l@ntb
                \def\l@nunitperpt{0.9345718}\def\l@nunits{dd}%
              \else
                \def\l@ntb{cc}%
                \ifx \l@nta\l@ntb
                  \def\l@nunitperpt{0.0778809}\def\l@nunits{cc}%
                \else
                  \def\l@ntb{PT}%
                  \ifx \l@nta\l@ntb
                    \def\l@nunitperpt{1.0}\def\l@nunits{PT}%
                  \fi
                \fi
              \fi
            \fi
          \fi
        \fi
      \fi
    \fi
  \fi
}
\uselengthunit{pt}

% \printlength{<length>} prints the value of <length> in the units set 
% by \uselengthunit.
\newcommand{\printlength}[1]{%
  \def\l@nta{pt}\ifx\l@nta\l@nunits\the#1\else
    \def\l@nta{PT}%
    \@tempdimc=\l@nunitperpt #1\relax\strip@pt\@tempdimc 
    \unitspace\relax\ifmmode
      \mathrm{\ifx\l@nta\l@nunits pt\else\l@nunits\fi}%
    \else
      \ifx\l@nta\l@nunits pt\else\l@nunits\fi
    \fi\fi}

% \rndprintlength{<length>} prints the rounded value of <length> in
% the units set by \uselengthunit. Contributed by Harald Harders.
\def\@round#1.#2\@empty{#1}%
\newcommand{\rndprintlength}[1]{%
  \def\l@nta{pt}\ifx\l@nta\l@nunits\the#1\else
    \def\l@nta{PT}%
    \setlength{\@tempdimc}{\l@nunitperpt #1}%
      \addtolength{\@tempdimc}{0.5pt}%
    \edef\@@round{\strip@pt\@tempdimc}%
    \expandafter\@round\@@round.\@empty
    \unitspace\relax\ifmmode
      \mathrm{\ifx\l@nta\l@nunits pt\else\l@nunits\fi}%
    \else
      \ifx\l@nta\l@nunits pt\else\l@nunits\fi
    \fi\fi}

% Small space. Contributed by Harald Harders.
\newcommand{\unitspace}{\,}

\endinput

% USAGE:
%
%    \printlength{<length>} prints the value of a LaTeX length in the 
% units specified by \uselengthunit{<unit>}, where <unit> may be any TeX
% length unit except for scaled point. That is, <unit> may be any of:
% pt, pc, in, mm, cm, bp, dd or cc. When pt is set the printed length
% value will include any stretch or shrink values, otherwise these
% are not printed. The <unit> argument may also be PT, in which case
% length values will be printed in pt units but without any stretch
% or shrink values. An unknown value for <unit> is treated as though it
% had been specified as pt.
%    The unit is separated from the number using the command
% \unitspace which is set to \, by default. In math mode the units are
% printed upright.
%    \rndprintlength{<length>} prints the rounded value of a LaTeX length.
% Use PT instead of pt for rounded points if there are stretch or
% shrink values.
%
%    The initial setting is \uselengthunit{pt}
%
% Example:
%
% The \verb|\textwidth| is \printlength{\textwidth} which is also
% \uselengthunit{in}\printlength{\textwidth} and
% \uselengthunit{mm}\printlength{\textwidth}.
%
%
% CHANGE HISTORY
%
% Version 1.1a (2009/09/03)
% - New maintainer (Will Robertson)
%
% Version 1.1 (2001/12/09)
% - Print rounded values
% - Space between value and units
%
% Version 1.0 (2001/11/03)
% - First public release
%