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?
3 Answers
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.
\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}
\myterm{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.
-
8And is
\input{myfile}\input{myfile}
equivalent to\usepackage{myfile}\usepackage{myfile}
? Jan 10, 2013 at 14:01 -
-
Didn’t you miss a
\relax
behind\ProcessOptions*
? I thought it’s god style to use it but forgot why …– TobiJan 10, 2013 at 22:28 -
1
-
3@tparker
\include
should only be used in thedocument
environment, for big chunks of text, typically a unit like a chapter. Remember it issues a page break.– egregMar 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 -
1In 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.
tex
orsty
or evencls
…\input
or\include
atex
file on the preamble?.tex
file with only the commonly used commands as opposed to defining a new style using.sty
files.\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}
.