Tx · LaTeX & TikZ

Where Should You Put LaTeX Macro Package Files?

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose the scope

    Keep one-document definitions in its preamble; use a project folder or personal TeX tree for reusable files.

  2. 2. Use the package folder

    Place .sty files under tex/latex/package-name inside a TeX tree, or beside the project’s main .tex file.

  3. 3. Check discovery

    Load the package without its extension and use kpsewhich or your TeX distribution’s file database tools if it is not found.

Advertisement

Where a LaTeX macro file belongs depends on whether you need it for one document or for many. A file that defines commands for a single document can go directly in that document’s preamble. A reusable package file belongs either in the project folder or in a personal TeX tree.

For a project-specific package, put the .sty file alongside the main .tex file. For a package you want LaTeX to find from different projects, use a personal TeX tree and follow its directory structure. Avoid changing files inside directories owned by your TeX distribution.

If the macros are for one document

For a few definitions, put them in the preamble—the part between \documentclass and \begin{document}:

\documentclass{article}
\usepackage{amsmath}

\newcommand{\vect}[1]{\boldsymbol{#1}}

\begin{document}
The vector \vect{x} is written in bold.
\end{document}

This is usually simplest when the definitions are only used in one file. If you have several related definitions, you can move them into a separate file and load it with \input{my-macros}. That file does not need to be a package: it can simply contain the command definitions.

If the package is for one project

Place the package file in the same folder as the document that uses it. For example:

my-project/
├── report.tex
└── projectmacros.sty

Load it in the preamble with:

\usepackage{projectmacros}

Do not include the .sty extension in \usepackage. Keeping the file with the project makes the project easier to move or share. If you put it in a subfolder, such as tex/, you can load it with \usepackage{tex/projectmacros}; keeping package files alongside the main document is often less error-prone.

If you want to reuse it across projects

Install the .sty file in a personal TeX tree. The usual TeX Directory Structure (TDS) location for a LaTeX package is:

<personal-tex-tree>/tex/latex/<package-name>/<package-name>.sty

For example, on many TeX Live installations on Linux, the personal tree is ~/texmf, giving a path such as:

~/texmf/tex/latex/projectmacros/projectmacros.sty

The precise personal-tree location depends on your operating system and TeX distribution. TeX Live commonly uses ~/texmf on Unix-like systems; other installations may use a different location. MiKTeX lets you configure additional roots in MiKTeX Console. Check your distribution’s documentation or settings rather than assuming that a path from another computer applies to yours.

Once LaTeX can find the file, you can load it by package name from a document in any working folder:

\usepackage{projectmacros}

Keep the package in its own directory if it has multiple files, such as a .sty file and supporting input files. The tex/latex/ part of the path tells the TeX system that these are LaTeX files; the package-specific subdirectory helps keep them organized.

Make LaTeX notice the file

Many installations find files in the personal tree automatically. If a new package is not found, check the path and then update the file-name database if your distribution uses one. In TeX Live, mktexlsr (also called texhash) can refresh the database. In MiKTeX, refresh the file-name database in MiKTeX Console. The exact menu names can vary by version.

You can test whether TeX can locate a file by running:

kpsewhich projectmacros.sty

If the command prints a path, the file is discoverable. If it prints nothing, check the filename, directory structure, and configured TeX roots. On case-sensitive systems, ProjectMacros.sty and projectmacros.sty are different filenames; matching the capitalization avoids portability problems.

Common mistakes

  • Putting a personal package in the distribution’s install directory. Updates may remove or replace files there, and permissions can prevent you from editing them. Use a personal tree instead.
  • Using the wrong directory structure. A personal-tree package should normally be below tex/latex/; placing the file at the tree’s top level can make it harder for TeX to find.
  • Including .sty in \usepackage. Write \usepackage{projectmacros}, not \usepackage{projectmacros.sty}.
  • Installing a package you only need in one project. Keeping it beside the project’s main .tex file is simpler and makes the project portable.
  • Confusing a package with ordinary macro definitions. For a small set of document-specific commands, a preamble or an \input file may be all you need.

The practical rule is: put one-document definitions in the document, project-specific package files beside the project, and reusable packages in a personal TeX tree following the TDS layout.

References

  1. A Directory Structure for TeX files (TDS)
  2. TeX Live Guide

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.