Underleaf Logo
Underleaf
PricingAboutBlog
Log InGet started

Learn LaTeX

Getting Started

  • What is LaTeX?
  • LaTeX for Beginners

LaTeX Style Files (.sty)

A .sty file is a LaTeX style file — essentially a custom package. It lets you bundle commands, environments, and formatting rules into a single reusable file that you load with \usepackage. If you've ever had a preamble that spans 50+ lines of custom macros, a style file is how you clean it up.

Why Use a .sty File?

Reusable Across Projects

Define your macros once, use them in every paper. No more copying preambles between documents.

Clean Preamble

Replace dozens of \newcommand lines with a single \usepackage{mycommands}.

Team Consistency

Share a .sty file with co-authors so everyone uses the same notation and formatting.

Works with AI Tools

Underleaf's Image to LaTeX and PDF to LaTeX tools can use your custom .sty files for output that matches your notation.

Creating Your First .sty File

A style file is a plain text file with a .sty extension. At minimum, it needs one line to identify itself. Create a file called mycommands.sty in the same directory as your .tex file:

% mycommands.sty \NeedsTeXFormat{LaTeX2e} \ProvidesPackage{mycommands}[2026/02/06 My custom commands] % Load any packages your commands depend on \RequirePackage{amsmath} \RequirePackage{amssymb} \RequirePackage{bm} % --- Custom commands --- % Probability and expectation \newcommand{\E}{\mathbb{E}} \newcommand{\Prob}{\mathbb{P}} \newcommand{\Var}{\operatorname{Var}} % Vector and matrix notation \newcommand{\vv}[1]{\bm{#1}} % bold vector \newcommand{\mat}[1]{\bm{#1}} % bold matrix \newcommand{\tr}{\operatorname{tr}} % trace % Sets \newcommand{\R}{\mathbb{R}} \newcommand{\N}{\mathbb{N}} \newcommand{\Z}{\mathbb{Z}} % Norms and inner products \newcommand{\norm}[1]{\left\lVert #1 \right\rVert} \newcommand{\abs}[1]{\left\lvert #1 \right\rvert} \newcommand{\inner}[2]{\left\langle #1, #2 \right\rangle} \endinput

Then load it in your document:

\documentclass{article} \usepackage{mycommands} % loads mycommands.sty \begin{document} Let $\vv{x} \in \R^n$. Then $\E[\norm{\vv{x}}^2] = \tr(\mat{\Sigma})$. \end{document}

Anatomy of a .sty File

  • \NeedsTeXFormat{LaTeX2e} — Declares that this package requires LaTeX2e (the current standard). Optional but good practice.
  • \ProvidesPackage{name}[date description] — Identifies the package. The name must match the filename (without .sty). LaTeX uses this to prevent loading the same package twice.
  • \RequirePackage{name} — The .sty equivalent of \usepackage. Use this instead of \usepackage inside style files to load dependencies.
  • \newcommand, \renewcommand, \newenvironment — Your custom definitions.
  • \endinput — Marks the end of the file. LaTeX stops reading here. Optional but recommended.

Common Patterns

Custom Environments

Style files can define custom environments, not just commands:

% A custom theorem-like environment \RequirePackage{amsthm} \newtheorem{thm}{Theorem}[section] \newtheorem{lem}[thm]{Lemma} \newtheorem{cor}[thm]{Corollary} \newtheorem{defn}[thm]{Definition} % A custom proof sketch environment \newenvironment{proofsketch} {\begin{proof}[Proof Sketch]} {\end{proof}}

Package Options

You can make your style file accept options:

\ProvidesPackage{mycommands} \newif\if@draft \DeclareOption{draft}{\@drafttrue} \DeclareOption{final}{\@draftfalse} \ProcessOptions\relax % Conditional content based on option \if@draft \newcommand{\todo}[1]{\textcolor{red}{[TODO: #1]}} \else \newcommand{\todo}[1]{} % silently ignore in final mode \fi

Load with options: \usepackage[draft]{mycommands}

Operator Shortcuts

A popular pattern is bundling field-specific operator macros:

% Machine learning notation \DeclareMathOperator{\softmax}{softmax} \DeclareMathOperator{\relu}{ReLU} \DeclareMathOperator*{\argmin}{arg\,min} \DeclareMathOperator*{\argmax}{arg\,max} \newcommand{\loss}{\mathcal{L}} \newcommand{\dataset}{\mathcal{D}} \newcommand{\params}{\bm{\theta}}

Using .sty Files in Overleaf

Overleaf fully supports custom .sty files. Upload the file to your project root (or a subdirectory), and use \usepackage as normal:

  • Click "Upload" in the Overleaf file panel and select your .sty file
  • Add \usepackage{mycommands} to your preamble
  • If the file is in a subdirectory, use \usepackage{subdir/mycommands}

The .sty file is included when you download the project as a .zip or submit to a journal, so your co-authors and reviewers will have it automatically.

.sty vs .cls Files

You may also encounter .cls files. The distinction is simple:

  • .sty (style file) — Loaded with \usepackage. Adds commands and features. You can load multiple .sty files.
  • .cls (class file) — Loaded with \documentclass. Defines the document type (article, book, etc.). You can only have one.

Conference templates (like NeurIPS or ICML) typically provide a .cls file for the document layout and sometimes a .sty file for additional macros. Your own custom notation should go in a .sty file.

Best Practices

  • Use \newcommand not \def \newcommand gives an error if the command already exists, preventing silent overwrites. \def silently replaces existing commands.
  • Prefix internal commands with @ — Commands like \my@internal@helper won't clash with user-level commands. The @ character is not normally allowed in command names in .tex files, so this provides a natural namespace.
  • Use \RequirePackage instead of \usepackage — They do the same thing, but \RequirePackage is the convention inside .sty files and works before \documentclass.
  • Keep one file per concern — If your macros grow large, split into math-notation.sty and formatting.sty rather than one giant file.
  • Version your .sty files — Include a date in the \ProvidesPackage line. Track changes in Git alongside your documents.

Starter Template

Copy this template and customize it for your field:

% mynotation.sty - Custom notation for [your field] \NeedsTeXFormat{LaTeX2e} \ProvidesPackage{mynotation}[2026/01/01 Custom notation] % Dependencies \RequirePackage{amsmath} \RequirePackage{amssymb} \RequirePackage{bm} % --- Shortcuts you'll use in every paper --- % Number sets \newcommand{\R}{\mathbb{R}} \newcommand{\N}{\mathbb{N}} \newcommand{\Z}{\mathbb{Z}} \newcommand{\C}{\mathbb{C}} % Vectors and matrices (bold notation) \newcommand{\vv}[1]{\bm{#1}} \newcommand{\mat}[1]{\bm{#1}} % Norms \newcommand{\norm}[1]{\left\lVert #1 \right\rVert} \newcommand{\abs}[1]{\left\lvert #1 \right\rvert} % Probability \newcommand{\E}{\mathbb{E}} \newcommand{\Prob}{\mathbb{P}} % Operators \DeclareMathOperator*{\argmin}{arg\,min} \DeclareMathOperator*{\argmax}{arg\,max} % --- Add your field-specific commands below --- \endinput

LaTeX Style Files (.sty)

A .sty file is a LaTeX style file — essentially a custom package. It lets you bundle commands, environments, and formatting rules into a single reusable file that you load with \usepackage. If you've ever had a preamble that spans 50+ lines of custom macros, a style file is how you clean it up.

Why Use a .sty File?

Reusable Across Projects

Define your macros once, use them in every paper. No more copying preambles between documents.

Clean Preamble

Replace dozens of \newcommand lines with a single \usepackage{mycommands}.

Team Consistency

Share a .sty file with co-authors so everyone uses the same notation and formatting.

Works with AI Tools

Underleaf's Image to LaTeX and PDF to LaTeX tools can use your custom .sty files for output that matches your notation.

Creating Your First .sty File

A style file is a plain text file with a .sty extension. At minimum, it needs one line to identify itself. Create a file called mycommands.sty in the same directory as your .tex file:

% mycommands.sty \NeedsTeXFormat{LaTeX2e} \ProvidesPackage{mycommands}[2026/02/06 My custom commands] % Load any packages your commands depend on \RequirePackage{amsmath} \RequirePackage{amssymb} \RequirePackage{bm} % --- Custom commands --- % Probability and expectation \newcommand{\E}{\mathbb{E}} \newcommand{\Prob}{\mathbb{P}} \newcommand{\Var}{\operatorname{Var}} % Vector and matrix notation \newcommand{\vv}[1]{\bm{#1}} % bold vector \newcommand{\mat}[1]{\bm{#1}} % bold matrix \newcommand{\tr}{\operatorname{tr}} % trace % Sets \newcommand{\R}{\mathbb{R}} \newcommand{\N}{\mathbb{N}} \newcommand{\Z}{\mathbb{Z}} % Norms and inner products \newcommand{\norm}[1]{\left\lVert #1 \right\rVert} \newcommand{\abs}[1]{\left\lvert #1 \right\rvert} \newcommand{\inner}[2]{\left\langle #1, #2 \right\rangle} \endinput

Then load it in your document:

\documentclass{article} \usepackage{mycommands} % loads mycommands.sty \begin{document} Let $\vv{x} \in \R^n$. Then $\E[\norm{\vv{x}}^2] = \tr(\mat{\Sigma})$. \end{document}

Anatomy of a .sty File

  • \NeedsTeXFormat{LaTeX2e} — Declares that this package requires LaTeX2e (the current standard). Optional but good practice.
  • \ProvidesPackage{name}[date description] — Identifies the package. The name must match the filename (without .sty). LaTeX uses this to prevent loading the same package twice.
  • \RequirePackage{name} — The .sty equivalent of \usepackage. Use this instead of \usepackage inside style files to load dependencies.
  • \newcommand, \renewcommand, \newenvironment — Your custom definitions.
  • \endinput — Marks the end of the file. LaTeX stops reading here. Optional but recommended.

Common Patterns

Custom Environments

Style files can define custom environments, not just commands:

% A custom theorem-like environment \RequirePackage{amsthm} \newtheorem{thm}{Theorem}[section] \newtheorem{lem}[thm]{Lemma} \newtheorem{cor}[thm]{Corollary} \newtheorem{defn}[thm]{Definition} % A custom proof sketch environment \newenvironment{proofsketch} {\begin{proof}[Proof Sketch]} {\end{proof}}

Package Options

You can make your style file accept options:

\ProvidesPackage{mycommands} \newif\if@draft \DeclareOption{draft}{\@drafttrue} \DeclareOption{final}{\@draftfalse} \ProcessOptions\relax % Conditional content based on option \if@draft \newcommand{\todo}[1]{\textcolor{red}{[TODO: #1]}} \else \newcommand{\todo}[1]{} % silently ignore in final mode \fi

Load with options: \usepackage[draft]{mycommands}

Operator Shortcuts

A popular pattern is bundling field-specific operator macros:

% Machine learning notation \DeclareMathOperator{\softmax}{softmax} \DeclareMathOperator{\relu}{ReLU} \DeclareMathOperator*{\argmin}{arg\,min} \DeclareMathOperator*{\argmax}{arg\,max} \newcommand{\loss}{\mathcal{L}} \newcommand{\dataset}{\mathcal{D}} \newcommand{\params}{\bm{\theta}}

Using .sty Files in Overleaf

Overleaf fully supports custom .sty files. Upload the file to your project root (or a subdirectory), and use \usepackage as normal:

  • Click "Upload" in the Overleaf file panel and select your .sty file
  • Add \usepackage{mycommands} to your preamble
  • If the file is in a subdirectory, use \usepackage{subdir/mycommands}

The .sty file is included when you download the project as a .zip or submit to a journal, so your co-authors and reviewers will have it automatically.

.sty vs .cls Files

You may also encounter .cls files. The distinction is simple:

  • .sty (style file) — Loaded with \usepackage. Adds commands and features. You can load multiple .sty files.
  • .cls (class file) — Loaded with \documentclass. Defines the document type (article, book, etc.). You can only have one.

Conference templates (like NeurIPS or ICML) typically provide a .cls file for the document layout and sometimes a .sty file for additional macros. Your own custom notation should go in a .sty file.

Best Practices

  • Use \newcommand not \def \newcommand gives an error if the command already exists, preventing silent overwrites. \def silently replaces existing commands.
  • Prefix internal commands with @ — Commands like \my@internal@helper won't clash with user-level commands. The @ character is not normally allowed in command names in .tex files, so this provides a natural namespace.
  • Use \RequirePackage instead of \usepackage — They do the same thing, but \RequirePackage is the convention inside .sty files and works before \documentclass.
  • Keep one file per concern — If your macros grow large, split into math-notation.sty and formatting.sty rather than one giant file.
  • Version your .sty files — Include a date in the \ProvidesPackage line. Track changes in Git alongside your documents.

Starter Template

Copy this template and customize it for your field:

% mynotation.sty - Custom notation for [your field] \NeedsTeXFormat{LaTeX2e} \ProvidesPackage{mynotation}[2026/01/01 Custom notation] % Dependencies \RequirePackage{amsmath} \RequirePackage{amssymb} \RequirePackage{bm} % --- Shortcuts you'll use in every paper --- % Number sets \newcommand{\R}{\mathbb{R}} \newcommand{\N}{\mathbb{N}} \newcommand{\Z}{\mathbb{Z}} \newcommand{\C}{\mathbb{C}} % Vectors and matrices (bold notation) \newcommand{\vv}[1]{\bm{#1}} \newcommand{\mat}[1]{\bm{#1}} % Norms \newcommand{\norm}[1]{\left\lVert #1 \right\rVert} \newcommand{\abs}[1]{\left\lvert #1 \right\rvert} % Probability \newcommand{\E}{\mathbb{E}} \newcommand{\Prob}{\mathbb{P}} % Operators \DeclareMathOperator*{\argmin}{arg\,min} \DeclareMathOperator*{\argmax}{arg\,max} % --- Add your field-specific commands below --- \endinput
Underleaf Logo
Underleaf

Empowering students and researchers with AI-powered tools for academic writing.

Go to appContact us

Company

PricingBlogTutorialsAffiliate Program

Free Tools

Image to LaTeXExcel to LaTeXArXiv to LaTeXTikZ GeneratorThesis GeneratorChrome ExtensionAll Tools

© 2026 Underleaf. All rights reserved.