9

The geometry package provides a simple way to set layout parameters. For example,

\usepackage[margin=1in]{geometry}

will give me a document with 1 inch margins on each side. In particular, it computes the LaTeX parameters like \textwidth, \oddsidemargin, \textheight, and \topmargin, among others. Is it possible to retrieve the 'friendlier' values like left, right, top, and bottom?

Of course, one can compute the values using something like \dimexpr \topmargin + \headheight + \headsep + 1in \relax. There's also the further complication that the includehead and includefoot options influence the setting of the parameters.

lockstep
  • 250,273
TH.
  • 62,639

2 Answers2

8

The values are stored in pt in commands like \Gm@lmargin, \Gm@tmargin etc. If you are interested in the unit you actually used in the options you could also patch \Gm@defbylen which is (as far as I can see) used by most options and then set the values with \geometry:

\documentclass[12pt]{article}
\usepackage[ansinew]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{geometry}
\makeatletter
\def\Gm@defbylen#1#2{%
  \expandafter\edef\csname myGm#1\endcsname{#2}%new
  \begingroup\setlength\@tempdima{#2}%
  \expandafter\xdef\csname Gm@#1\endcsname{\the\@tempdima}\endgroup}%

\geometry{margin=1in}

\begin{document}

\makeatletter\Gm@lmargin \myGmlmargin

\end{document}
Hendrik Vogt
  • 37,935
Ulrike Fischer
  • 327,261
0

The command\geometryget takes a single mandatory argument, which should be the name of one of geometry package's optional parameters, e.g. left, top, textheight, etc. (the version below supports only the parameters lmargin, rmargin, tmargin, and bmargin, and their various aliases, but it can be readily extended to include all of geometry's optional parameters).

It expands to the value of the corresponding parameter, in units of pts.

\documentclass{article}

\usepackage{pgfkeys} \pgfkeyssetvalue{/geometry/lmargin}{lmargin} \pgfkeyssetvalue{/geometry/left}{lmargin} \pgfkeyssetvalue{/geometry/inner}{lmargin} \pgfkeyssetvalue{/geometry/innermargin}{lmargin} \pgfkeyssetvalue{/geometry/rmargin}{rmargin} \pgfkeyssetvalue{/geometry/right}{rmargin} \pgfkeyssetvalue{/geometry/outer}{rmargin} \pgfkeyssetvalue{/geometry/outermargin}{rmargin} \pgfkeyssetvalue{/geometry/tmargin}{tmargin} \pgfkeyssetvalue{/geometry/top}{tmargin} \pgfkeyssetvalue{/geometry/bmargin}{bmargin} \pgfkeyssetvalue{/geometry/bottom}{bmargin}

\newcommand{\geometryget}[1] {\csname Gm@\pgfkeysvalueof{/geometry/#1}\endcsname}

\usepackage{geometry}

\begin{document} \begin{tabular}{|ll|ll|ll|ll|} lmargin&\geometryget{lmargin}& rmargin&\geometryget{rmargin}& tmargin&\geometryget{tmargin}& bmargin&\geometryget{bmargin}\ left&\geometryget{left}& outer&\geometryget{outer}& top&\geometryget{top}& bottom&\geometryget{bottom} \end{tabular} \end{document}

Retrieving the values of package geometry's optional parameters


A more substantial example of usage.

The following example uses \geometryget to set the width of the margin notes to .8 of the width of the right margin and to center them in the right margin.

In the code above insert the following to the preamble after the loading of the geometry package.

\geometry{hscale=.66,hmarginratio=1:4}

\usepackage{pgfmath} \pgfmathsetlength{\marginparsep}{.1\geometryget{right}} \pgfmathsetlength{\marginparwidth}{.8\geometryget{right}}

\usepackage{lipsum}

Replace the document's body with the following:

\lipsum[1-1]
\marginpar{\lipsum[2-2][1-1]}

\lipsum[3-7]

Using \geometryget to set the width of the margin notes relative to the width of the right margin

Evan Aad
  • 11,066