I have a bunch of common commands I put in document preambles. I don't always use them all, but it makes the preamble too long for my tastes.

I want to put them all in an external file (preferably in my user texmf tree). A class file seems like overkill; it isn't robust and I don't know TeX anyway. Creating a package seems intimidating as well. Can I just put the macros in a macro.tex in my tree and use \include{macro.tex} in my preamble? Also, could I include the same after \begin{document}?


Just put them all in a mymacros.sty file and add the single line on top (which is also optional!):

\ProvidesPackage{mymacros}[2011/02/23 v1.0 My own macros]

That's all what is required for a simple package. No plainTeX knowledge required. Load it as usually using \usepackage{mymacros}

Please note that \include can't be used in the preamble. For this kind of files (as .tex without the above code line) you need to use \input.

  • Won't this be a problem if mymacros.sty includes \usepackage lines? – Lev Bishop Feb 23 '11 at 22:53
  • 5
    Not really. However, these should be renamed to \RequirePackage. (Note that \usepackage is \let to \RequirePackage inside \documentclass and defined as an error before that) – Martin Scharrer Feb 23 '11 at 23:05
  • Does each package need its own \RequirePackage line? – jrhorn424 Feb 25 '11 at 15:05
  • Lev and Josef both have good points. I would have chosen both if I could. Thanks, everyone. – jrhorn424 Feb 25 '11 at 15:05
  • 1
    @jrhorn424: No, you can use \RequirePackage as well as \usepackage to load multiple packages as long you don't need to set different options for these. However, the normal way is to use one command per package. This makes it easier to remove and add packages or options and is also easier to read IMHO. – Martin Scharrer Feb 25 '11 at 15:14

You can of course use \input after \begin{document} but not for preamble commands. \include adds a new page and is meant for chapters. Simply put your commands in something like mydef.sty and load it with \usepackage{mydefs}

  • What makes you think you can't use \input for preamble commands? – Lev Bishop Feb 23 '11 at 22:51
  • @Lev: I think the point is that if mydefs.sty were to contain various setup the OP always finds himself writing in the preamble, it should not (and, depending on which macros were used, cannot) be loaded after \begin{document}. – Villemoes Feb 23 '11 at 23:32
  • 1
    @Lev You can of course \input preample commands, but not after \begin{document} jehorn242's second question´ – Josef Feb 23 '11 at 23:45
  • 2
    @Josef: now I see what you are saying. Perhaps you could edit your answer to clarify that \input is valid both in and out of the preamble, depending on which commands you are inputting. – Lev Bishop Feb 24 '11 at 4:33
  • 1
    Yes, \input can go anywhere. Macro definitions can go anywhere. A few commands are preamble-only, the most common ones being \documentclass and \usepackage, so if you \input a file containing these commands, you must do so in the preamble. – Lev Bishop Feb 24 '11 at 19:09

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.