# Is there a way I can separately store all of my new commands in a single file?

Basically what the title says. I feel like if this is possible it's probably well known but I didn't find anything from searching. I just want to have a single place where I can store all of my new commands, then load the single file into any TeX document I make so I'm not constantly having to reenter them. Is this possible/easy to do?

• while keeping all your definitions in one place is actually a good idea, what isn't a good idea is submitting the whole thing to a publisher with a manuscript when only a few of the definitions are actually needed there. this can cause problems in preparation of manuscripts for publication -- publishers typically have their own commands as required for production of books and journals, and it's possible that some of their command names may overlap with some of yours. at the very least, "cleaning up" will take time and hair-pulling, both expensive. – barbara beeton Nov 5 '15 at 20:35

You can create a file for example mycommands.tex. There you can write your own commands as usual in TeX/LaTeX.

File mycommands.tex:

\newcommand ...    % first own command
\newcommand ...    % second own command
\endinput          % to close this file

\documentclass{article}
\usepackage{hyperref}
\input{mycommands.tex}

\begin{document}
...
\end{document}
• Thanks! So I'm assuming there's no way to do this with the "mycommands.tex" file in a separate directory? – Alex Mathers Nov 5 '15 at 3:43
• Why mycommands.tex? Surely the normal advice is to write mycommands.sty and load it the normal way: \usepackage{mycommands}? (Admittedly, there are exceptions to this, but that's usually for fairly 'advanced' reasons.) – jon Nov 5 '15 at 6:12

I also couldn't find an answer to this exact question when searching, so here's what I do: I place all my commands into one file, called preamble.tex, which goes in the same directory as the main file. Then, I call \input{preamble.tex} from the main file.

For example, here's a preamble.tex:

\newcommand{\eggs}{1}
\newcommand{\wine}{2}
\newcommand{\ziti}{3}

And here's main.tex, in the same directory:

\documentclass{article}
\input{preamble.tex}
\begin{document}
$\eggs + \wine = \ziti$.
\end{document}

If your preamble grows large enough, you may consider turning it into a package, which is discussed in this post.

• I'm using MiKTeX 2.9. The main reason for me is, that \input can input all kind of files like test.txt or test.cfg or ... \include can't. So writing \input{mycommands.tex} documents what one is doing. BTW: I learned it in this way years ago. It may be that you can omit it now, but I think it is a good way to document which file is used (and so I am sure that not a file mycommands.txt or similar existing in the same directory is used). – Kurt Nov 5 '15 at 4:42
• Oh, I never thought about it as best practices. That makes a lot of sense; thank you! I've edited my post. – Arun Debray Nov 5 '15 at 4:52

I know that it might sound strange, but in this case I suggest to keep it simple and use copy-and-paste. You could \input a file or create a .sty package, but this creates an additional dependency that you have to keep synchronized and distribute with your documents.

Whether you decide to keep a single instance of the file containing your commands or copy it in multiple directories, it seems complicated to keep track of it across multiple documents, and I don't think there are visible benefits over the good old ctrl-c,ctrl-v (or M-w,C-y, if Emacs is your cup of tea). What happens, for instance, if you make a seemingly trivial change to your master command list and this breaks an old document?

• That's actually a really excellent point. I'll definitely take this into consideration – Alex Mathers Nov 5 '15 at 4:58
• @mathers101 -- This is what bundledoc and arlatex address. Once you want to 'freeze' a document, you use them (or t least bundledoc) to collect a local version of this common file. See, e.g., this answer of mine. This way you get the best of both worlds: common main macro file while writing, then a static, unchanging, and 'local' macro file when the writing is complete. – jon Nov 5 '15 at 6:08