0

This is somehow related to this Load a package as optional argument:

I'm trying to load geometry package as optional, meaning: if is defined in preamble should be load with my margins, if not document will have the default geometry of article, book, report etc.

I have to mention that I'm not a programmer at all, but in my mind what I have wrote in MWE makes sens :). Basicaly I'm trying to find a formula (for my Humanistic mind) -- i.e. in simple steps, to load some packages with my specifications when I need them.

I would also appreciate some %%% comments %%% on the code, because I want to understand and to reproduce on other packages that I want to load.


With my code, margins are changing, but not with my specs...


The MWE

% !TEX program = pdflatex
% !TeX encoding = UTF-8

\documentclass{article}
\usepackage{kantlipsum}
\usepackage{testpckg}
%\usepackage{geometry}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{filecontents}[force]{testpckg.sty}


\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{testpckg}[2021/02/26 Test package]

\RequirePackage[T1]{fontenc}
\RequirePackage[utf8]{inputenc}

%\RequirePackage{xstring}   
%\RequirePackage{geometry}
    
    % -- if specified in preamble load with this specs
\newif\iftestpckg@geometry
\DeclareOption{geometry}{%
    \testpckg@geometrytrue
    \geometry{top=.5cm,bottom=.5cm,right=.5cm,left=.5cm} % <-- just for better visualization
    }
    \ProcessOptions
    \iftestpckg@geometry
    \RequirePackage{geometry} % <-- else load default geometry from article, book, report...
    \fi
    
\end{filecontents}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\kant

\end{document}

2
  • You can't actually prevent users from loading geometry themselves and choose their own parameters.
    – egreg
    Feb 26 at 10:59
  • @egreg can you please elaborate? Because I can choose my own parameters if I specify optional arguments [margin=2.5cm].
    – Mafsi
    Feb 26 at 11:01
1
% !TEX program = pdflatex
% !TeX encoding = UTF-8

\begin{filecontents}[force]{testpckg.sty}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{testpckg}[2021/02/26 Test package]

\RequirePackage[T1]{fontenc}
\RequirePackage[utf8]{inputenc}

\newif\iftestpckg@geometry

\DeclareOption{geometry}{\testpckg@geometrytrue}
\ProcessOptions

\RequirePackage{geometry}

\iftestpckg@geometry
  \geometry{top=.5cm,bottom=.5cm,right=.5cm,left=.5cm}
\fi
\end{filecontents}

\documentclass{article}
\usepackage{kantlipsum}
\usepackage{testpckg}
%\geometry{margin=5cm}

\begin{document}
 \kant 
\end{document}

With \usepackage{testpckg} you will get:

enter image description here

With \usepackage[geometry]{testpckg} you will get:

enter image description here

2
  • Thank you! Can I use your model to other packages?
    – Mafsi
    Feb 26 at 13:01
  • 1
    Yes, of course. But just to be honest I wouldn't call it "mine" or even a "model" ;-) As a suggestion I recommend starting from an existing package to understand how to handle the options. At least I do that
    – Ivan
    Feb 26 at 13:17

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.