3

I've learnt how to use LaTeX at the user level, but wanted to package the recurring code (resulting mostly from the styling of the cover) in a class file. Therefore, I tried the following simple example class file:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{test}[2018/10/12 v0.1 Test class]
\PassOptionsToClass{12pt}{report}
\ProcessOptions
\LoadClassWithOptions{report}

Although this works if I pass the size I want, if I omit it, it defaults to 10pt, instead of 12pt that I want. Do you know how to make the default become the 12pt, and still work if I decide to give it another size option?

Alan Munn
  • 218,180
tigre200
  • 191

1 Answers1

2

From here:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{test}[2018/10/12 v0.1 Test class]
\def\@@ptsize{12pt}
\DeclareOption{10pt}{\def\@@ptsize{10pt}}
\DeclareOption{11pt}{\def\@@ptsize{11pt}}
\DeclareOption{12pt}{\def\@@ptsize{12pt}}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{report}}
\ProcessOptions\relax
\LoadClass[\@@ptsize]{report}

Then it works as you expect with a main file like:

\documentclass{test}

or

\documentclass[11pt]{test}

and:

\begin{document}
\chapter{Test}
\section{test}
Test

\end{document}
koleygr
  • 20,105
  • 1
    What are the @@ in @@ptsize? – tigre200 Oct 14 '18 at 17:31
  • And isn't there a way that avoids re-declaring all the font size options? – tigre200 Oct 14 '18 at 17:34
  • nothing special... This character works usually inside in \makeatletter and \makeatother commands and will not work in the simple preamble or document... This way the author of the package/class, can add a protection for his command/or variable to be somehow more difficult to change its value someone by mistake etc – koleygr Oct 14 '18 at 17:36
  • 1
    The only accepted values for report are these three values.... So,it is just a declaration that will be used later to add our default value and to keep the other values still available by saving them in the same variable we will finally give to our class.... It could possibly avoided with hacks that would make the code much more difficult (like looking for options that finish in "pt" and adding commands to use them). – koleygr Oct 14 '18 at 17:40
  • But now if I want to change, for example, to a4paper, then I will have to do the same thing for the types of paper, right? Isn't this solution very brittle? – tigre200 Oct 14 '18 at 17:47
  • I see what you mean... I left these options there as it was in the original post to show you that you can also have some values there... Removed them now and I thing this does what you want after the last comment – koleygr Oct 14 '18 at 17:55
  • @tigre200... this have to be done only for the non default options... the default will be given anyway to the report... If you have to change the default options, then I think you can not avoid these (or more complex) kind of code – koleygr Oct 14 '18 at 17:57
  • Now, I am trying to think better, and I think you can give just some default options... but what if you don't want to use these options and change them (the user of the class)... If you don't want the user to ba able to change them... then the things are simple (like the post before editing) – koleygr Oct 14 '18 at 18:04