[Tex/LaTex] Using option for sty file

kvoptionspackage-optionspackages

I would like to create and option (namely grid) to my sty file to produce a grid covering the whole page.

I had success using

\newif\if@sigur@grid
\DeclareOption{grid}{\@sigur@gridtrue}

and using the code below, which I copied from somewhere here on SE, inside some blocks of codes.

\if@sigur@grid
  \IfFileExists{tikz.sty}
    {\RequirePackage{tikz}%
    \usetikzlibrary{shapes.misc}%
    \usepackage{eso-pic}%
    }
    {\PackageWarning{sigur}{You requested `tikz.sty' %MessageBreak 
    which doesn't exist on your system}
    }
    \AtBeginDocument{
     \AddToShipoutPicture{%
     \begin{tikzpicture}[overlay,remember picture]
     \draw[red!30!white]
                 (current page.south west) grid[step=1mm]
                 (current page.north east);
     \draw[red!80!white]
                 (current page.south west) grid[step=10mm]
                 (current page.north east);
     \end{tikzpicture} 
     }
     }
\fi

Well, this is fine and it is working. I simply load my sty file with the option grid and the red grid appears.

Now here comes my problem. As you can see the grid was drawn in red color. I'd like to be able to pass its color to the grid option when loading it. For example, the default color is red but if I want blue grid I'd simply use grid=blue as option and the grid would appear in blue color.

How to do this?

ps: I'd like to test first if some additional package is installed. I'm doing this for other packages using \IfFileExists{} and it is working. But I'm not able to do this for the solution proposed by @cgnieder below.

The reason for the test is that sometimes I share this style file with other users and I'd like to inform them with some option is not working instead of just show many errors caused by the missing packages.

Best Answer

The pgfopts is a convenient way of using pgf's keys as package options. Here's an example where

\usepackage{sigur}

does not draw a grid,

\usepackage[grid]{sigur}

draws the default red grid, and

\usepackage[grid=green]{sigur}

draws a green grid.

\RequirePackage{filecontents}% be careful not to overwrite your actual package!
\begin{filecontents}{sigur.sty}
\ProvidesPackage{sigur}[2013/11/09 v0.0 a sample package]

\newif\if@sigur@grid
\newcommand*\sigur@grid@color{red}

\IfFileExists{pgfopts.sty}{% only if `pgfopts' is available
\RequirePackage{pgfopts}

\pgfkeys{
  sigur/.cd ,
    grid/.code =
      \@sigur@gridtrue
      \renewcommand*\sigur@grid@color{##1} ,
    grid/.default = red % if no value is given
}

\ProcessPgfOptions{/sigur}
}{% else
\DeclareOption{grid}{\@sigur@gridtrue}
\ProcessOptions*\relax
}

\if@sigur@grid
  \RequirePackage{tikz}
  \usetikzlibrary{shapes.misc}
  \RequirePackage{eso-pic}
  \AtBeginDocument{%
    \AddToShipoutPicture{%
      \begin{tikzpicture}[overlay,remember picture]
        \draw[\sigur@grid@color!30!white]
          (current page.south west) grid[step=1mm]
          (current page.north east);
        \draw[\sigur@grid@color!80!white]
          (current page.south west) grid[step=10mm]
          (current page.north east);
      \end{tikzpicture}
    }%
  }
\fi
\end{filecontents}
\documentclass{article}
\usepackage[grid=green]{sigur}
\begin{document}
foo
\end{document}
Related Question