Why is it preferable to put inclusions of commonly used packages and user defined commands in a .sty file instead of putting them in an ordinary .tex file?

  • 6
    To avoid copy/pasting the same definitions over and over again to each document.
    – percusse
    Jan 10, 2013 at 13:11
  • 11
    @percusse: I guess the question is more about the suffix and which one to use – tex or sty or even cls
    – Tobi
    Jan 10, 2013 at 13:18
  • Is it possible to \input or \include a tex file on the preamble?
    – Sigur
    Jan 10, 2013 at 13:32
  • 1
    @percusse I think he's trying to compare a separate .tex file with only the commonly used commands as opposed to defining a new style using .sty files.
    – recluze
    Jan 10, 2013 at 13:39
  • 3
    @Sigur -- it is certainly possible to \input a file in the preamble. however, \include always starts a new page, an action that is not permitted in the preamble, only after \begin{document}. Jan 10, 2013 at 13:42

3 Answers 3


First of all: never use \include to load a file with personal definitions and packages to use.

The choice is thus between \input and \usepackage; for the first it's better to use the extension .tex for the file, for the second .sty is mandatory.

What are the pros of the latter solution? Many. For instance you can define options that can change the behavior of your macros or selectively load parts of it (see example below).

In a .sty file @ is assumed to be a letter, so no \makeatletter or \makeatother command is needed to access "private macros", which is often the case for complex macros.

If you don't need options nor access to private macros, loading your definitions and package with \input{mymacros} is exactly equivalent to \usepackage{mymacros} (provided that the file is mymacros.tex in the first case and mymacros.sty in the second one).

As noticed by Andrew Stacey, there is one more pro in using a .sty file: it won't be loaded twice, even if called twice in a document (maybe frome some other loaded file or package). This is important because \newcommand would raise errors on the second loading (and other definitions might lead to infinite loops).

Example: Suppose you have a macro that must change its behavior when the draft option is enabled in the \documentclass line; for instance it should have an argument that's emphasized in the text and is also written in the margin for draft copies.



  \newcommand{\myterm}[1]{\emph{#1}\marginpar{TERM: #1}}


If a document does



then "TERM: foo" will be written in the margin. If draft is removed, the same source will only emphasize "foo" in the text.

  • 8
    And is \input{myfile}\input{myfile} equivalent to \usepackage{myfile}\usepackage{myfile}? Jan 10, 2013 at 14:01
  • @AndrewStacey You have a point!
    – egreg
    Jan 10, 2013 at 14:02
  • Didn’t you miss a \relax behind \ProcessOptions*? I thought it’s god style to use it but forgot why …
    – Tobi
    Jan 10, 2013 at 22:28
  • 1
    Could you elaborate on why \include is so bad?
    – tparker
    Mar 4, 2017 at 2:43
  • 3
    @tparker \include should only be used in the document environment, for big chunks of text, typically a unit like a chapter. Remember it issues a page break.
    – egreg
    Mar 4, 2017 at 9:40

You may put "library" code into .tex files and use \input instead of \usepackage, but then to give package options you'd have to define some variables. This would looks worse and you can do more mistakes.

  • 1
    "library code" implies code with "internal" definitions, using the @ sign in command names. when a .sty file is read in with \usepackage, @ is automatically assumed to be a letter (so it can be used in a command name), and its status is converted back to "other" (a non-letter) when the .sty file is exited. Jan 10, 2013 at 13:46
  • 1
    In addition by using a package you get automatic setting of @ and access to the version control \usepackage{mypkg}[2013/01/01] to ensure that you get this year's version of your package.. Jan 10, 2013 at 13:48

A minor point from a very practical point of view: Certain tools such as fig2pdf allow to specify a list of packages to be loaded. This is done with \usepackage, thus the .sty ending is required for personal adjustments.

You must log in to answer this question.

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