Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

5 Answers

up vote 33 down vote accepted

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. It'll walk you through this in more detail.

share|improve this answer
4  
I suggest you use \RequirePackage instead of \usepackage while inside the the package, that will prevent problems with double inclusions and such. –  Giel Aug 6 '10 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 Aug 11 '10 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. –  Martin Scharrer Dec 27 '11 at 11:26
add comment

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.
share|improve this answer
5  
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 Jul 26 '10 at 23:50
    
@Konrad: wouldn't \LoadClass be better? –  Bruno Le Floch Jan 31 '11 at 15:05
    
@Bruno: Better than \documentclass? Why? –  Konrad Rudolph Jan 31 '11 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. –  Bruno Le Floch Jan 31 '11 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. –  Bruno Le Floch Jan 31 '11 at 15:42
show 2 more comments

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.

share|improve this answer
2  
Is the \endinput actually required? –  Will Robertson Jul 29 '10 at 5:53
    
I don't think it is required, but have been told it is good practice. –  equaeghe Jul 31 '10 at 20:02
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.