Document ClassesOption [natbib] Declaration

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
Ethan Phan
Posts: 3
Joined: Tue Mar 31, 2015 10:58 am

Option [natbib] Declaration

Post by Ethan Phan »

Hi everyone,
I'm currently working on building a new class file based on an old one which is my university thesis class. And I am totally new user for such a work.

Here is the code in the old file named "muthesis09.cls"

Code: Select all

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{muthesis09}[2010/04/24 v0.91 Mahidol University thesis class]
\LoadClass[12pt,a4paper]{report}

\RequirePackage{times}
\RequirePackage{ifthen}

\newboolean{@natbibmode}\setboolean{@natbibmode}{false}
\DeclareOption{natbib}{%
  \setboolean{@natbibmode}{true}%
}
\ProcessOptions\relax
\if@natbibmode
  \RequirePackage{natbib}% this cannot go inside \DeclareOption argument
  \bibpunct{(}{)}{;}{a}{}{,}%
\else
\fi
I think this code is to declare the option @natbibmode, execute, then ask if it is a require package; at last, changing the way of producing the bibliography using a natbib package command \bibpunct.

Now suppose there is a "new way" to achieve the aiming work as listed above---my own code:

Code: Select all

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{muthesis15}[2015/03/09 My custom muthesis]
% below are some packages needed to use:
% package IFTHEN for coding this cls file 
\RequirePackage{ifthen}

\DeclareOption{natbibmode}
	{\newboolean{natbibmode}%
	 \setboolean{natbibmode}{true}}
\ifthenelse
        {\boolean{natbibmode}}
        {
         \RequirePackage{{natbib}
         \bibpunct{(}{)}{;}{a}{}{,}
        }
        {}
% package NATBIB for using the bibliography enviroment more effective
\PassOptionToClass{report}
% the typesetting thesis will made use of: 12pt (font size), A4 paper;
%     and natbib for the bibliography (references)
\LoadClass[12pt,a4paper]{report}
then that raises questions:
  1. Why in the old code the author need to set the false value boolean to @natbibmode? Why didn't he just creat it and then set the value to be true later in the \DeclareOption command? Alternative, why didn't he set it to be true at the time he created that boolean?
  2. From the above questions, I have my own code. Is it right?
  3. What is the difference between \ExecuteOptions and \ProcessOptions commands? Do I need any of them in the above code?
  4. (Side question) Do I need to include the special character @ at the very begining of each command or counter convention name? The reason of this question is, in the file muthesis09, the author then put @ in names of every counter and every command name that made used(!!!), and his action make me headache to classify which one is LaTeX primitive (or TeX primitive) and which one is his code :!:
If possible, please let me know a better code to achieve the aim. Thank you.

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

Option [natbib] Declaration

Post by Johannes_B »

Hi and welcome,

this is quite a bunch of question you are asking here. In my experience, thesis classes from universities are very very bad maintained. A few weeks back i was confronted with a class that was using some code from the beginnings of LaTeX2e, around 1993.

First of all, though many journals still use the old system of BibTeX in conjunction with packages like natbib, there is a new package on the horizon, called biblatex.


Writing packages and classes is something for experienced users. You should definitely have read LaTeX2e for class and package authors. This will get rid of some of your questions and ensure a stable document.
In current documents, @ is not a letter, and hence the user has to take care to redefine those commands, which is done by the \makeatletter and \makeatother combo.


A completely different approach would be using the new programming interface of LaTeX3.


No matter which way you choose, use some freely accessible repository, on github and the like. This makes contribution by others, in form of bug reports and other issues, as well as suggestions for improbvements, much more easy.


As far as i can tell by a quick look, the code cannot work. The boolean flag for natbib will only be defined, if the option is given. If not, the test for true or false will result in the boolean flag not defined, if natbib-option was not given.
Please also have a look at package etoolbox that makes work with bools easier.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Ethan Phan
Posts: 3
Joined: Tue Mar 31, 2015 10:58 am

Re: Option [natbib] Declaration

Post by Ethan Phan »

Thank you for your answer and suggestion ( and also, your warm welcome, too :) ), Johannes.
I will read and look up carefully in those you mentioned.
For the CLSGuide, I'm on the way of reading it, but right now I stop a while to read "LaTeX: A Document Preparation System" to understand some `usual concepts', such as moving argument, terminal, robust vs. fragile commands, etc.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10321
Joined: Mon Mar 10, 2008 9:44 pm

Re: Option [natbib] Declaration

Post by Stefan Kottwitz »

Hello Ethan,

welcome to the forum!

If you would like to talk about such LaTeX concepts, here is the right place. :-)

Stefan
LaTeX.org admin
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: Option [natbib] Declaration

Post by Johannes_B »

I have to be honest that i never read Leslie Lamports book.

LaTeX is a bit like cars over the years ... Whereas Lamports design was a breakthrough, a lot of advancements and enhancements were made over the years, making some of the stuff he wrote obsolete, thoug still true in principle.


But its great that you keep on hanging with LaTeX.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Option [natbib] Declaration

Post by cgnieder »

Ethan Phan wrote:Here is the code in the old file named "muthesis09.cls"

Code: Select all

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{muthesis09}[2010/04/24 v0.91 Mahidol University thesis class]
\LoadClass[12pt,a4paper]{report}

\RequirePackage{times}
\RequirePackage{ifthen}

\newboolean{@natbibmode}\setboolean{@natbibmode}{false}
\DeclareOption{natbib}{%
  \setboolean{@natbibmode}{true}%
}
\ProcessOptions\relax
\if@natbibmode
  \RequirePackage{natbib}% this cannot go inside \DeclareOption argument
  \bibpunct{(}{)}{;}{a}{}{,}%
\else
\fi
I think this code is to declare the option @natbibmode, execute, then ask if it is a require package; at last, changing the way of producing the bibliography using a natbib package command \bibpunct.
This declares a class option natbib (\DeclareOption{natbib}) which does nothing but setting the boolean flag @natbibmode to true. This flag is defined earlier (\newboolean{@natbibmode}) and set to false (which is redundant at that state because the default state of a boolean is false but it doesn't hurt either). After the options have been processed (\ProcessOptions) the state of the boolean is checked and the package natbib is loaded and \bibpunct is used if the state is true.
Ethan Phan wrote:Now suppose there is a "new way" to achieve the aiming work as listed above---my own code:

Code: Select all

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{muthesis15}[2015/03/09 My custom muthesis]
% below are some packages needed to use:
% package IFTHEN for coding this cls file 
\RequirePackage{ifthen}

\DeclareOption{natbibmode}
	{\newboolean{natbibmode}%
	 \setboolean{natbibmode}{true}}
\ifthenelse
        {\boolean{natbibmode}}
        {
         \RequirePackage{{natbib}
         \bibpunct{(}{)}{;}{a}{}{,}
        }
        {}
% package NATBIB for using the bibliography enviroment more effective
\PassOptionToClass{report}
% the typesetting thesis will made use of: 12pt (font size), A4 paper;
%     and natbib for the bibliography (references)
\LoadClass[12pt,a4paper]{report}
This defines the boolean flag if (and only if) the class option natbibmode is used by the user. I don't know how forgiving the ifthen package and its \ifthenelse command is when tested for an undefined boolean but I'd consider it bad practice. It's probably safe with ifthen (if it is forgiving) since this package is not very likely to get further updates but in other testing methods one might expect an error in such a case.

I'd rather use

Code: Select all

\newboolean{natbibmode}
\DeclareOption{natbibmode}{\setboolean{natbibmode}{true}}
Other then that the code looks fine to me.
Ethan Phan wrote:What is the difference between \ExecuteOptions and \ProcessOptions commands? Do I need any of them in the above code?
Is this clear now from LaTeX2e for class and package writers
(clsguide)? Or are there questions left?
Ethan Phan wrote:Do I need to include the special character @ at the very begining of each command or counter convention name? The reason of this question is, in the file muthesis09, the author then put @ in names of every counter and every command name that made used(!!!), and his action make me headache to classify which one is LaTeX primitive (or TeX primitive) and which one is his code :!:
Johannes already said that the usual state (the category code) of @ in a document usually is not “letter” (category code 11) but “other” (category code 12). However, due to how packages and classes are loaded it is a letter in class and package files. It is a broad convention to use it in internal macro names that are not to be used by users (or let's say: not if they don't know what they're doing). Modifying package and class internals thus usually requires \makeatletter (for making @ a “letter”) and \makeatother (for making it “other” again).

Personally I use prefixes for all macros in my packages. For example in my translations package every macro starts with \@trnslt@ except for the user commands.

Regards
site moderator & package author
Post Reply