39

Since I am using (almost) the same set up for my tex documents, I wonder if there is a way to create a preamble with my default packages and to recall it anytime I want to create a tex document.

If yes, how I can call this "default" preamble?

Thanks in advance

  • 1
    IMHO, that is a matter of TeX editor. For instance, Emacs with YASnippet, or TeXstudio, that provides templates (and you can create yours) which are easy to load. – Denis Bitouzé Nov 25 '15 at 6:29
41
+150

An option is to create a package (sty file) that consists to use your default package:

\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{mypreamble}
  [2015/11/18 MyPreamble]

\RequirePackage{graphicx} % Put the package you need as required package

\endinput

Instead of putting \usepackage, you must use \RequirePackage to use them. You can also write \newcommand and so.

You can acces it by following the answers from this question: Where do I place my own .sty or .cls files, to make them available to all my .tex files?

You can call in your document \usepackage{mypreamble} and voila:

\documentclass{article}
\usepackage{mypreamble}

\begin{document}

With my own preamble

\includegraphic{test}

\end{document}

As you could see I include graphicx in mypreamble.sty and I'm able to include a picture:

enter image description here

  • I am relative new to LaTex, so I would be grateful if you could give me a more detailed MWE :) – Yorgos Nov 18 '15 at 8:09
  • @Yorgos see my edit ;-) – Romain Picot Nov 18 '15 at 8:15
  • 1
    You might add that you can put this in $HOME/texmf/tex/latex/local/ and it will be automatically found in any file. – musarithmia Nov 18 '15 at 17:20
  • 1
    @AndrewCashner I added it as a link to the question tex.stackexchange.com/questions/1137/… juste click on after installation ;-) (it's also explain the possibility for windows user) – Romain Picot Nov 18 '15 at 17:23
29

Why you shouldn't do this

Since this is longer than a comment, I will add it as an answer even though it's an answer that recommends not doing what you are asking.

There are both conceptual and practical reasons for not doing so. Of course, if you find yourself producing particular kinds of documents with a similar structure, there may be grounds to create a custom class or package for that kind of document. But if you just find that in most of your documents you load the same sets of packages, I would not recommend this, but instead use your editor to save some sort of standard template documents.

Conceptual arguments

Conceptually, packages should add a specific set of functionality to a document of any kind.

Document classes, on the other hand, should provide the markup for a particular kind of document.

See the following question for some discussion of the distinction.

Now what you are suggesting seems to be neither of these, but rather is just the list of \usepackage commands (perhaps with some added macros) that you commonly use in most of your documents. This doesn't meet either of these criteria, and so should probably not be implemented as a package or a class.

Now if you do have needs for a particular functionality then of course creating your own package makes sense. For example, I have a package which I load when I write letters which contains macros for my signature etc. I also have created a class for writing conference abstracts, since they require very condensed formatting due to word/page size limits.

Practical reasons

Even if you use some packages very frequently, it's likely that you will not need every package you load in your default preamble in every document. So you will effectively be loading packages you don't need. For smaller packages this isn't much of an issue, but for large package like TikZ for example, this will increase the compilation time for those documents.

Creating a default preamble effectively hides the packages that any document is using from you. This means that you will have to remember which packages you always load if you ever need to look for documentation for example or to debug an error that might arise from one of the packages. This may not seem like a big deal, but in my experience it can get annoying quite quickly, because over time you will forget what's in your standard preamble. It makes more sense to have exactly the packages you need loaded explicitly in the preamble of each document you write.

Creating a default preamble also makes your documents much less portable. You can't just put them on a flash drive and use them on someone else's computer, unless you also copy over the preamble package. Nor can you upload them to journals or share them with collaborators (without sharing the preamble code too).

What you should do instead

If what you want to do is just have a bunch of packages loaded and a few other things, you should use your editor to save a template. I use TeXShop on the Mac, and it allows me to create "Stationery" files which are document templates which have a standard preamble which I can define. When I open a file using the Stationery I get a new blank document with anything I like already there. Then I can delete the packages I'm not using easily to create each document so that it conforms to the criteria above.

You can do the same in other editors too. TeXStudio has a "Make Template" function (in the File menu). Create a new document with the preamble you use the most and then choose "Make Template" from the File Menu. A dialogue box will show up which allows you to name it, and add a description (and even a licence). Once that's done, for subsequent documents you can use "New From Template" to open a new untitled document with that basic structure already filled in.

TeXworks also has such a function. You can create template files and store them in <resources>/templates and they will be available using the "New From Template" menu item.

  • Very interesting solution! Do you know how to apply it in TexStudio on Ubuntu 14.04? – Yorgos Dec 23 '15 at 9:07
  • 1
    @Yorgos Yes. I've updated my answer with some information about TeXStudio and TeXworks, both of which can do this. – Alan Munn Dec 23 '15 at 14:53
  • @AlanMunn I'm also a TexShop user and I'm familiar with the Stationery. TexShop also allows us to save "Templates". Is there a difference between Stationery and Templates? I've been kicking around stackexchange and other internet sights and no one seems to address this question. – HTG Jan 28 '17 at 18:48
  • @HTG I've never actually used the Templates menu, but it seems to be commented example documents, and choosing one inserts that code into the current document. I think they are intended to help new users. The Stationery route is more efficient, since it automatically creates a new document with the Stationery content in it. I could see a use for the Templates menu in another way, as a way to hold fragments of code you use a frequently. – Alan Munn Jan 28 '17 at 20:17
12

If you don't want to make a custom package, the other option is to put your preamble into a .tex file then use \input{file_path\preamble.tex} before you begin your document where file_path is the full path to preamble.tex.

  • 3
    this works but why not give the file a .sty extension and use \usepackage rather than \input? – David Carlisle Nov 18 '15 at 11:00
  • 2
    @DavidCarlisle One advantage of \input: It allows you to also put the \documentclass to another file. Don't know if that's a good idea, but still. – jarauh Nov 18 '15 at 18:45
  • 1
    @jarauh You can also use different class with a package – Romain Picot Nov 19 '15 at 7:24
2
  1. You can create a new .tex file only having your preamble and you can use in your file..
  2. My model Preamble file will only have these contents...
\usepackage{amsmath,amsthm}

\def\qn{\question}

\def\ov{\overline}
  1. Save only these details as a .tex file.(I am saving it as preamble.tex
  2. Then in your new file's folder copy and paste the preamble.tex
  3. Now just call this file into your new file, for example
\documentclass{amsart}
\input{preamble}
\begin{document}

your work

\end{document}

Note:

  1. Your preamble.tex file should not contain any codes like \end{document} ...etc
  2. Verify that the preamble.tex file is in your folder where your new file is there.
  • You should use \input{preamble}. The extension is not needed ;-) – Romain Picot Nov 25 '15 at 10:07
  • ofcourse..its not necessory..but keeping will not harm.. – David Nov 25 '15 at 10:36
  • It will not harm of course but people use extension everywhere after. And using extension with \includegraphic could create some trouble. But of course your question is totally fine ;-) – Romain Picot Nov 25 '15 at 10:40
  • 1
    You are right..I also faced this problem earlier.. I have editted my answer..@RomainPicot – David Nov 26 '15 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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