2
\documentclass{article}
\begin{document}
\newdimen\mylength
 \mylength=1in
 \the\mylength
 \end{document}

Output is: 72.27 pt

But i want to show output in centimeter using VTeX.

  • Is this meant to be the new question I suggested? If so, it's not VTeX that's important: it's not using e-TeX. Moreover, did you see my suggestion about using printlen in the comment I left on your other question? – Joseph Wright Apr 20 '15 at 18:17
  • 3
    is there any particular reason to be using vtex rather than a more standard tex distribution such as miktex or texlive? – David Carlisle Apr 20 '15 at 20:53
3

Using macros by Claudio Beccari in widetable.sty:

\documentclass{article}

\makeatletter
\dimendef\wt@Numer=2
\dimendef\wt@Denom=4
\countdef\wt@Num=2
\countdef\wt@Den=4
\countdef\wt@I=6
\def\wt@segno{}

\def\dividi#1\per#2\in#3{%
  \begingroup
  \wt@Numer #1\relax \wt@Denom #2\relax
  \ifdim\wt@Denom<\z@ \wt@Denom -\wt@Denom \wt@Numer -\wt@Numer\fi
  \ifdim\wt@Numer<\z@ \def\wt@segno{-}\wt@Numer -\wt@Numer\fi
  \ifdim\wt@Denom=\z@
    \edef\wt@Q{\ifdim\wt@Numer<\z@-\fi\strip@pt\maxdimen}%
  \else
    \wt@Num=\wt@Numer \wt@Den=\wt@Denom \divide\wt@Num\wt@Den
    \edef\wt@Q{\number\wt@Num.}%
    \advance\wt@Numer -\wt@Q\wt@Denom \wt@I=6
    \@whilenum \wt@I>\z@ \do{\wt@dividiDec\advance\wt@I\m@ne}%
  \fi
  \edef\x{\noexpand\endgroup\noexpand\def\noexpand#3{\wt@segno\wt@Q}}
  \x
}

\def\wt@dividiDec{%
  \wt@Numer=10\wt@Numer \wt@Num=\wt@Numer \divide\wt@Num\wt@Den
  \edef\wt@q{\number\wt@Num}\edef\wt@Q{\wt@Q\wt@q}%
  \advance\wt@Numer -\wt@q\wt@Denom}

\makeatletter

\newcommand{\convert}[2]{\dividi#1\per 1#2\in\temp\temp\,#2}

\begin{document}

\convert{1in}{pt}

\convert{1in}{cm}

\newlength\mylength
\setlength\mylength{.3in}

\convert{\mylength}{cm}

\end{document}

The accuracy is not guaranteed beyond three digits, but it should be sufficient.

enter image description here

6

If I understand your question, you need to do unit conversion in a obscure TeX system where eTeX primitives are not available. First of all, ask yourself if this is a good idea. The most of current LaTeX packages need eTeX primitives.

If you really needs to convert TeX units without eTeX then you must reconcile to the precision loss because the 64bit integer arithmetic isn't available. And you can use, for example, the following macro \printin with the usage:

\printin{unit}\register

for example:

\printin{cm}\hsize  

prints \hsize register in cm. The \printin macro can be implemented by:

{\lccode`\?=`\p \lccode`\!=`\t  \lowercase{\gdef\ignorept#1?!{#1}}}
\newdimen\tmpdim
\newcount\tmpnum

\def\printin#1#2{%
    \tmpnum=1073741824 \tmpdim=1#1
    \divide\tmpdim by4
    \divide\tmpnum by\tmpdim
    \tmpdim=\the\tmpnum sp
    \tmpdim=\expandafter\ignorept\the#2\tmpdim
    \expandafter\ignorept\the\tmpdim#1%                
}
  • Is there any specific reason for the spaces after \p and \t ? – percusse Apr 20 '15 at 19:56
  • @percusse If you mean the first line -- then the reason is only code transparency. The spaces are consumed by token processor. But I can say \lccode`?=`p, then the space is consumed by main processor and the result is the same. – wipet Apr 20 '15 at 20:01
  • Ah OK, I thought there might be some space sensitive context that I'm not seeing right away. – percusse Apr 20 '15 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.