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 ?
-
5What's the TeX angle of this query? After all, it's pretty much universally known 1in=2.54cm=25.4mm, 1cm=10mm, 1in=72.27pt=72bp, etc. – Mico Mar 02 '24 at 20:53
-
1Why would you need to convert from one unit to another? Latex can just deal fine with different units. – samcarter_is_at_topanswers.xyz Mar 02 '24 at 20:58
-
I want to match equivalence to values set in other packages. The angle is for doing the conversion using a latex command. – Ragonese Mar 02 '24 at 21:09
-
I still don't understand that. If another package does 12pt, then you can do 12pt. If your command is looking for mm, then TeX will do that conversion. The only way I can see is if you're trying to parse user input. But that would be silly. – Teepeemm Mar 02 '24 at 21:41
-
See also https://tex.stackexchange.com/q/8260/107497 – Teepeemm Mar 02 '24 at 21:43
-
4@Ragonese Like so many of your previous questions (including the ones from your previous accounts), this again very much sounds like an xy-question. You can compare lengths regardless of their dimension. – samcarter_is_at_topanswers.xyz Mar 02 '24 at 21:49
-
Although I have very good reasons for the task, it is not the purpose here to first convince others about. – Ragonese Mar 02 '24 at 23:31
-
2@Ragonese You don't need to convince others, but you need to ask clear questions. – samcarter_is_at_topanswers.xyz Mar 03 '24 at 10:37
2 Answers
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}
- 1,121,712
-
1I'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
l3fpmimics. – Skillmon Mar 02 '24 at 22:04 -
Have change from
ommto 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 -
-
@Ragonese If you want a fully expandable command, the last argument must be of type
m. – egreg Mar 02 '24 at 22:58 -
-
@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
-
-
1@Ragonese - I have no idea what
\thisdoes or is supposed to do. Assuming\thislengthis 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\converttomight be: For everywhere where\convertto[2]{34pt}{mm}is legal, so is34pt. – Mico Mar 03 '24 at 03:34
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.
- 1,834
- 1
- 13
