# How can I get precise horizontal margins the manual way?

It seems that I am getting imprecise values for my left and right margins. What magic is geometry doing that I am not?

My intended paper size was A4 (but as discussed in the comments and answer, this was not completely the case). I've measured 18mm left and 16mm right. Printed it without scaling it. <—just to clarify this: I printed the pdf to a physical sheet of paper and used a ruler and my near-sighted eyes.

\documentclass{article}
\usepackage{fontspec}% xelatex
\usepackage{lipsum}
\setlength{\textwidth}{\dimexpr \paperwidth - 20mm * 2 \relax}
\setlength{\oddsidemargin}{\dimexpr (\paperwidth - \textwidth)/2 - 1 in\relax}
\begin{document}
\lipsum

\lipsum
\end{document}

• Using LuaLaTeX, I get exactly 20mm for each margin, measured by bringing the PDF as an image to a graphics program. My default paper is USletter. – user139954 Jan 9 '18 at 16:32
• @RobtAll Ah cool. I am using xelatex and a4 paper. I shoulda mentioned that. – Jonathan Komar Jan 9 '18 at 16:32
• Also 20mm with LuaLaTeX and a4 paper. Thus, the problem has something to do with XeTeX. – user139954 Jan 9 '18 at 16:37
• is your default pdf size A4? (you may want to set \pdfpageheight and width to \paperheight and width) – David Carlisle Jan 9 '18 at 16:37
• The install time option sets defaults for your pdf viewer and perhaps for the dvi to pdf driver but it does not affect latex itself which always defaults to US Letter. – David Carlisle Jan 9 '18 at 17:28

Let's see what happens.

By default, LaTeX sets paper size to US letter. If I only add to your example, after \begin{document},

\showthe\paperwidth
\showthe\textwidth
\showthe\oddsidemargin


the console shows

> 614.295pt.
l.8 \showthe\paperwidth

?
> 500.484pt.
l.9 \showthe\textwidth

?
> -15.36449pt.
l.10 \showthe\oddsidemargin

?


which are exactly the same value we'd get by doing

\usepackage[left=20mm,right=20mm]{geometry}


However, xdvipdfmx is set to output to A4 paper, according to your comments. If I open the PDF file and check its properties, I get

Creator:         XeTeX output 2018.01.09:1918
Producer:       xdvipdfmx (20170318)
CreationDate:   Tue Jan  9 19:18:31 2018
Tagged:         no
Form:           none
Pages:          2
Encrypted:      no
Page size:      595.28 x 841.89 pts (A4) (rotated 0 degrees)
File size:      13853 bytes
Optimized:      no


which becomes

Creator:         XeTeX output 2018.01.09:1923
Producer:       xdvipdfmx (20170318)
CreationDate:   Tue Jan  9 19:23:09 2018
Tagged:         no
Form:           none
Pages:          2
Encrypted:      no
Page size:      612 x 792 pts (letter) (rotated 0 degrees)
File size:      13845 bytes
Optimized:      no


when geometry is loaded instead: this is because geometry informs xdvipdfmx of the output paper size.

In my measurement, the “non-geometry” version has a left margin of 20mm, and the right margin becomes 14mm. What's 612 – 595.28 (Postscript points)? It's 5.9mm.

\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{lipsum}
\setlength{\textwidth}{\dimexpr \paperwidth - 20mm * 2 \relax}
\setlength{\oddsidemargin}{\dimexpr (\paperwidth - \textwidth)/2 - 1 in\relax}
\begin{document}
\lipsum

\lipsum
\end{document}


you'll get the desired margins.