OthersHandling of passed Option in a custom Package

Information and discussion about other tools not listed above.
Post Reply
mas
Posts: 226
Joined: Thu Dec 04, 2008 4:39 am

Handling of passed Option in a custom Package

Post by mas »

I have a collection of commands in a sty file, say myown.sty, which I call from my document. What I want to do is use the following construct:

Code: Select all

\documentclass{article}
\usepackage[color]{myown}

\begin{document}
  some text \ldots
\end{document}
In the "myown" package, I would like to take different actions based on the parameter "color" like

Code: Select all

if color then
  \newcommand\headcol{\color{red}}
  ...
else
  \newcommand\headcol{\color{black}}
  ...
Many pages on the net talk about handling options to a class file. I need a simpler way of just looking at the option and take appropriate action. I would like some pointers to achieve this.

OS: Debian/GNU Linux; LaTeX System : TeXLive; Editor : Vim

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Handling of passed Option in a custom Package

Post by Johannes_B »

Hi,

you could have a look at »LaTeX2e for Class and Package writers«. The LaTeX Companion is also very good to learn how to write classes and packages.

Give it a try.


Regards
Johannes_B
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
mas
Posts: 226
Joined: Thu Dec 04, 2008 4:39 am

Handling of passed Option in a custom Package

Post by mas »

Johannes_B wrote: you could have a look at »LaTeX2e for Class and Package writers«.
Thanks, I already had given that a look, but found some difficulty in getting what I wanted. I have now some MWE which gives me the error. It might be due to my improper understanding of the option processing in a style/package file.

The main file:

Code: Select all

\documentclass[svgnames,x11names]{article}
  \usepackage[color]{myown}

\begin{document}
 Hello. 
\end{document}
The package file:

Code: Select all

\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%
\ProvidesPackage{myown}[2012/12/18]
%
\RequirePackage[a5paper,body={10cm,17cm},headheight=15.2pt,centering]{geometry}
\RequirePackage[a5,center,off]{crop}
\RequirePackage{fancyhdr,comment}
\RequirePackage[final]{graphicx}
\RequirePackage{tocloft,eqlist,enumitem,pifont,setspace,calc}
\RequirePackage{tikz,tfrupee,titlesec,paralist,textcomp,everyshi}
\RequirePackage{hyperref}

%
\DeclareOption{color}{%
% Color 
  \newcommand{\CHPCOL}{Firebrick3}
  \newcommand{\RTITLCOL}{Blue}
  \newcommand\LOGO{logo-col}
}  

\DeclareOption{%
% Mono 
  \newcommand{\CHPCOL}{black}
  \newcommand{\RTITLCOL}{black}
  \newcommand\LOGO{logo}
}

\ProcessOptions\relax
When I tried this with

Code: Select all

pdflatex test.tex
...
(/home/software/texlive/2012/texmf-dist/tex/latex/hyperref/hpdftex.def
(/home/software/texlive/2012/texmf-dist/tex/latex/oberdiek/rerunfilecheck.sty))

! Undefined control sequence.
\GenericError  ...                                
                                                    #4  \errhelp \@err@     ...
l.27 
     
? x
No pages of output.
Hope to get some pointers to understand the options better.

OS: Debian/GNU Linux; LaTeX System : TeXLive; Editor : Vim
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Handling of passed Option in a custom Package

Post by cgnieder »

You're simply missing the option name for your second option. I guess it should read:

Code: Select all

\DeclareOption{mono}{%
% Mono
  \newcommand{\CHPCOL}{black}
  \newcommand{\RTITLCOL}{black}
  \newcommand\LOGO{logo}
}
Regards
site moderator & package author
mas
Posts: 226
Joined: Thu Dec 04, 2008 4:39 am

Handling of passed Option in a custom Package

Post by mas »

cgnieder wrote:You're simply missing the option name for your second option. I guess it should read:

Code: Select all

\DeclareOption{mono}{%
% Mono
  \newcommand{\CHPCOL}{black}
  \newcommand{\RTITLCOL}{black}
  \newcommand\LOGO{logo}
}
Tried that and got the same error. My understanding of the DeclareOption* was that it would be, sort of, default behaviour if nothing is mentioned on \usepackage line.

I even included

Code: Select all

\ExecuteOptions{color,mono}
with the same result. Adding

Code: Select all

\errorcontextlines=999
gives the following output

Code: Select all

\GenericError  ...                                
                                                  \endgroup 
\ds@mono ->\newcommand {\CHPCOL }{black}
                                         \newcommand {\RTITLCOL }{black} \ne...

\@forloop ...e #5\def #4{#2}\ifx #4\@nnil \else #5
                                                  \@iforloop #3\@@ #4{#5}\fi...

\@for ...fter \@forloop #2,\@nil ,\@nil \@@ #1{#3}
                                                  \fi 
\reserved@a ...name ds@\CurrentOption \endcsname }
                                                  \edef \CurrentOption {#1}
l.28 \ExecuteOptions{color,mono}
                                
? x
No pages of output.
Regards.

OS: Debian/GNU Linux; LaTeX System : TeXLive; Editor : Vim
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Handling of passed Option in a custom Package

Post by cgnieder »

mas wrote:Tried that and got the same error.
Then you used a different sty file than the one you posted because it works there (I tried it before I answered).

Code: Select all

\usepackage[color,mono]{myown}
can't work, though, as both options define the same commands with \newcommand so you'd get

Code: Select all

! LaTeX Error: Command \CHPCOL already defined.
Maybe you can elaborate a bit on what it is you're trying to achieve?

Regards
site moderator & package author
mas
Posts: 226
Joined: Thu Dec 04, 2008 4:39 am

Handling of passed Option in a custom Package

Post by mas »

cgnieder wrote: Then you used a different sty file than the one you posted because it works there (I tried it before I answered).
It was the same thing except for the addition of \errorcontextlines=999 which I had included to get a detailed error message.

I removed that and it is working now.
Maybe you can elaborate a bit on what it is you're trying to achieve?
I thought it was a bit obvious :-) I wanted the chapter titles and the running titles in color for the screen version and in black for the print version.

Thanks for the help.

Regards.

OS: Debian/GNU Linux; LaTeX System : TeXLive; Editor : Vim
Post Reply