# Creating a package of custom-made commands

I tend to use the \mathbb{...} command a lot, so I have made some commands to ease the amount of typing I have to do when I use LaTeX:

\newcommand{\R}{\ensuremath{\mathbb{R}}}
\newcommand{\C}{\ensuremath{\mathbb{C}}}
\newcommand{\Z}{\ensuremath{\mathbb{Z}}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\F}{\mathbb{F}}
\newcommand{\W}{\mathbb{W}}


Up until this point, I have been simply copying and pasting these lines each time I begin working with a new file. What is the recommended way of importing these lines from say, a custom-made package? Is this generally recommended (i.e. making shortcuts like this)?

I'm currently using TexLive on Mac OS X.

-

A custom-made package is much easier than it sounds. Make a new file called, for example, alexmath.sty, which contains:

\ProvidesPackage{alexmath}
\newcommand{\R}{\ensuremath{\mathbb{R}}}
\newcommand{\C}{\ensuremath{\mathbb{C}}}
\newcommand{\Z}{\ensuremath{\mathbb{Z}}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\F}{\mathbb{F}}
\newcommand{\W}{\mathbb{W}}


and simply use it with \usepackage{alexmath}.

That's it, you've made a custom package. It's not really clean or documented enough to be distributed to the community, but it's really enough for your needs. You can even install it in your local system TeX directories so you don't need to copy it in all your source directories.

-
On Mac OS X a good place is ~/Library/texmf/tex/latex/alexmath/, creating the needed folders if necessary (~ represents the home directory); on GNU/Linux systems it's ~/texmf/tex/latex/alexmath. The deepest folder is not really necessary, but it helps to maintain an ordered structure. It may well be called alex or whatever, to be populated by similar personal packages. –  egreg Sep 5 '11 at 23:26
Just be aware of two potential pitfalls with this approach: (i) your documents become less portable (you will need to supply your package along with your document if you share it) and (ii) you may forget how things are defined (depending on how often you use them) and figuring out the definitions is easier if they are in your document preamble. I'm not suggesting you not do what you're suggesting, just that it can have its downsides. –  Alan Munn Sep 5 '11 at 23:33
OK, thanks for the input! I've actually been putting them in the \usr\local\texlive\texmf-local\tex\latex directory. ~/Library/texmf/tex/latex sounds a bit safer though :). –  Alex Lockwood Sep 6 '11 at 0:05
Alan has a good point there. This is why I made a separate "commands" file which is included via \input{./commands.tex} into the actual document. This way every project will compile on friends' machines or on my own computer in several years, when my local installation is long gone. –  0x6d64 Sep 6 '11 at 7:00
While all that is true, you can still keep the .sty file in your current directory instead of installing it in your system. You will then benefit from using \usepackage without having the pitfall mentioned by @AlanMunn. This is what I usually do for my books. Eventually, if your packages are generic enough and potentially interesting for others, you can make them into CTAN packages, upload them and use them as standard LaTeX packages (which is what I also do when I reach this point). –  ℝaphink Sep 6 '11 at 7:28