2

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. –  Jan 09 '18 at 16:32
  • @RobtAll Ah cool. I am using xelatex and a4 paper. I shoulda mentioned that. – Jonathan Komar Jan 09 '18 at 16:32
  • Also 20mm with LuaLaTeX and a4 paper. Thus, the problem has something to do with XeTeX. –  Jan 09 '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 09 '18 at 16:37
  • You have to pass a4paper to \documentclass, otherwise xdvipdfmx gets confused. – egreg Jan 09 '18 at 17:07
  • @egreg I thought a4paper is set implicitly...hence the install option to set a4 to default, which writes a4paper to the default options used upon loading the article class. – Jonathan Komar Jan 09 '18 at 17:11
  • 1
    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 09 '18 at 17:28
  • @RobtAll the behaviour here should be the same in luatex as xetex (or pdftex) but if you have a texlive pdf default of us letter the issue does not arise. – David Carlisle Jan 09 '18 at 18:50
  • @DavidCarlisle Interesting. I presume that TeX is internally adjusting for the paper width difference. Seeing as how so many TeX users prefer A4, I would have guessed that the USletter users would be the ones with unexpected results. Anyway, the OP should have mentioned compiler and paper size in the first place. :) –  Jan 09 '18 at 19:04

1 Answers1

2

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
PDF version:    1.5

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
PDF version:    1.5

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.

If you instead do

\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.

egreg
  • 1,121,712