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.

\makeatletter \newcommand{\Gmrmargin}{\dimexpr\Gm@rmargin} \newcommand{\Gmlmargin}{\dimexpr\Gm@lmargin} \makeatotherand then you can use\Gmrmarginor\Gmlmarginthroughout your document... – Werner Sep 16 '14 at 16:58\Gm@lmarginfor? The computations bygeometryare in the.logfile anyway. – egreg Sep 16 '14 at 17:30\newdimeninstead of usingnewcommand. I suppose the difference is that\newcommandwould evaluate the margin every time it is called, and thedimenis only updated withsetlength. Is it correct?@egreg, thank you! I'm using it to define chapter, section and subsection titles with titlesec and tikz.
– umarcor Sep 16 '14 at 22:031inwas wrong in my comment, just switch the sign. – Heiko Oberdiek Sep 16 '14 at 22:06\newcommand*{\myleftmargin}{\dimexpr...\relax}. This way\myleftmarginis automatically updated, if some of the used values are changed. – Heiko Oberdiek Sep 16 '14 at 22:08Thank you all for your very fast and accurate help ;)
– umarcor Sep 16 '14 at 22:10\newcommand*{\mylmargin}{\dimexpr\oddsidemargin+1in\relax}instead of adimen, and then use it inside thetikzpicture, I get the following error: ! You can't use `\dimexpr' in restricted horizontal mode.\mylmargininsidetikzpicture? It works for the example in the first MWE as value forxshift. – Heiko Oberdiek Sep 16 '14 at 22:30\myrmargin. – Heiko Oberdiek Sep 16 '14 at 22:41\theto the macro definition, expanding the length topt. It works. – umarcor Sep 16 '14 at 23:09