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}? – Andrew Stacey Jan 10 '13 at 14:01 -
-
Didn’t you miss a
\relaxbehind\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 -
1
-
3@tparker
\includeshould only be used in thedocumentenvironment, for big chunks of text, typically a unit like a chapter. Remember it issues a page break. – egreg Mar 04 '17 at 09: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.
- 757,742
- 1,405
-
1"library code" implies code with "internal" definitions, using the
@sign in command names. when a.styfile 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.styfile is exited. – barbara beeton Jan 10 '13 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.. – David Carlisle Jan 10 '13 at 13:48
texorstyor evencls… – Tobi Jan 10 '13 at 13:18\inputor\includeatexfile on the preamble? – Sigur Jan 10 '13 at 13:32.texfile with only the commonly used commands as opposed to defining a new style using.styfiles. – recluze Jan 10 '13 at 13:39\inputa file in the preamble. however,\includealways 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.texfile phrase the wrong way. – percusse Jan 10 '13 at 14:22