4

I'm trying to use the margins defined when geometry is loaded to draw several fancy section titles using tikz. In order to make \Gm@lmargin and \Gm@rmargin usable, I have to do

\makeatletter\Gm@lmargin
\makeatletter\Gm@rmargin

I'm only allowed to do so after \begin{document}. Since I'm defining many titles and other stuff before, I'd like to make those margins usable just after the geomtry package is loaded, or \newgeometry is defined.

Do you know how to do \makeatletter\Gm@lmargin before \begin{document} ?

MWE:

\documentclass[a4paper]{report}
\usepackage[left=3.5cm, right=1.5cm, top=2.5cm, bottom=2.5cm]{geometry}
%I'd like to make them usable here
\usepackage{tikz,bm}
\usetikzlibrary{arrows}
\usetikzlibrary{calc,positioning}

%Definition of fancy stuff

\begin{document}

%I get errors if done before this point
\makeatletter\Gm@lmargin
\makeatletter\Gm@rmargin

\vspace{2cm}

  \begin{tikzpicture}[remember picture, overlay]
      \node[anchor=east,xshift=-\Gm@rmargin,rectangle,draw=black,fill=white] at (current page.east|-0,0) {Section};
  \end{tikzpicture}

\end{document}

I've tried the procedures commented by @Heiko Oberdiek and @Werner, and this is the MWE:

\documentclass[a4paper]{report}

\newdimen\plmargin \setlength{\plmargin}{3.5cm}
\newdimen\prmargin \setlength{\prmargin}{1.5cm}

\usepackage[left=\plmargin, right=\prmargin, top=2.5cm, bottom=2.5cm]{geometry}

\usepackage{tikz,bm}
\usetikzlibrary{arrows,calc,positioning}

\newdimen\mylmargin \setlength{\mylmargin}{\dimexpr\oddsidemargin+1in\relax}
\newdimen\myrmargin \setlength{\myrmargin}{\dimexpr\paperwidth-\oddsidemargin-1in-\textwidth\relax}

\makeatletter
\newdimen\Gmrmargin \setlength{\Gmrmargin}{\dimexpr\Gm@rmargin}
\newdimen\Gmlmargin \setlength{\Gmlmargin}{\dimexpr\Gm@lmargin}
\makeatother

\begin{document}

\noindent
plmargin: \the\plmargin ~prmargin: \the\prmargin \\
oddsidemargin: \the\oddsidemargin ~evensidemargin: \the\evensidemargin \\
mylmargin: \the\mylmargin ~myrmagin: \the\myrmargin \\
Gmlmargin: \the\Gmlmargin ~Gmrmargin: \the\Gmrmargin

\end{document}

And the results:

plmargin: 99.58464pt      prmargin: 42.67912pt
oddsidemargin: 27.31465pt evensidemargin: 27.31465pt
mylmargin: 99.58464pt     myrmagin: 42.67912pt
Gmlmargin: 99.58464pt     Gmrmargin: 42.67912pt

Both work great. The solution provided by @Werner depends on the variables defined by geometry, as I asked. However, @Heiko Oberdiek pointed out why it should be better to use the official length registers:

\Gm@lmargin and \Gm@rmargin are not always defined. They are unknown if package geometry is not used. If package geometry is used, it depends on the options; for example, \usepackage[pass]{geometry} will not define them.

12
  • You're not using it correctly. Define in your preamble \makeatletter \newcommand{\Gmrmargin}{\dimexpr\Gm@rmargin} \newcommand{\Gmlmargin}{\dimexpr\Gm@lmargin} \makeatother and then you can use \Gmrmargin or \Gmlmargin throughout your document...
    – Werner
    Sep 16 '14 at 16:58
  • 1
    Welcome to TeX.SX! What would you be using \Gm@lmargin for? The computations by geometry are in the .log file anyway.
    – egreg
    Sep 16 '14 at 17:30
  • @Werner, as you can see in the edited question, what you pointed worked, but I had to define \newdimen instead of using newcommand. I suppose the difference is that \newcommand would evaluate the margin every time it is called, and the dimen is only updated with setlength. Is it correct? @egreg, thank you! I'm using it to define chapter, section and subsection titles with titlesec and tikz. Sep 16 '14 at 22:03
  • @U.Martinez-Corral: Yes, that is correct.
    – Werner
    Sep 16 '14 at 22:03
  • @U.Martinez-Corral The sign for 1in was wrong in my comment, just switch the sign. Sep 16 '14 at 22:06
1

\Gm@lmargin and \Gm@rmargin are internal macros belonging to the implementation details that might change in any later version of package geometry.

Instead I would use two dimen registers instead of the internals:

\documentclass[a4paper]{report}

\newdimen\myleftmargin
\newdimen\myrightmargin
\setlength{\myleftmargin}{3.5cm}
\setlength{\myrightmargin}{1.5cm}

\usepackage[
  left=\myleftmargin,
  right=\myrightmargin,
  top=2.5cm,
  bottom=2.5cm
]{geometry}

\usepackage{tikz,bm}
\usetikzlibrary{arrows}
\usetikzlibrary{calc,positioning}

% Definition of fancy stuff
% \myleftmargin and \myrightmargin can be used here

\begin{document}

left margin: \the\myleftmargin, right margin: \the\myrightmargin

\vspace{2cm}

  \begin{tikzpicture}[remember picture, overlay]
      \node[anchor=east,xshift=-\myrightmargin,rectangle,draw=black,fill=white]
 at (current page.east|-0,0) {Section};
  \end{tikzpicture}

\end{document}

Result

4
  • @U.Martinez-Corral \oddsidemargin (\evensidemargin) still exists. The left margin would be \dimexpr\oddsidemargin-1in\relax and the right margin is \dimexpr\paperwidth-\oddsidemargin+1in-\textwidth\relax (if the "marginpar" area is not used). Sep 16 '14 at 21:03
  • Thanks for your recommendation. I'm actually doing that as a workaround. But I think it would be more "elegant" to define the dimensions in a unique command (loading geometry or using \newgeometry} and have the variables updated automatically. Since the macros may change, I've thought of using the \appto hook available with etoolbox. Something like: \usepackage[left=1.5cm,right=1.5cm,top=2.5cm,bottom=2.5cm]{geometry} \newcommand\mygeometry{ %Code to update \myleftmargin and \myrightmargin } \appto\newgeometry{\mygeometry} Sep 16 '14 at 21:06
  • @U.Martinez-Corral Then I would calculate the needed margins from the official length registers as shown in my previous comment. Of course, these calculateions can be hidden in a command, The full \dimexpr...\relax can be used as "read-only" length. \Gm@lmargin and \Gm@rmargin are not always defined. They are unknown if package geometry is not used. If package geometry is used, it depends on the options; for example, \usepackage[pass]{geometry} will not define them. Sep 16 '14 at 21:12
  • @U.Martinez-Corral Wrong sign in the first comment: reverse -1in by +1in and reverse +1in by -1in. Sep 16 '14 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.