# How to get the active left and right margins to use them in a tikzpicture?

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.

• Welcome to TeX.SX! What would you be using \Gm@lmargin for? The computations by geometry are in the .log file anyway. Sep 16 '14 at 17:30
• @U.Martinez-Corral: Yes, that is correct. 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

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

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


• @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