1

This question already has an answer here:

sorry if the title is badly worded but I wasn't sure how to word it.

My question is this: I have a lot of things before \begin{document} in my latex file, maybe like 180 lines of setting up commands and use packages.

I was wondering if I could put all the code above the line \begin{document} into a package and import that in so I can save lines.

Thanks

marked as duplicate by Alan Munn, Stefan Pinnow, user121799, TeXnician, Sebastiano Mar 10 '18 at 8:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • For a single document, you can just save all the files as e.g. mypreamble.tex and then use \input{mypreamble}. No real need to make a package. – Alan Munn Mar 10 '18 at 4:32
  • @AlanMunn A project specific preamble and a default preamble are different things. – Heiko Oberdiek Mar 10 '18 at 4:37
  • @HeikoOberdiek Of course, but the principles of creating the package are identical. – Alan Munn Mar 10 '18 at 4:38
  • @AlanMunn Not entirely. For a project I would rather use a class instead of a package, because I have then a really clean preamble for the document. – Heiko Oberdiek Mar 10 '18 at 4:42
  • Could you also put the \bibliographystyle in there as well? – Vass Mar 10 '18 at 4:45
1

You can write a class:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{MyProject}[2018/03/10 v1.0 Class for my project]
\LoadClass[<options>]{<class name>}% former: \documentclass[...]{...}

\RequirePackage[<options>]{<package name>}% former: \usepackage[...]{...}

% other stuff from the preamble

\endinput

\documentclass and \usepackage need other names, see above. Also, remove \makeatletter/\makeatother pairs, because \makeatletter is automatically active in class and package files.

Then use the class:

\documentclass{MyProject}
\begin{document}
  ...
\end{document}

A class or package have advantages over a simple \input:

  • Options can be added.
  • Some version support by LaTeX:

    \documentclass{<class>}[2018/03/10]
    \usepackage{<package>}[2018/03/10]
    

    Then, LaTeX will warn, if the class or package are older than specified.

Not the answer you're looking for? Browse other questions tagged or ask your own question.