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.

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?

share|improve this question
4  
To avoid copy/pasting the same definitions over and over again to each document. –  percusse Jan 10 '13 at 13:11
6  
@percusse: I guess the question is more about the suffix and which one to use – tex or sty or even cls –  Tobi Jan 10 '13 at 13:18
    
Is it possible to \input or \include a tex file on the preamble? –  Sigur Jan 10 '13 at 13:32
    
@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 '13 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}. –  barbara beeton Jan 10 '13 at 13:42

3 Answers 3

up vote 47 down vote accepted

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.

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.

\ProvidesPackage{mymacros}
\newif\if@myfiledraft
\DeclareOption{draft}{\@myfiledrafttrue}

\ProcessOptions\relax

\if@myfiledraft
  \newcommand{\myterm}[1]{\emph{#1}\marginpar{TERM: #1}}
\else
  \newcommand{\myterm}[1]{\emph{#1}}
\fi

\endinput

If a document does

\documentclass[draft]{article}
\usepackage{mymacros}

\begin{document}
\term{foo}
\end{document}

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

share|improve this answer
7  
And is \input{myfile}\input{myfile} equivalent to \usepackage{myfile}\usepackage{myfile}? –  Loop Space Jan 10 '13 at 14:01
    
@AndrewStacey You have a point! –  egreg Jan 10 '13 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 '13 at 22:28
    
@Tobi Actually I wanted to type \ProcessOptions\relax; when there's the * you don't need \relax, because the * stops \@ifstar. –  egreg Jan 10 '13 at 22:30
    
Ah :-) Thank you –  Tobi Jan 11 '13 at 0:38

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.

share|improve this answer
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. –  barbara beeton Jan 10 '13 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.. –  David Carlisle Jan 10 '13 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.

share|improve this answer

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.