summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/printlen/printlen.sty
blob: 7156670a20f1e82b52c6a1f3dbc7033d9e2f6437 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
% 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
%