64

I have a preamble that I copy and paste into nearly all of my documents. Is there a nice way to turn this into a package than I can use? I would prefer to do this in a way that doesn't depend on my Latex front end or compiler, so I can just \usepackage{coolpreamble}. This would also be useful for distributing to others to keep a standard LaTeX format amongst my research group.

1

5 Answers 5

63

You can put your preamble code into a .sty file and then use \usepackage{mystyle} to load it. Your .sty file will look like this:

% Declare that this style file requires at least LaTeX version 2e.
\NeedsTeXFormat{LaTeX2e}

% Provide the name of your page, the date it was last updated, and a comment about what it's used for
\ProvidesPackage{mystyle}[2010/07/28 BBischof's custom LaTeX style]

% Now paste your code from the preamble here

% Finally, we'll use \endinput to indicate that LaTeX can stop reading this file. LaTeX will ignore anything after this line.
\endinput

Note that style files can get much more complicated (if you want to include options like \usepackage[option]{mystyle}, etc.), but this basic format should get you started.

Save your file as mystyle.sty and put it in the current directory with your .tex file.

You can also take a look at the LaTeX2e for class and package writers guide which also available on ctan. It'll walk you through this in more detail.

3
  • 8
    I suggest you use \RequirePackage instead of \usepackage while inside the the package, that will prevent problems with double inclusions and such.
    – Giel
    Commented Aug 6, 2010 at 13:56
  • Good point, Giel. When you move the code from the preamble of your document to the style file, you can change all of the \usepackage commands to \RequirePackage (and leave the package name and options the same).
    – godbyk
    Commented Aug 11, 2010 at 19:13
  • @Giel: Actually \RequirePackage and \usepackage have the same definition most of the time. Before \documentclass the \usepackage is defined to be an error message (like package loaded before class), but is then defined to be like \RequirePackage. But you are right, people should use \ReuqirePackage inside packages and classes. Commented Dec 27, 2011 at 11:26
48

I’d say: don’t. There’s a plethora of thesis.sty packages on the web and I think they all miss one important point:

This is what classes are there for, not packages. So if you find yourself reusing the same preamble, chances are it’s for the same kind of documents. In that case, don’t make a package out of it – make a class.

The differences are minimal:

  • You need to call your file <foo>.cls instead of <foo>.sty
  • You (most probably) need to load a base class inside your class using \LoadClass
  • You load the class via \documentclass at the beginning of your documents, instead of using \usepackage.
7
  • 8
    I accepted the other answer because it was directly the answer to my question. However, this answer was very helpful to me and I think I will take your advice. Thanks for answering!
    – BBischof
    Commented Jul 26, 2010 at 23:50
  • @Konrad: wouldn't \LoadClass be better? Commented Jan 31, 2011 at 15:05
  • @Bruno: Better than \documentclass? Why? Commented Jan 31, 2011 at 15:12
  • @Konrad: I am just blindly following pp.9--10 of clsguide.pdf... But come to think about it, I don't know the difference. Commented Jan 31, 2011 at 15:38
  • 1
    @Konrad: here it is: run pdflatex with no input file, and as your first input type \show\documentclass. This will show \documentclass=macro:->\let \documentclass \@twoclasseserror \if@compatibility \else \let \usepackage \RequirePackage \fi \@fileswithoptions \@clsextension. In other words, the first occurrence of \documentclass redefines \documentclass to produce an error. Commented Jan 31, 2011 at 15:42
12

Putting your preamble in a .sty file, and changing \usepackage statements to RequirePackage, and closing off the file with \endinput is the most straightforward way.

It is important to realize that in a style file @ is a letter, meaning that it can be used in commands. So if you have code in your preamble between a \makeatletter and \makeatother pair, you can leave out these two commands in the .sty file.

Now, if you want to go a step further, you can specify what format is needed, a name and description of the package using \NeedsTeXFormat and \ProvidesPackage. Furthermore, you can define package options and process them using \DeclareOption and \ProcessOptions, but if you go that far, either learn from looking in well-made .sty files on CTAN or have a look at LaTeX2e for class and package writers.

2
  • 2
    Is the \endinput actually required? Commented Jul 29, 2010 at 5:53
  • I don't think it is required, but have been told it is good practice.
    – equaeghe
    Commented Jul 31, 2010 at 20:02
3

The easiest way would be to put it into a .sty file, and then having the other people simply include it. Admittedly, this is a bit of a hack, and you can be more sophisticated with it.

I recommend you look at another style file to kind of see the layout, like how to declare the package, etc.

3

I use a different approach other than a style file. I use Autohotkey, a small little hotkey expander. Every time I type ´std#` in any IDE, it gets expanded to

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs}
\usepackage{babel}
\usepackage{graphicx}
\usepackage{csquotes}
\usepackage{paralist}
\usepackage{xcolor}

The software can do much more, e.g. inserting (calculated) dates in different formats or moving the cursor after expansion. The definition file is in my dropbox so changes go to all my machines more or less immediately.

You must log in to answer this question.

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