-1

I want a command to convert from one unit (pt, ex, em, in, bp, dd, pc, mm, cm) to another. What can I do with l3 ?

Ragonese
  • 171
  • 4

2 Answers2

6

With great accuracy (perhaps too great, so I provide a limit to the decimal digit):

\documentclass{article}

\NewExpandableDocumentCommand{\convertto}{omm}{% % #1 = number of decimal digits % #2 = length % #3 = new unit \IfNoValueTF{#1}{% \fpeval{\dimeval{#2}/(1#3)},#3% }{% \fpeval{round(\dimeval{#2}/(1#3),#1)},#3% }% }

\begin{document}

\convertto{1pc}{pt}

\convertto{1in}{cm}

\convertto[2]{1in}{cm}

\convertto{\textwidth}{cm}

\convertto{1ex}{mm}

{\Large\convertto{1ex}{mm}}

\end{document}

enter image description here

egreg
  • 1,121,712
  • 1
    I'd argue about the accuracy part, after all an inch is defined to be 2.54cm, and the 2.539999648540197 is the result fo TeX's inaccuracy being built with fixed point in sp that l3fp mimics. – Skillmon Mar 02 '24 at 22:04
  • Have change from omm to mmoand do getBad argument specification 'mmo'`. – Ragonese Mar 02 '24 at 22:23
  • 2
    @Ragonese Of course. It's accurately explained in usrguide, which you probably forgot to read. – egreg Mar 02 '24 at 22:26
  • I do not understand it. – Ragonese Mar 02 '24 at 22:50
  • @Ragonese If you want a fully expandable command, the last argument must be of type m. – egreg Mar 02 '24 at 22:58
  • What is the reason for choosing a fully expandable command ? – Ragonese Mar 02 '24 at 23:11
  • @Ragonese - A macro that's (fully) expandable can feature in the argument of another macro. That's usually a very useful property. – Mico Mar 02 '24 at 23:32
  • So I can then use the result as argument to another command. Is this the advantage ? – Ragonese Mar 02 '24 at 23:34
  • @Ragonese - No, that's not what I wrote. I did not claim that the result of an expandable argument can be entered in the argument of some other macro. (That's trivially true of all macros that produce some output.) I wrote that the expandable macro itelf can be in the argument of some other macro. – Mico Mar 03 '24 at 00:06
  • Like \this{that}{\convertto[2]{34pt}{mm}} ? – Ragonese Mar 03 '24 at 00:38
  • 1
    @Ragonese - I have no idea what \this does or is supposed to do. Assuming \thislength is defined as a length, a more natural example might be \setlength{\thislength}{\convertto[2]{34pt}{mm}}, although \setlength{\thislength}{34pt} would work too. This example, by the way, also illlustrates why you've received several questions as to what the purpose of \convertto might be: For everywhere where \convertto[2]{34pt}{mm} is legal, so is 34pt. – Mico Mar 03 '24 at 03:34
5

The l3skip module provides \dim_to_decimal_in_unit:nn.

From LaTeX3 Interface, p. 231 (2024-02-20 release):

Evaluates the ⟨dim exprs ⟩, and leaves the value of ⟨dim expr1 ⟩, expressed in a unit given by ⟨dim expr2 ⟩, in the input stream. If the decimal part of the result is zero, it is omitted, together with the decimal marker. The precisions of the result is limited to a maximum of five decimal places with trailing zeros omitted.

For example

\dim_to_decimal_in_unit:nn { 1bp } { 1mm }

leaves 0.35278 in the input stream, i.e. the magnitude of one big point when expressed in millimetres.

jlab
  • 1,834
  • 1
  • 13