Difference between revisions of "LaTeX:Packages"

(Making Your Own)
(fix em dash)
 
(14 intermediate revisions by 9 users not shown)
Line 4: Line 4:
  
 
==General Information==
 
==General Information==
In the Commands pages, we discussed building your own commands. Fortunately, many, many people have built their own commands already and made them available to LaTeX users in packages. Packages allow us to use extra commands without having to include tons and tons of code in the preamble of a document. Instead, we just add the line
+
In the Commands pages, we discussed building your own commands. Fortunately, many, many people have built their own commands already and made them available to <math>\text{\LaTeX}</math> users in packages. Packages allow us to use extra commands without having to include tons and tons of code in the preamble of a document. Instead, we just add the line
  
\usepackage{package name}
+
\usepackage{package name}
  
 
and we have access to all the commands, symbols, environments, etc., that are in the package.
 
and we have access to all the commands, symbols, environments, etc., that are in the package.
 +
 
==Common Packages==
 
==Common Packages==
This section will cover the packages released by the [American Mathematical Society], as well as xypic and fancyhdr.
+
This section will cover the packages released by the [http://www.ams.org/ American Mathematical Society], as well as xypic and fancyhdr.
  
 
===AMS Math packages===
 
===AMS Math packages===
Line 20: Line 21:
 
</pre>
 
</pre>
 
Most of the basic parts of these packages which will be useful to you are covered somewhere on these pages. If you'd like to read about more options these packages provide, read the documentation for them at the following links:
 
Most of the basic parts of these packages which will be useful to you are covered somewhere on these pages. If you'd like to read about more options these packages provide, read the documentation for them at the following links:
*[ftp://ftp.ams.org/pub/tex/doc/amsmath/amsldoc.pdf amsmath documentation]
+
*[http://texdoc.net/texmf-dist/doc/latex/amsmath/amsldoc.pdf amsmath documentation]
*[ftp://ftp.ams.org/pub/tex/doc/amsfonts/amsfndoc.pdf amssymb documentation]
+
*[http://milde.users.sourceforge.net/LUCR/Math/mathpackages/amssymb-symbols.pdf amssymb documentation]
*[ftp://ftp.ams.org/pub/tex/doc/amscls/amsthdoc.pdf amsthm documentation]
+
*[http://www.ctex.org/documents/packages/math/amsthdoc.pdf amsthm documentation]
 
Note that including these packages can slow down the compilation of your document, so you may not want to include them unless you really need them.
 
Note that including these packages can slow down the compilation of your document, so you may not want to include them unless you really need them.
  
 
+
''Note: \usepackage{amsmath} is always recommended. amsthm must be loaded ''after'' amsmath, so everything hooks up properly.''
 
 
 
 
  
 
===xypic===
 
===xypic===
Line 34: Line 33:
 
\usepackage{xy}
 
\usepackage{xy}
 
</pre>
 
</pre>
 
 
 
 
  
 
===fancyhdr===
 
===fancyhdr===
Line 83: Line 78:
  
 
If introducing headers and footers makes you want to resize the dimensions of the text portion of your page, consult the [[LaTeX:Layout|Layout]] article. Note that if you do resize the dimension of the text portion of your page, you should execute those commands '''before''' executing the header and footer commands.
 
If introducing headers and footers makes you want to resize the dimensions of the text portion of your page, consult the [[LaTeX:Layout|Layout]] article. Note that if you do resize the dimension of the text portion of your page, you should execute those commands '''before''' executing the header and footer commands.
 
  
 
==Finding Other Packages==
 
==Finding Other Packages==
Line 89: Line 83:
  
 
The [http://tug.ctan.org/tex-archive/help/Catalogue/catalogue.html TeX Catalogue Online] has a very comprehensive list of all the LaTeX packages out there.
 
The [http://tug.ctan.org/tex-archive/help/Catalogue/catalogue.html TeX Catalogue Online] has a very comprehensive list of all the LaTeX packages out there.
 +
 +
(Link was broken in 2018, according to Wayback Machine by Internet Archive. Visit [http://ctan.org CTAN] (CTAN stands for <math>\text{Comprehensive \TeX\ Archive Network}</math>) to search with the search bar on the top.)
 +
 +
To install those packages, look for the name in "Contained in" section of the information pane on a package's homepage on CTAN. On <math>\text{\TeX\ Live or Mac\TeX}</math> do on the terminal "sudo tlmgr install <package-name>". For <math>\text{Mik\TeX}</math> it will prompt you to install the first time you try to use it.
  
 
==Making Your Own==
 
==Making Your Own==
Line 96: Line 94:
 
<li>You have two options for where to save the file to be able to include it in LaTeX source files:</li>
 
<li>You have two options for where to save the file to be able to include it in LaTeX source files:</li>
 
<ol><li>(Recommended) Install the .sty file with MiKTeX as follows:</li>
 
<ol><li>(Recommended) Install the .sty file with MiKTeX as follows:</li>
<ul><li>Navigate to the folder where LaTeX packages get installed. If you accepted the default location when you installed MiKTeX, this folder is probably '''C:\Program Files\MiKTeX 2.5\tex\latex'''. (If you have an older MiKTeX installation, this folder is probably '''C:\texmf\tex\latex'''.)</li>
+
<ul><li>Navigate to the folder where LaTeX packages get installed. If you accepted the default location when you installed MiKTeX, this folder is probably '''C:\Program Files\MiKTeX 2.9\tex\latex'''. (If you have an older MiKTeX installation, this folder is probably '''C:\texmf\tex\latex'''.)</li>
 
<li>Create a new folder inside this folder and call it whatever you want.</li>
 
<li>Create a new folder inside this folder and call it whatever you want.</li>
 
<li>Save your .sty file into this folder.</li>
 
<li>Save your .sty file into this folder.</li>
<li>Open the MiKTeX Options program. (You can do this by clicking Start, then All Programs, then MiKTeX, then MiKTeX Options.)</li>
+
<li>Open the MiKTeX Options program. (You can do this by clicking Start, then All Programs, then MiKTeX, then MiKTeX Options. It might also be labelled 'Settings.')</li>
<li>Click 'Refresh Now' on the window that opens. Now you should be able to use any information in that .sty file in any LaTeX source file.</li></ul>
+
<li>Click 'Refresh Now' or 'Refresh FNDB' on the window that opens. Now you should be able to use any information in that .sty file in any LaTeX source file.</li></ul>
<li>Simply put your .sty file in the same folder as your source files. Unfortunately, this means if you have LaTeX source files in other folders, you'll have to carry copies of your .sty files to those folders, too. The real problem with this approach comes when you want to change one of these files - if you use this method rather than method 1, you have to change it all those copies (or have many different versions of the same file, which is dangerous).</li></ol>
+
<li>Simply put your .sty file in the same folder as your source files. Unfortunately, this means if you have LaTeX source files in other folders, you'll have to carry copies of your .sty files to those folders, too. The real problem with this approach comes when you want to change one of these files --- if you use this method rather than method 1, you have to change it all those copies (or have many different versions of the same file, which is dangerous).</li></ol>
 
<li>Now you are able to include your package in the usual way: \usepackage{your file name}, where you don't include the '.sty' in the file name in the \usepackage statement.</li>
 
<li>Now you are able to include your package in the usual way: \usepackage{your file name}, where you don't include the '.sty' in the file name in the \usepackage statement.</li>
 
<li>You can now call any command you have created in your .sty file.</li></ol>
 
<li>You can now call any command you have created in your .sty file.</li></ol>
Line 153: Line 151:
  
 
If you installed everything properly, your source file should compile just fine now.
 
If you installed everything properly, your source file should compile just fine now.
 +
 +
==Command Basics==
 +
 +
Of course, the previous section was very basic.  You still need to know, for example, how to include variables.
 +
To begin with, the syntax is
 +
<pre>
 +
\newcommand\<commandname>[number of variables]{what it does}
 +
</pre>
 +
So, for example, you want an addition package.  Here is a basic addition function.
 +
<pre>
 +
\newcommand\addition[2]{#1 + #2}
 +
</pre>
 +
Here, #1 and #2 refer to the variables in order.  If you had 3 variables, and you wanted to make a division package, you would have
 +
<pre>
 +
\newcommand\division[3]{#1 \div #2 = #3}
 +
</pre>
 +
And in a document:
 +
<pre>
 +
\documentclass[11pt]{article}
 +
\usepackage{division}
 +
\begin{document}
 +
\begin{math}\division{6}{3}{2}\end{math}
 +
\end{document}
 +
</pre>
 +
Would result in <math>6\div 3 = 2</math>.
  
 
==See Also==
 
==See Also==
 
*[[LaTeX:Help | Next: Help]]
 
*[[LaTeX:Help | Next: Help]]
 
*[[LaTeX:Commands | Previous: Commands]]
 
*[[LaTeX:Commands | Previous: Commands]]

Latest revision as of 08:33, 21 September 2024

LaTeX
About - Getting Started - Diagrams - Symbols - Downloads - Basics - Math - Examples - Pictures - Layout - Commands - Packages - Help

This article has information about a few common packages, as well as instructions on how to make your own package.

General Information

In the Commands pages, we discussed building your own commands. Fortunately, many, many people have built their own commands already and made them available to $\text{\LaTeX}$ users in packages. Packages allow us to use extra commands without having to include tons and tons of code in the preamble of a document. Instead, we just add the line

\usepackage{package name}

and we have access to all the commands, symbols, environments, etc., that are in the package.

Common Packages

This section will cover the packages released by the American Mathematical Society, as well as xypic and fancyhdr.

AMS Math packages

The American Mathematical Society has produced several packages for use with LaTeX. These packages allow much of the mathematical formatting we have introduced on these pages, as well as much, much more. The packages are included with the installation of MiKTeX. In order to be able to use the commands and symbols in these packages, they must be included in each document in which you require them. To include the packages, include the following in the preamble of your document:

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}

Most of the basic parts of these packages which will be useful to you are covered somewhere on these pages. If you'd like to read about more options these packages provide, read the documentation for them at the following links:

Note that including these packages can slow down the compilation of your document, so you may not want to include them unless you really need them.

Note: \usepackage{amsmath} is always recommended. amsthm must be loaded after amsmath, so everything hooks up properly.

xypic

The xypic package allows you to make tricky diagrams such as ones which include various items connected by arrows or lines. Take a look at the xypic home page to see what xypic can do. The package comes with MiKTeX. To use it, include the following in your preamble:

\usepackage{xy}

fancyhdr

The fancyhdr package provides an easy way to place text on the top and/or bottom of every page. The fancyhdr package does not come with MiKTeX, but assuming that you installed MiKTeX using our recommendations, it should prompt you to install it the first time you try to use it. You include it in your source file in the usual way:

\usepackage{fancyhdr}

The fancyhdr user's guide has detailed instructions on how to use the package. Here's an example that illustrates most of the basics you'll need:

\documentclass[11pt]{article}
\usepackage{fancyhdr}

\pdfpagewidth 8.5in
\pdfpageheight 11in

\pagestyle{fancy}
\headheight 35pt

\rhead{Right top}
\chead{Middle top}
\lhead{Left top}
\rfoot{Right bottom}
\cfoot{\thepage}
\lfoot{Left bottom}

\begin{document}
First page

\pagebreak

Second page

\pagebreak

Third page
\end{document}

\pagestyle{fancy} tells LaTeX that you'll be using the fancy style which allows you to use the header and footer commands.

The \headheight command makes space at the top of the page for the header. Try compiling without it and look through the errors to see what happens.

The \rhead, \chead, \lhead, \rfoot, \cfoot, and \lfoot commands create headers and footers in the obvious places (you'll see them in the output when you create the example).

The \pagebreak forces new pages so you can see how the headers and footers appear on every page.

If introducing headers and footers makes you want to resize the dimensions of the text portion of your page, consult the Layout article. Note that if you do resize the dimension of the text portion of your page, you should execute those commands before executing the header and footer commands.

Finding Other Packages

Many, many packages have been created for LaTeX. If there's something unusual that you want to do, there's a good chance that there's already a package out there that does it.

The TeX Catalogue Online has a very comprehensive list of all the LaTeX packages out there.

(Link was broken in 2018, according to Wayback Machine by Internet Archive. Visit CTAN (CTAN stands for $\text{Comprehensive \TeX\ Archive Network}$) to search with the search bar on the top.)

To install those packages, look for the name in "Contained in" section of the information pane on a package's homepage on CTAN. On $\text{\TeX\ Live or Mac\TeX}$ do on the terminal "sudo tlmgr install <package-name>". For $\text{Mik\TeX}$ it will prompt you to install the first time you try to use it.

Making Your Own

Making your own high-powered, fancy package like the ones created by the AMS requires a great deal of work and detailed knowledge about LaTeX. However, making your own set of commands or a basic preamble that you can include in any of your LaTeX documents is simple. Just follow these steps:

  1. Create a file for the information you'd like to include in multiple files and name it something with a .sty extension, such as mylay.sty or mycommands.sty.
  2. Put in the .sty file all the commands you'd like to use in multiple source files and save the file.
  3. You have two options for where to save the file to be able to include it in LaTeX source files:
    1. (Recommended) Install the .sty file with MiKTeX as follows:
      • Navigate to the folder where LaTeX packages get installed. If you accepted the default location when you installed MiKTeX, this folder is probably C:\Program Files\MiKTeX 2.9\tex\latex. (If you have an older MiKTeX installation, this folder is probably C:\texmf\tex\latex.)
      • Create a new folder inside this folder and call it whatever you want.
      • Save your .sty file into this folder.
      • Open the MiKTeX Options program. (You can do this by clicking Start, then All Programs, then MiKTeX, then MiKTeX Options. It might also be labelled 'Settings.')
      • Click 'Refresh Now' or 'Refresh FNDB' on the window that opens. Now you should be able to use any information in that .sty file in any LaTeX source file.
    2. Simply put your .sty file in the same folder as your source files. Unfortunately, this means if you have LaTeX source files in other folders, you'll have to carry copies of your .sty files to those folders, too. The real problem with this approach comes when you want to change one of these files --- if you use this method rather than method 1, you have to change it all those copies (or have many different versions of the same file, which is dangerous).
  4. Now you are able to include your package in the usual way: \usepackage{your file name}, where you don't include the '.sty' in the file name in the \usepackage statement.
  5. You can now call any command you have created in your .sty file.

We recommend that you use different .sty files for all your new commands and for your layout. You may end up having many different layouts, but you'll probably want your custom commands available to all your documents. Separating the commands from the layout preamble makes it easy to include those commands with many different layouts. Do note that every time you create a new .sty, you must go through the steps above to use it (i.e. if you install it with MiKTeX, you have to put it in the folder mentioned above, then use MiKTeX Options as described above to complete the installation). You don't have to reinstall every time you add a command to an existing installed .sty file.

Here's a quick example. In mylay.sty, include the following:

%packages
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage[pdftex]{graphics}
\usepackage{mycommands}

%layout
\pdfpagewidth 8.5in
\pdfpageheight 11in
\topmargin 0in
\headheight 0in
\headsep 0in
\textheight 9in
\textwidth 6.5in
\oddsidemargin 0in
\evensidemargin 0in

In the file mycommands.sty, put the following:

%commands

\newcommand\rttw{\sqrt{2}}
\newcommand\rtth{\sqrt{3}}
\newcommand\rtfi{\sqrt{5}}
\newcommand\rtsi{\sqrt{6}}
\newcommand\rtse{\sqrt{7}}

Install these two files using either method from above.

In a LaTeX source file, put the following:

\documentclass[11pt]{article}
\usepackage{mylay}

\begin{document}
This is my first document with my own packages.

I can use my new commands, watch: $\rttw + \rtse$.

\end{document}

Notice that we only have to include mylay with \usepackage; mycommands is included in the file mylay.sty (look at the mylay.sty file contents and you'll see the line \usepackage{mycommands}).

If you installed everything properly, your source file should compile just fine now.

Command Basics

Of course, the previous section was very basic. You still need to know, for example, how to include variables. To begin with, the syntax is

\newcommand\<commandname>[number of variables]{what it does}

So, for example, you want an addition package. Here is a basic addition function.

\newcommand\addition[2]{#1 + #2}

Here, #1 and #2 refer to the variables in order. If you had 3 variables, and you wanted to make a division package, you would have

\newcommand\division[3]{#1 \div #2 = #3}

And in a document:

\documentclass[11pt]{article}
\usepackage{division}
\begin{document}
\begin{math}\division{6}{3}{2}\end{math}
\end{document}

Would result in $6\div 3 = 2$.

See Also