2

I'm looking to write my own class file for a couple of reasons, but my main interest is better understanding LaTeX and how it works. I understand that I likely don't need to define my own class for whatever I want to do, but I'm interested in learning the A to Z on what a LaTeX document needs in order to compile correctly (and with minimal fragility).

The resources I've found so far for creating a class seem to be leaving out key information. Overleaf's guide seems to indicate that you only need the \NeedsTeXFormat and \ProvidesClass commands in order to have a working class. Through trial and error, I've also found that \normalsize also needs to be defined.

Other resources and examples I've found use the \LoadClass command, which I'm sure copies over common settings and commands that make adding onto the class easier, but I'm not finding resources that otherwise don't do this.

Here's the code I have so far:

%%% myclass.cls %%%

% Required per Overleaf
% https://www.overleaf.com/learn/latex/Writing_your_own_class
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myclass}[2023/01/31 Version 0.1 by Me]

% Required Font Sizes
\renewcommand{\normalsize}{10pt}
%%% example.tex %%%
\documentclass{myclass}

\begin{document}
   Hello World!
\end{document}

This produces the error:

example.tex:4: LaTeX Error: Missing \begin{document}.

My understanding is that something is missing from the class file that doesn't allow the compiler to see the contents of example.tex, but I'm not sure what it is. Searching for this error comes up with a lot of results to work around of poorly-written packages or fix beginner errors, but little to no results on how to write good packages (or classes).

Any help or resources is appreciated!

2
  • 3
    the default definition of \normalsize is something like \renewcommand\normalsize{\fontsize{10pt}{12pt}\selectfont}, so it shouldn't print something but select a font. See the minimal.cls and article.cls for small classes. Jan 31, 2023 at 19:44
  • @UlrikeFischer - Thanks, this fixed it! Jan 31, 2023 at 20:32

2 Answers 2

6

If you look at the definition of \normalsize you have:

$ latexdef normalsize                                            

\normalsize:
macro:->\protect \normalsize  


\normalsize :
\long macro:->\@setfontsize \normalsize \@xpt \@xiipt \abovedisplayskip 10\p@ \@plus 2\p@ \@minus 5\p@
 \abovedisplayshortskip \z@ \@plus 3\p@ \belowdisplayshortskip 6\p@ \@plus 3\p@ \@minus 3\p@ \belowdisplayskip \abovedisplayskip \let 
\@listi \@listI 

You are changing it to the text "10 pt" which, when called before the \begin{document} by the format, creates an unexpected text that give the error.

1
  • Thanks, that was the issue. The fixed implementation is using \renewcommand\normalsize{\fontsize{10pt}{12pt}\selectfont} instead. Jan 31, 2023 at 20:35
0

Though the Overleaf Guide you cite seems to be quite good, I still prefer rereading "The LaTeX Companion" for this purpose.

First, it provides a nice structure, which I incorporate as comments in whatever class I might be trying, and which is a bit unclear to my taste from said guide:

% ~~~ identification ~~~~~~~~~

% ~~~ initial code ~~~~~~~

% ~~~ declaration of options ~~~~~~~~~

% ~~~ execution of options ~~~~~~

% ~~~ package loading ~~~~~~~~~~

% === main code ==========

Second, it's important to understand/recall the logic behind. Sticking to this order minimizes trouble to my experience.

Third, it clarifies special commands to be used with package and/or class files.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .