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 defined a documentclass in which I redefine a heading format with (using the package titlesec):

\titleformat{name=\section} 

This works.

But if I move this \titleformat{name=\section} to a block \DeclareOption, then it does not compile. It say that all the commands in the \DeclareOption are already defined.

On the other hand, this format declaration in the \DeclareOption block works if I use the syntax \titleformat{name=\section}

Is it a bug?

The point is that I want to use two arguments {name=\section, numberless=true}; this is why I want to have this syntax.

[EDIT] Thanks for your post. So here would be an example of the "bug" : documentclass file maclasse.cls :

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{maclasse}[2011/12/19 maclasse]
\LoadClass{article}
\RequirePackage[explicit]{titlesec}

% Sectioning
%% BLOC 1
\titleformat{\section}%
   {\large\sffamily\bfseries}%
   {lasection \arabic{section}}%
   {0.5em}%
   {#1}% 

%% BLOC 2
\titleformat{name=\section,numberless=true}%
   {\sffamily\bfseries}%
  {}%
   {0.5em}%
   {#1}



\DeclareOption{opt}{


}

\ProcessOptions

A test file with

\documentclass[opt]{maclasse}

\begin{document}
\section{abc}
abc
\section*{def}
def

\end{document}

It compiles.

  • Now, move BLOC1 into the \DeclareOption{opt} -> it works.

  • Instead, move BLOC2 into \DeclareOption{opt} -> Compilation fails :

    ! LaTeX Error: \RequirePackage or \LoadClass in Options Section.

What do i do wrong ?

thanks

nicolas

share|improve this question
    
Welcome to TeX.sx! You can mark code by selecting it and hitting Ctrl + K (or clicking the {} button above the text field). Separate lines will be indented with four spaces, inline code enclosed in backticks (`). –  Torbjørn T. Dec 19 '11 at 13:27
    
Welcome to TeX.SX! It would be better if you provide an example of what you're trying to do in your class file. –  egreg Dec 19 '11 at 13:29
add comment

1 Answer

The class should be written differently:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{maclasse}[2011/12/19 maclasse]

% Sectioning
\newif\if@maclasseopt
\DeclareOption{opt}{\@maclasseopttrue}

\ProcessOptions\relax
\LoadClass{article}

\RequirePackage[explicit]{titlesec}
\if@maclasseopt
\titleformat{name=\section}%
  {\large\sffamily\bfseries}%
  {lasection \arabic{section}}%
  {0.5em}%
  {#1}
\titleformat{name=\section,numberless=true}%
  {\sffamily\bfseries}%
  {}%
  {0.5em}%
  {#1}%
\fi

It's better not to load much code into a \DeclareOption command, usually one does with a conditional. Moreover, it's not possible to load titlesec before the main class, so the loading must be deferred.

Most important, \LoadClass must go after \ProcessOptions.

share|improve this answer
    
ok, i will try. But i still do not quite understand why the argument {name=\section} makes a problem, and not {section}... –  nicolas roy Dec 20 '11 at 12:11
    
Ok, finally, i come to test your proposition. Ma class is actually already rather big, and therefore i have to do it carefully, with all the copy paste. Nevertheless, i have a doubt : you write "\LoadClass must go after \ProcessOptions". But actually, i have a \LoadClassWithOptions{scrartcl} at the begining of my class, because in particular, i want the option "draft" to be passed to the class scrartcl. I'm afraid, that loading the class after the \ProcessOptions will cause some problem. What do you think ? –  nicolas roy Jan 2 '12 at 10:18
    
Oh, and please tell me where i should put my \RequirePackage{} ? –  nicolas roy Jan 2 '12 at 10:22
    
@nicolasroy It's quite difficult to tell something with only this information available. –  egreg Jan 2 '12 at 10:28
    
I'm asking here about the standard structure of a document class. In which order should i write the following blocs : *\LoadClassWithOptions , *\DeclareOption , *ProcessOptions, *Requirepackage ? –  nicolas roy Jan 2 '12 at 10:37
add comment

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.