Skip to main content
To make a LaTeX package, create a .sty file next to your main .tex file that starts with \NeedsTeXFormat{LaTeX2e} and \ProvidesPackage{packagename}, loads its dependencies with \RequirePackage, defines your commands or environments, and ends with \endinput. Load it in any document with \usepackage{packagename}. If you want to find or install an existing CTAN package instead of writing your own, see Package management and the Packages reference. If you need to define a new document structure or replace \maketitle, see Create a document class.

Step 1: Write a minimal .sty file and load it

Move repeated preamble macros and environments into a .sty file when you use them in more than one document. Inside a .sty file:
  • \NeedsTeXFormat{LaTeX2e} declares the required LaTeX format.
  • \ProvidesPackage{reviewnotes}[2026/11/21 Drafting and review macros] registers the package name (matching the filename without .sty) and an optional YYYY/MM/DD version date.
  • \RequirePackage{xcolor} loads dependencies (the package-file counterpart to \usepackage).
  • \endinput tells TeX to stop reading the file.
Inside .sty files, @ is treated as a letter automatically, so you can name internal helper macros with @ (for example \rn@helper) without wrapping them in \makeatletter and \makeatother.
reviewnotes.sty
main.tex
Ready to use this syntax? Continue in the browser editor when you want to adapt the example in a real project.Open LaTeX Cloud Studio

Step 2: Add package options with \DeclareOption

Packages can accept options inside \usepackage[...]{...} using \DeclareOption, \ExecuteOptions (for defaults), and \ProcessOptions\relax. Add a fallback \DeclareOption* with \PackageWarning so typos in an option name produce a clear warning in the log.
reviewopts.sty
main.tex
In this example, \ExecuteOptions{draft} sets the default state before \ProcessOptions\relax runs the options passed by the user. Passing [blue] overrides the internal \ro@color macro, while [final] flips \ifro@hide so \todoitem expands to nothing.

Step 3: Save the .sty file where \usepackage finds it

TeX searches the directory containing your main .tex file before scanning system package trees. Keeping reviewnotes.sty in the same folder as main.tex makes \usepackage{reviewnotes} work immediately without modifying a local TeX installation, and keeps the package versioned alongside the document in Git. In LaTeX Cloud Studio, projects hold multiple files (including .sty files), and the compiler places all project files into the build directory alongside main.tex on TeX Live 2026 (full distribution), so standard dependencies such as xcolor are already installed.

Common mistakes

  • Option clashes on \RequirePackage: If a document loads \usepackage[margin=2cm]{geometry} and your package later runs \RequirePackage[margin=1cm]{geometry}, LaTeX stops with Option clash for package geometry. Prefer loading dependencies with no options inside your .sty file whenever possible.
  • Unintended spaces in macro definitions: Line breaks inside \newcommand definitions turn into spaces in output. End intermediate lines with % inside horizontal macros such as \todoitem.
  • Filename mismatch with \ProvidesPackage: If the file is named notes.sty but contains \ProvidesPackage{reviewnotes}, LaTeX prints a warning about the mismatched package name.
  • Putting \LoadClass in a .sty file: LaTeX raises Two \LoadClass commands when the package is loaded after \documentclass (or \LoadClass in package file if loaded beforehand via \RequirePackage); packages extend the document’s existing class rather than loading a new one.

Try it in the editor

Start from the Basic Article template in LaTeX Cloud Studio, replace main.tex with the complete example, and add reviewnotes.sty (or reviewopts.sty) to the project.