[Tex/LaTex] How to get the active left and right margins to use them in a tikzpicture

geometry

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.

Best Answer

\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

Related Question