2

I'm try to write a package with pgfkeys and etoolbox. What I want is to define a boolean and a key. Depending if the key is true or false I want to load a package.

The boolean seems to work but the package isn't loaded regarding to the listed packages in the log file.

\begin{filecontents}{iotestm.sty}
    \ProvidesPackage{iotestm}[mwe for golatex]
    \RequirePackage{etoolbox, pgfopts}

    %---------------------------------------------------
    % Defining package options
    %---------------------------------------------------
    \newbool{test@if@prettyfont}

    \pgfkeys{
        test/.cd,
        pretty-font/.is choice,
            pretty-font/true/.code = \booltrue{test@if@prettyfont},
            pretty-font/false/.code= {\boolfalse{test@if@prettyfont}},
            pretty-font/.initial = true
    }

    \newrobustcmd\testsetup[1]{\pgfqkeys{/test}{#1}}

    \ProcessPgfOptions{/test}

    %---------------------------------------------------
    % Defining package commands
    %---------------------------------------------------
    \ifbool{test@if@prettyfont}{%
        \RequirePackage{lmodern}%
    }{}

    \newcommand{\testfont}{\ifbool{test@if@prettyfont}{true}{false}}
\end{filecontents}

\listfiles
\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{iotestm}

\testsetup{%
    pretty-font = false
}

\begin{document}
    Font: \testfont
\end{document}

Can someone tell me what I'm doing wrong or is it a completely wrong usage what I've done so far?

1 Answer 1

2

The \testsetup macro is used a 'long' time after iotestm has been loaded -- at that loading time, test@if@prettyfont is not set to true and the test expands to false, thereby not loading the requested package(s).

The \ifbool{...}{\RequirePackage{...}}{...} test can be deferred to the end of the preamble hook, with \AtEndPreamble, at this stage the \testsetup macro has been called and set the relevant data.

\begin{filecontents}{iotestm.sty}
  \ProvidesPackage{iotestm}[mwe for golatex]
  \RequirePackage{etoolbox}
  \RequirePackage{pgfopts}

    %---------------------------------------------------
    % Defining package options
    %---------------------------------------------------
    \newbool{test@if@prettyfont}

    \pgfkeys{
        test/.cd,
        pretty-font/.is choice,
        pretty-font/true/.code = {\booltrue{test@if@prettyfont}},
        pretty-font/false/.code= {\boolfalse{test@if@prettyfont}},
        pretty-font/.initial=true,
    }

    \newrobustcmd\testsetup[1]{\pgfqkeys{/test}{#1}}

    \ProcessPgfOptions{/test}

    %---------------------------------------------------
    % Defining package commands
    %---------------------------------------------------
    \AtEndPreamble{
      \ifbool{test@if@prettyfont}{%
        \RequirePackage{lmodern}%
      }{}
    }

    \newcommand{\testfont}{\ifbool{test@if@prettyfont}{true}{false}}
\end{filecontents}

\listfiles
\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{iotestm}


\testsetup{%
    pretty-font=true
}



\begin{document}
    Font: \testfont
\end{document}
2
  • Thanks that works. So if I want to load a couple different packages I put them all inside the \AtEndPreamble macro? Or is there another better approach?
    – faltfe
    Commented Aug 30, 2017 at 18:51
  • @faltfe: Not as far as I know.
    – user31729
    Commented Aug 30, 2017 at 18:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .