166

What are some good tutorials on writing custom styles (classes) that clearly explain commands used and some of the features that TeX has to offer in the way of styles?

6
  • 1
    Are you asking about help for writing packages (in general), or specifically about layout/fonts/etc.? Commented Jul 29, 2010 at 10:59
  • 4
    @EricR - I'm shocked that this one has zero answers thus far. This is one question I thought would be featured! Sadly, I don't know of any! I'm on a quest to write a custom style (see tex.stackexchange.com/questions/615), and I'll let you know what I find, but I don't have an answer for you yet. Commented Jul 30, 2010 at 0:50
  • 2
    @Navarro, I'm asking for help writing packages in general, it's a quite broad question, perhaps something to help intermediate LaTeX users get started writing their own styles when the ones they have fail to meet their needs. @reemrevnivek, wonderful, thank you!
    – EricR
    Commented Jul 30, 2010 at 17:03
  • 1
    @ShreevatsaR - I meant featured in terms of the Area51 interface. I imagined that this would be a perfect example of "What kind of questions will be asked here." Commented Jul 30, 2010 at 20:33
  • 5
    I'd be tempted to stick with the LaTeX2e 'package'/'class' naming here. In this context, 'style' is really the LaTeX2.09 name (although the file extension still reflects the older use, of course).
    – Joseph Wright
    Commented Jul 31, 2010 at 16:08

7 Answers 7

81
5
  • 7
    Thank you, that's an excellent resource, with actual code included. Much appreciated.
    – EricR
    Commented Aug 3, 2010 at 21:03
  • 2
    Excellent work. Very helpful for beginners like myself. Thank you.
    – Heisenb0rg
    Commented Mar 4, 2011 at 19:38
  • Lots of fluff up until section 5.1, but helpful!
    – AdamO
    Commented Sep 24, 2012 at 19:58
  • 1
    The link has expired. How can I find this book now?
    – youthdoo
    Commented Dec 18, 2022 at 7:18
  • @youthdoo, I edited it to point to TUGboat. Commented Dec 18, 2022 at 20:44
45

While I cannot point to a pre-built 'how to' guide, I can at least try to provide some pointers here. The first point to remember is that LaTeX2e is rather weak at providing documented 'hooks' for customisation in the kernel. As a result, a lot has to be done by loading support packages.

At the most basic, a class is a collection of instructions to alter the standard formatting. Many simple classes therefore start out as little more than a customised preamble spun out into a separate file. Unless you are very experienced, it is likely that the best place to start a custom class is by loading one of the standard ones, for example

\LoadClass{article}

It's then possible to load a selection of packages (using \RequirePackage rather than \usepackage), to allow modification. Obvious examples here would be geometry, float, caption, various font packages, etc. Quite a lot can be achieved by simply bundling up a group of support packages and appropriate options into one file.

The next thing to consider is providing custom commands. These might be as simply as something like

\newcommand\subject[1]{%
  \begin{center}
    \bfseries
    #1%
  \end{center}
}

which I do in a slightly-customised letter class for my own use. Of course, what makes sense here will depend on what you want. Custom commands might of course depend on packages that you know will be loaded as they are included in the list for the class.

The next thing that people tend to do in custom classes is simple redefinition of LaTeX internals. The most obvious one is \@maketitle, which you may well want to modify. Remember that class files set @ as a letter, so \makeatletter is not needed. At this stage, modifications tend to be made by starting with the existing code and altering it. The LaTeX2e kernel classes (article.cls, report.cls, etc.) and the kernel itself (latex.ltx) are the first port of call for code to modify. Taking the \@maketitle example, the version from article.cls reads

\def\@maketitle{%
  \newpage
  \null
  \vskip 2em%
  \begin{center}%
  \let \footnote \thanks
    {\LARGE \@title \par}%
    \vskip 1.5em%
    {\large
      \lineskip .5em%
      \begin{tabular}[t]{c}%
        \@author
      \end{tabular}\par}%
    \vskip 1em%
    {\large \@date}%
  \end{center}%
  \par
  \vskip 1.5em}

So obvious changes are to alter the spacing or fonts.

Beyond that, you are in to serious (La)TeX programming, where I'd suggest writing some of the code as a package and requiring it from the class might be better. This is a complex subject, and I think is not the way to start out learning how to write class files!

1
  • 3
    Oh, so that's why one should use \makeatletter! Pretty good answer.
    – rberaldo
    Commented May 5, 2011 at 2:49
35
+25

The LaTeX Companion[1] has a chapter on writing package/class files.

EDIT: The table of content is available online. The relevant chapter is Appendix A and in particular Section A.4. The style of the chapter is pretty much the same as the style of the rest of the book (Chapter 3 is available online) with all needed commands explained in some details and with lots of examples (and counterexamples).

[1] F. Mittelbach and M. Goossens, The LaTeX Companion. Addison-Wesley 2004

0
26

I wrote an article on the LaTeX Community Forum on how to write a LaTeX class file. It's specifically about creating form-style documents, but there is some general class-writing stuff in there, if that's of any use.

Edit: there's also Writing a Class File for a Form which is developed from my LC article.

4
  • 1
    That's excellent! Very well documented and easy to read. Thank you for sharing
    – EricR
    Commented Jan 28, 2014 at 15:15
  • @NicolaTalbot I've tried to reach your article, but without success, it is currently a dead link. If it is still around, an update would be welcome.
    – gusbrs
    Commented Feb 16, 2019 at 12:37
  • @gusbrs It might be worth contacting the LaTeX Community Forum to find out where the LC articles are now located. Commented Mar 2, 2019 at 11:39
  • I had to disable that article software since it got hacked and I had to avoid that my provider blocks the whole site. I will see what I can do to bring them back online. I answered @gusbrs on latex.org.
    – Stefan Kottwitz
    Commented Mar 2, 2019 at 22:51
20

Flynn (2006) and clsguide?

3
  • Those are both good links about the elements of a new class, and they should certainly be on-hand when writing a new class or following a tutorial, but they don't provide much for someone who's never made one. They read too much like an API rather than a how-to. Commented Jul 31, 2010 at 15:31
  • I'm afraid that this is probably the best you will get. The issues of programming LaTeX are well-known, and make it very difficult to actually write a 'how to' document. This is one of the reasons for trying to provide a proper programming system within the LaTeX3 work.
    – Joseph Wright
    Commented Aug 1, 2010 at 7:38
  • These are more like the Java API, when we're looking for how-to tutorials. Commented Aug 1, 2010 at 20:38
10

If it's the syntax of writing macros that you're looking for I'd definitely recommend Tex By Topic

2
  • Thank you, but this is more slanted at online tutorials and writing .cls/.sty files.
    – EricR
    Commented Aug 3, 2010 at 21:04
  • If you want to do some tweaking or write a cls yourself, you will probably need some experience with low-level TeX as well. Commented Aug 7, 2010 at 10:54
8

The tutorial that helped me the most was Scott Pakins dtxTut. If you are a learn by example type then consider browsing the dtxGallery.

One should also mention the makedtx and sty2dtx packages, which assist with package creation, though I have not used these myself.

Personally I have always found it hard to find information on package writing, presumably my google fu was not up to scratch. This information does exist though. TeX.SE provides a rather complete list which may help you further.

One should also consider reading existing package sources for examples on macro writing for this there is texdoc PACKAGE. Simply punch this into the command line to see relevant documentation e.g. texdoc --view pgfmanual for the TikZ manual, texdoc glossaries for the glossaries package or better yet texdoc -l glossaries.

I do not know whether or not a best practices document exists for package writers. Something that say considers LaTeX3, ConTeXt and related.

3
  • 1
    texdoc glossaries-code should get you the documented code for glossaries (texdoc glossaries ought to show the user guide). Commented Apr 28, 2016 at 8:04
  • 1
    I was actually trying to hint that it opens a webpage providing complete listing of the glossaries documentation (User guide, annotated code and examples). Although that may just be on my system (MikTeX on Windows). As a side note : Thank you for the glossaries package it is really useful. Personally I have dtx'd my glossaries if anyone is interested I can post the template.
    – Carel
    Commented Apr 28, 2016 at 12:56
  • 1
    Okay, I need to use the -l switch to get the listing (TeXLive on Linux) so it seems like it's different for me. You're welcome :-) (Incidentally glossaries.dtx was created using makedtx.) Commented Apr 28, 2016 at 13:40

You must log in to answer this question.

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