[Tex/LaTex] Is possible set table or \textwidth fixed width in cm? (LATEX)

pdftex

I need create a table in a document with a width of exactly 24cm. Print will be A4 – landscape (in EU if it matters). Is there some way set it?

If I set:

\usepackage[a4paper,landscape,top=0cm, bottom=0cm, left=0cm, right=0cm]{geometry}

then my \textwidth is 29.69577cm.

EDIT (I forgot):

when I set:

\usepackage[a4paper,landscape,textwidth=24cm]{geometry}

then my \textwidth is 23.99658cm

I want to use this later on:

\begin{tabular*}{\textwidth}

I am doing somethig wrong?

Best Answer

First case:

\documentclass{article}
\usepackage[a4paper,landscape,top=0cm, bottom=0cm, left=0cm, right=0cm]{geometry}
\typeout{* paper width: \the\paperwidth}
\typeout{* text width: \the\textwidth}
\begin{document}
\end{document}

The margins are zero, thus the text width equals the paper width:

* paper width: 845.04684pt
* text width: 845.04684pt

Conversion to cm:

* 72.27 pt = 1 in
* 1 in = 2.54 cm

Result (gcalculator): 29.6999996347 cm
Result (perl1): 29.6999996347032 cm

This is pretty close to the 29.7 cm of the longer side of A4 paper.

TeX's accuracy is limited by it smallest unit: 1 sp

  • 1 pt = 216 sp = 65536 sp ≈ 0.000000536285 cm

Specifying 297 mm instead of 29.7 cm improves the accuracy:

\documentclass{article}
\usepackage[a4paper, paperheight=297mm, landscape, margin=0cm]{geometry}
\typeout{* paper width: \the\paperwidth}
\typeout{* text width: \the\textwidth}
\begin{document}
\end{document}

Result:

* paper width: 845.04684pt
* text width: 845.04684pt

Conversion to cm (perl): 29.6999996347032 cm

(By adding 1 sp this can be made larger and a tiny little bit closer to 29.7 cm.)

Second case:

\documentclass{article}
\usepackage[a4paper,landscape,textwidth=24cm]{geometry}

\typeout{* text width: \the\textwidth}

\begin{document}
\end{document}

Result:

* text width: 682.86613pt

Conversion to cm (perl): 23.9999995876574 cm

Again very close to 24 cm, the greatest value ≤ 24 cm in TeX. (It could made be closer to, but larger as 24 cm by adding 1 sp.)

Calculations in TeX

A scaling operation (multiplication followed by division) in e-TeX's \...expr commands provide the best accuracy, from the e-TeX manual:

The arithmetic operations are performed individually, except for 'scaling' operations (a multiplication immediately followed by a division) which are performed as one combined operation with a 64-bit product as intermediate value.

Also e-TeX's \...expr commands round the result rather than truncate it to fit the result in TeX's internal sp scala.

Second case as example:

\documentclass{article}
\usepackage[a4paper,landscape,textwidth=240mm]{geometry}

\makeatletter
\typeout{* text width: \strip@pt\dimexpr\textwidth*254/7227\relax cm}
\makeatother

\begin{document}
\end{document}

The real numbers 2.54 and 72.27 are multiplied by 100 to get an integer for the divisor. \strip@pt removes the pt at the end. Result:

* text width: 24cm

1 perl -e 'print 845.04684/72.27*2.54'

Related Question