2

I would like to calculate the the ratio between two lengths in order to automatically get the scale for a map included using the mercatormap package. For instance, I want the map to show 1km in the horizontal direction and be \textwidth wide, this means that the scale argument I have to provide would be something like 100000cm / \textwidth.

I found this solution:

\documentclass{minimal}
\usepackage{graphicx}

\begin{document}

\makeatletter
\Gscale@div\tmp {100cm}{\textwidth}

{\textbackslash}textwidth = \the\textwidth

100cm / {\textbackslash}textwidth = \tmp

\Gscale@div\tmp {100000cm}{\textwidth}       % Causes: ! Dimension too large. <to be read again> [...] 
                                             % I can't work with sizes bigger than about 19 feet.

100000cm / {\textbackslash}textwidth = \tmp  

\end{document}

enter image description here

which apparently does not work for large dimensions. Given that 1pt is about 28.4cm the first ratio is calculated correctly, the second one on the other hand raises the error on the comment above. Got the same error using \pgfmathparse as well.

Is there a way to get around this "about 19 feet" limitation?

1 Answer 1

4

You can use xfp and \fpeval.

\documentclass{article}
\usepackage{xfp}

\begin{document}

\edef\tmp{\fpeval{100cm/\textwidth}}

100cm = \the\dimexpr100cm\relax

\verb|\textwidth| = \the\textwidth

\tmp

\end{document}

Depending on your use case, you may be able to even avoid \tmp.

enter image description here

With bc I get

bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
2845.27559/345
8.24717562318840579710

so the results agree to the eighth decimal place, which is more than enough for TeX.

1
  • This works perfectly! And yes, it is way more precise than it needs to be! :) Thank you very much!
    – Ninoett
    Jul 3, 2022 at 20:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .