6

I want to place some elements on my page for which I need to calculate their size.

For my example lets say three of those elements should exactly occupy a \linewidth when put side by side without space in between.

The easiest possibility might be to just give their width as 0.3\linewidth -- but that might be a bit too small generating a gap somewhere. It is however possible to just use 0.3333333333333\linewidth -- but that is much to write for a seemingly simple fraction [and it strikes my pedanticism as it's not exactly one third].

If I want to get an exact value of one third, I may use

\newlength\onethirdlinewidth
\onethirdlinewidth=\linewidth
\divide\onethirdlinewidth by 3

that might be the best way if I use this length multiple times but might be a bit much to type for a one-shot use.

My question is: Is there any simple possibility to get a length of one third (or seven eighths) of a given length?

  • 1
    \dimexpr\linewidth/3 and \dimexpr7\linewidth/8 work. However TeX uses scaled integers to represent dimensions, so technically 0.3333333333333\linewidth is more precise than TeX's representation of \linewidth/3. When you input a long decimal chain like that TeX will truncate that to a value it can represent. – Phelype Oleinik Jan 29 at 12:47
  • Actually, I didn't think about dimexpr. Mind putting that in an answer? – David Woitkowski Jan 29 at 12:51
  • @PhelypeOleinik "a long decimal chain like that TeX will truncate that to a value it can represent" is misleading, it could be that no truncation can be exactly represented as an integer multiple of pt/65536. – user4686 Jan 29 at 12:58
  • 1
    the most precise way to multiply by a fraction is \dimexpr\numexpr A*<dimen>/B sp\relax (where <dimen> is like \linewidth but not 10pt then use \dimexpr10pt\relax in place of <dimen>) – user4686 Jan 29 at 13:00
  • @jfbu Agreed, unfortunate choice of words. On your second comment, why is that? Do you mind writing the answer explaining your comment, please? – Phelype Oleinik Jan 29 at 13:03
6

I described how TeX inputs dimensions and handles units in https://tex.stackexchange.com/a/231281 and Why pdf file cannot be reproduced? and possibly at other locations, including some comments which are not always read.

I am using Plain TeX but of course it works exactly the same in LaTeX.

\newdimen\fixed

\fixed 1pt

\newdimen\testA

\newdimen\testB

\testA 0.33333587646484374\fixed

\testB 0.33333587646484375\fixed

\ifdim\testA = \testB
  The two dimensions are equal
\else
  The two dimensions are not equal
\fi

\bye

Outputs:

enter image description here

One needs 17 fractional digits to be certain that the dimension stabilizes (of course you get only 1sp possible difference after 5 fractional digits, because 1/10^5 < 1/65536, here in this example where one multiplies 1pt). And some things are counterintuitive, for example 0.33333 is enough but 0.22222 is not although it looks closer to 0.222222 than 0.33333 was to 0.333333.

It goes without saying that Knuth has programmed it exactly to fetch 17 fractional digits and not one more, because the theorem is that it will never change after that.



As another random example consider this

\number\dimexpr 0.824440000pt\relax

\number\dimexpr 0.824440003pt\relax

\bye

which produces

enter image description here

showing that 0.824440003 gives distinct result from 0.82444.

We can confirm this also in a rôle as <factor> :

\documentclass[a4paper]{article}
\usepackage{geometry}

\newlength{\mylength}
\begin{document}

\setlength{\mylength}{0.82444\linewidth}

\verb|0.82444\linewidth| gives \the\mylength.

\setlength{\mylength}{0.824440003\linewidth}

\verb|0.824440003\linewidth| gives \the\mylength.

These two things differ!

I hope this will dispel some misunderstandings\\
about ``five fractional digits suffice''. Wrong.
\end{document}

enter image description here

Notice that above \linewidth is 418.25368pt so 0.000000003\linewidth is in truth 0.00000125476104pt well below the TeX "error".

THE DIFFERENCE IS AMPLIFIED BY A FACTOR GREATER THAN 5000 !

As per the actual question, here is my comment

the most precise way to multiply by a fraction is \dimexpr\numexpr A*<dimen>/B sp\relax (where <dimen> is like \linewidth but not 10pt then use \dimexpr10pt\relax in place of <dimen>) 
  • @jfbu And it would be great if you would consider to stay on TeX.SE. Things happen, tone can get rough, and it can be healed by understanding on both sides and cleaning up comments or texts that led to it. – Stefan Kottwitz Jan 30 at 18:40

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.