Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to convert a length in cm (centimeter) to bp (big point). Is there a command to convert cm to bp?

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

You can store the cm in a register, which converts it into the internal pt format and then convert pt to bp using a 72/72.27 factor (graphic/x uses .99626).

You can use the eTeX macro \dimexpr for this. Then it is even expandable:

\def\converttobp#1{\the\dimexpr .99626\dimexpr #1\relax\relax}

This takes a cm or any other dimension and converts it to bp but return it without the bp unit attached. However the two conversions (cm->pt->bp) will add rounding errors.

You can avoid one of them by stripping the cm off and replacing it with pt to make TeX take in points instead, then convert it with the cm-to-bp factor of 28.3464567:

\makeatletter
\def\strip@cm#1cm{#1}
\def\cmtobp#1{\strip@pt\dimexpr 28.3464567\dimexpr\strip@cm #1 pt\relax\relax}
\makeatother
% Usage: \cmtobp{1cm} (returns value in bp without trailing unit)

If you want to use the cm value without unit and get a dimension with bp unit use:

\def\cmtobp#1{\strip@pt\dimexpr 28.3464567\dimexpr #1 pt\relax\relax bp}
share|improve this answer
    
there is a rounding error. –  xport Jul 10 '11 at 11:29
1  
@xport: Yes, sure there is. TeX numeric accuracy isn't overwhelming. You can try a more accurate factor like 0.99626401 but I get the same result for e.g. 1cm. There should be reason why graphics/x uses it. If you want more accurate results I would convert it using an external script. –  Martin Scharrer Jul 10 '11 at 11:36
2  
@xport: No offense, but isn't this the kind of +1-comments we agreed on not to post? –  doncherry Jul 10 '11 at 17:05
    
@Martin: how to add a trailing bp and remove cm when invoking \cmtobp. It means when I invoke \cmtobp{2.54}, I will get 72bp. –  xport Jul 11 '11 at 8:06
    
@xport: I now updated my question to include this version. –  Martin Scharrer Jul 11 '11 at 8:17
add comment

And if you need to "ceil" the result:

\makeatletter
\def\strip@cm#1cm{#1}
\def\cmtobp#1{\expandafter\@ceil
  \the\dimexpr 28.3464567\dimexpr\strip@cm #1 pt\relax\relax}

\begingroup\catcode`P=12 \catcode`T=12
\lowercase{\endgroup
  \def\@ceil#1.#2PT{\number\numexpr#1+0\ifnum#2>\z@ 1\fi\relax}}
\makeatother

\cmtobp{3cm} % -> 86
\cmtobp{2.54cm} % -> 72
\cmtobp{2.541cm} % -> 73

This uses the fact that \the<dimen> has always at least one decimal digit (possibly 0).

The following version has different input and output, as per request:

\makeatletter
\def\cmtobp#1{\expandafter\@ceil
  \the\dimexpr 28.3464567\dimexpr #1pt\relax\relax}

\begingroup\catcode`P=12 \catcode`T=12
\lowercase{\endgroup
  \def\@ceil#1.#2PT{\number\numexpr#1+0\ifnum#2>\z@ 1\fi\relax bp}}
\makeatother

\cmtobp{3}     % -> 86bp
\cmtobp{2.54}  % -> 72bp
\cmtobp{2.541} % -> 73bp
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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