LaTeX forum ⇒ OthersHandling of passed Option in a custom Package Topic is solved

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

Handling of passed Option in a custom Package

Postby mas » Tue Dec 18, 2012 3:59 am

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:
  1. \documentclass{article}
  2. \usepackage[color]{myown}
  3.  
  4. \begin{document}
  5. some text \ldots
  6. \end{document}

In the "myown" package, I would like to take different actions based on the parameter "color" like
  1. if color then
  2. \newcommand\headcol{\color{red}}
  3. ...
  4. else
  5. \newcommand\headcol{\color{black}}
  6. ...

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

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

Postby Johannes_B » Tue Dec 18, 2012 8:39 am

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: 204
Joined: Thu Dec 04, 2008 4:39 am

Postby mas » Tue Dec 18, 2012 1:02 pm

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:
  1. \documentclass[svgnames,x11names]{article}
  2. \usepackage[color]{myown}
  3.  
  4. \begin{document}
  5. Hello.
  6. \end{document}


The package file:
  1. \NeedsTeXFormat{LaTeX2e}[1999/12/01]
  2. %
  3. \ProvidesPackage{myown}[2012/12/18]
  4. %
  5. \RequirePackage[a5paper,body={10cm,17cm},headheight=15.2pt,centering]{geometry}
  6. \RequirePackage[a5,center,off]{crop}
  7. \RequirePackage{fancyhdr,comment}
  8. \RequirePackage[final]{graphicx}
  9. \RequirePackage{tocloft,eqlist,enumitem,pifont,setspace,calc}
  10. \RequirePackage{tikz,tfrupee,titlesec,paralist,textcomp,everyshi}
  11. \RequirePackage{hyperref}
  12.  
  13. %
  14. \DeclareOption{color}{%
  15. % Color
  16. \newcommand{\CHPCOL}{Firebrick3}
  17. \newcommand{\RTITLCOL}{Blue}
  18. \newcommand\LOGO{logo-col}
  19. }
  20.  
  21. \DeclareOption{%
  22. % Mono
  23. \newcommand{\CHPCOL}{black}
  24. \newcommand{\RTITLCOL}{black}
  25. \newcommand\LOGO{logo}
  26. }
  27.  
  28. \ProcessOptions\relax


When I tried this with
  1. pdflatex test.tex
  2. ...
  3. (/home/software/texlive/2012/texmf-dist/tex/latex/hyperref/hpdftex.def
  4. (/home/software/texlive/2012/texmf-dist/tex/latex/oberdiek/rerunfilecheck.sty))
  5.  
  6. ! Undefined control sequence.
  7. \GenericError ...
  8. #4 \errhelp \@err@ ...
  9. l.27
  10.  
  11. ? x
  12. 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: 1974
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany
Contact:

Postby cgnieder » Tue Dec 18, 2012 9:19 pm

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

  1. \DeclareOption{mono}{%
  2. % Mono
  3. \newcommand{\CHPCOL}{black}
  4. \newcommand{\RTITLCOL}{black}
  5. \newcommand\LOGO{logo}
  6. }


Regards
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

mas
Posts: 204
Joined: Thu Dec 04, 2008 4:39 am

Postby mas » Wed Dec 19, 2012 2:49 am

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

  1. \DeclareOption{mono}{%
  2. % Mono
  3. \newcommand{\CHPCOL}{black}
  4. \newcommand{\RTITLCOL}{black}
  5. \newcommand\LOGO{logo}
  6. }


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
  1. \ExecuteOptions{color,mono}

with the same result. Adding
  1. \errorcontextlines=999

gives the following output
  1. \GenericError ...
  2. \endgroup
  3. \ds@mono ->\newcommand {\CHPCOL }{black}
  4. \newcommand {\RTITLCOL }{black} \ne...
  5.  
  6. \@forloop ...e #5\def #4{#2}\ifx #4\@nnil \else #5
  7. \@iforloop #3\@@ #4{#5}\fi...
  8.  
  9. \@for ...fter \@forloop #2,\@nil ,\@nil \@@ #1{#3}
  10. \fi
  11. \reserved@a ...name ds@\CurrentOption \endcsname }
  12. \edef \CurrentOption {#1}
  13. l.28 \ExecuteOptions{color,mono}
  14.  
  15. ? x
  16. No pages of output.

Regards.

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

User avatar
cgnieder
Site Moderator
Posts: 1974
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany
Contact:

Postby cgnieder » Wed Dec 19, 2012 2:58 am

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).
  1. \usepackage[color,mono]{myown}
can't work, though, as both options define the same commands with \newcommand so you'd get
  1. ! LaTeX Error: Command \CHPCOL already defined.


Maybe you can elaborate a bit on what it is you're trying to achieve?

Regards
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

mas
Posts: 204
Joined: Thu Dec 04, 2008 4:39 am

Postby mas » Wed Dec 19, 2012 10:19 am

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


Return to “Others”

Who is online

Users browsing this forum: No registered users and 1 guest