6

What is the best practice for separating the preamble to a dedicated file?

I've seen two variants:

  1. A package: preamble.sty and \usepackage{preamble}
  2. A .tex file: preamble.tex and \input{preamble}

Which one is to be preferred?

David Carlisle
  • 757,742
Tim Hoffmann
  • 11,159
  • To be honest, the one that you like best :-) Either way is ok and good practice, though i prefer to write my own packages (with options and stuff). Having said that, if you want to implement any options, write a package :-) – Johannes_B Sep 02 '14 at 20:06
  • I think the package version is logically more consistent, although in the absence of options the definition of \usepackage is more or less \input{#1.sty} so it doesn't make any difference in practice. – David Carlisle Sep 02 '14 at 20:12
  • Definitely the separate .sty file option: In a style file that's loaded with a \usepackage statement, the @ ("at") character is immediately of category "letter", which makes all those \makeatletter and \makeatother statements unnecessary. – Mico Sep 02 '14 at 20:21

1 Answers1

7

My opinion mostly:

Preamble commands that add functionality to the document (e.g., defining macros) belong in a package (.sty file).

Preamble commands that define the look of the document (paper geometry, page headers, font selections), belong in a custom document class (.cls file). You can use high-level LaTeX commands to derive a custom class from a standard-issue class, and pass options to it.

See Style/class tutorials for examples of both.


Update 2022-03-27 User @tush asked for a reference. The closest I could find is LaTex2e for class and package writers by the LaTeX project team. They write:

2.3 Is it a class or a package?

The first thing to do when you want to put some new LaTeX commands in a file is to decide whether it should be a document class or a package. The rule of thumb is:

If the commands could be used with any document class, then make them a package; and if not, then make them a class.

There are two major types of class: those like article, report or letter, which are free-standing; and those which are extensions or variations of other classes—for example, the proc document class, which is built on the article document class. Thus, a company might have a local ownlet class for printing letters with their own headed note-paper. Such a class would build on top of the existing letter class but it cannot be used with any other document class, so we have ownlet.cls rather than ownlet.sty.

The graphics package, in contrast, provides commands for including images into a LaTeX document. Since these commands can be used with any document class, we have graphics.sty rather than graphics.cls.

Commands that set the document's look like paper geometry, page headers, font selections, seem closer to the ownlet case than to the graphics case. That's why I think they belong in a class rather than a package.

Matthew Leingang
  • 44,937
  • 14
  • 131
  • 195
  • Is it ok to place .sty files in the local folder next to the .tex document (if it contains really document-specific code) or should .sty files always be in the appropriate TEXMFHOME subfolder? – Tim Hoffmann Sep 17 '14 at 20:30
  • @TimHoffmann: either are perfectly acceptable. But if you are putting auxiliary code in a package you will probably want to use it more than once. That suggests a central location. – Matthew Leingang Sep 18 '14 at 00:47
  • @MatthewLeingang Could you refer me to where it is said that the commands related to the look of the document should be in a .cls file? – tush Mar 24 '22 at 12:45
  • 1
    @tush Like I said at the beginning, it's my opinion so I don't know if these words are written elsewhere. But I added a reference which I think supports it. – Matthew Leingang Mar 27 '22 at 19:52