Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
We do not usually put the Thanks in advance, right? Remember? –  azetina Nov 9 '13 at 18:45
    
@azetina, edited. –  Sigur Nov 9 '13 at 18:54

2 Answers 2

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}
share|improve this answer
    
I'm trying to adjust your code to test first if the package pgfopts is installed. I'm using \IfFileExists{pgfopts.sty} {\RequirePackage{pgfopts}% \pgfkeys{ sigur/.cd , grid/.code = \@sigur@gridtrue \renewcommand*\sigur@grid@color{#1} , grid/.default = red % if no value is given } \ProcessPgfOptions{/sigur} } {\PackageWarning{sigur}{You requested 'pgfopts.sty' %MessageBreak which doesn't exist on your system} } but I'm getting errors. –  Sigur Nov 9 '13 at 21:37
    
Let me guess: ! Illegal parameter number in definition of? If that's the case you need to double the #: \renewcommand*\sigur@grid@color{##1} –  cgnieder Nov 9 '13 at 21:43
    
No, \RequirePackage or \LoadClass in Options Section. –  Sigur Nov 9 '13 at 21:44
    
@Sigur that's unrelated to the \IfFileExists addition. Do you still have a \ProcessOptions in addition to \ProcessPgfOptions or something? (This is only random guesswork, of course...) –  cgnieder Nov 9 '13 at 21:48
    
Should I use both ProcessPgfOptions and \ProcessOptions? –  Sigur Nov 9 '13 at 21:50

Since your question is tagged , here's an answer using that package.

\RequirePackage{filecontents}
\begin{filecontents}{sigur.sty}
\ProvidesPackage{sigur}[2013/11/09 v0.0 a sample package]

\RequirePackage{kvoptions}

\newif\ifsigur@grid
\DeclareStringOption{grid}[red]
\AddToKeyvalOption*{grid}{\sigur@gridtrue}

\ProcessKeyvalOptions*

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

I've added three possible calls to the sigur package. The optionless one will not draw any grid (but the .aux file should be removed before processing if the option grid had been specified in a previous run).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.