I've looked at some related questions and can't solve this.
I have a common LaTeX file where I load the geometry
package among others.
This file (called pre.tex
here) is a common, site wide file which loads many packages. These options work for almost all files on the site, But sometimes one file needs different geometry options.
Hence I want to change geometry setting after inputting this common LaTeX file.
I am following the answer give in Unload a LaTeX package but it is giving me an error. Here is a MWE:
\documentclass[11pt,titlepage]{article}%
\makeatletter
\@namedef{geometry.sty}{}% a fake for a "loaded" package
\makeatother
\input{pre} %this has in it \usepackage{geometry} with some options
% I want to override common file and load geometry
% with different options here
\usepackage[letterpaper,bindingoffset=0.2in,
left=1.2in,right=1.2in,top=.8in,bottom=.8in,
footskip=.25in]{geometry}
\begin{document}
test
\end{document}
Where pre.tex
has just the line
\usepackage[letterpaper,bindingoffset=0.2in,left=2in,right=2in]{geometry}
The error from lualatex file.tex
is
(/usr/local/texlive/2018/texmf-dist/tex/generic/ifxetex/ifxetex.sty))
! LaTeX Error: Option clash for package geometry.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.12
?
The strange thing is that this works and compiles
\documentclass[11pt,titlepage]{article}%
\makeatletter
\@namedef{geometry.sty}{}% a fake for a "loaded" package
\makeatother
\input{pre} %this has in it \usepackage{geometry}
\usepackage{geometry}
\begin{document}
test
\end{document}
So it only fails when I want to load geometry
again with options that are different from the original loading?
What is the correct way to remove geometry
once loaded including any options it was loaded with, so that if specific file want to use different options they can reload geometry with different options?
Using TL 2018
Best Answer
It will probably always be a bad idea to trick LaTeX into allowing you to load a package twice. Even if you managed to to that by un-defining
\ver@geometry.sty
(which I will not show how -- pretty easy, though -- so people don't blame me for teaching this :), you would get a long list of other errors, such asbecause packages aren't supposed to be loaded twice, so they use
\newcommand
, which we all know doesn't work twice.That said, for this geometry case, you can pass your options to the
\geometry
command:From the manual:
For a more general case, I would suggest turning your
pre.tex
into a packagepre.sty
and adding some options to it. For example, here's what I did in a class I wrote to have predefined values but allow the user to override them:Basically, this sets up the package loading so that the package has a list of default options for each package, the user is allowed to pass additional options to that package that respect the package's defaults and is also allowed to override those defaults, if they explicitly ask for that.
The code is is quite long because it allows interfacing with any number of packages and allows this three-level hierarchy. But for your case, with only one package and no hierarchy levels (that is, any option overrides the package's defaults) it could be reduced to: