Day 5 - $\LaTeX$ beyond the basics

Video

Motivation

You can use $\LaTeX$ effectively without ever defining a command or environment of your own, but that is hobbling yourself. New commands are one of the core features that has the mathematical, and scientific world in general, so enthralled with $\LaTeX$.

New commands

The standard way to define a new command in $\LaTeX$ is to place in the preamble:

\newcommand{\mycustomcommand}{My Custom Command}

For example, a common new command might be:

\newcommand{\RR}{\ensuremath{\mathbb{R}}}

This allows you to type \RR whenever you need the symbol for the Real Numbers. The \ensuremath command lets you use it both inside and outside a math environment - and this \RR command alone could dramatically speed up your typing of, for example, a real analysis course.

Some commands - like \frac - have one or more parameters. To use parameters, you can define:

\newcommand{\legendre}[2]{\ensuremath{\left( \frac{#1}{#2} \right) }}

This creates a new command with two arguments, to do Legendre symbols as \legendre{n}{p} - useful if you are in a number theory course.

If you find one or more of the inputs is always the same, you can do optional inputs; in the case of Legendre, the denominator is often $p$ - so we can create an optional parameter, with the default value "$p$":

\newcommand{\legendre}[2][p]{\ensuremath{\left( \frac{#2}{#1} \right) }}

Which we can invoke with \legendre{n} or \legendre[q]{n}.

Note that the parameters have switched order - optional parameters always come before required parameters in $\LaTeX$.

Default commands can have optional parameters as well- from \sqrt[n]{4} for nth roots, to optional parameters on the documentclass (such as \documentclass[letterpaper, 12pt]{article} or package imports (such as \usepackage[margin=1in]{geometry}).

Sections and Chapters

In books, articles, and reports, you can separate the portions of the document into chapters and sections with

\chapter{Chapter Title}
\section{Section Title}

These come with auto-incrementing counters in most implementations; there is some good documentation on counters on Wikibooks.

These counters are often configured with automatic resets. For example, the default behavior in articles is for section numbers is to reset every chapter, and increment immediately before each use. However, you can define and control the counters yourself with the commands listed on that page.

For example, you can set the section counter to 0 with:

\setcounter{section}{0}

Environments

Environments are used to format blocks of code. Like the document, they are started with \begin and \end; they are used for theorems, matrices, tables, embedded images, and many more.

You can create your own environments, but the main environments you will create are environments for your theorems and lemmas.

For that purpose, the amsthm package provides:

\newtheorem

and

\newtheorem*

commands.

\newtheorem{thm}{Theorem}

creates a new numbered environment thm which starts each block with Theorem.

\newtheorem{thm}{Theorem}[section]

creates a theorem with a counter that is reset every section - Theorems 0.1,0.2,1.1,1.2,....

Theorems can also use predefined counters- for example,

\newtheorem{lem}[thm]{Lemma}

would create a Lemma environment that uses the same counter as the Theorem environment.

They can also be created without counters:

\newtheorem*{aside}{Aside}

creates an unnumbered environment "Aside".

This example document demonstrates all the behaviors:

\documentclass{article}
\usepackage{amsmath,amsthm,amssymb,physics}
\newtheorem{thm}{Theorem}
\newtheorem{sthm}{Section-Numbered Theorem}[section]
\newtheorem{lem}[thm]{Lemma}
\newtheorem*{thm*}{Un-Numbered Theorem}
\begin{document}
\begin{thm}
First Theorem
\end{thm}
\begin{thm}
Second Theorem
\end{thm}
\begin{thm*}
First Unnumbered Theorem
\end{thm*}
\begin{sthm}
Section-Numbered Theorem
\end{sthm}
\begin{sthm}
Second Section-Numbered Theorem
\end{sthm}
\section{New Section}
\begin{thm}
First Theorem
\end{thm}
\begin{lem}
Second Theorem
\end{lem}
\begin{sthm}
Section-Numbered Theorem
\end{sthm}
\begin{sthm}
Second Section-Numbered Theorem
\end{sthm}
If we increment the value of a counter with $\backslash$stepcounter, it automatically resets:
\stepcounter{section}
\begin{sthm}
Third Theorem
\end{sthm}
However, if we change the value of a counter with $\backslash$setcounter, it does not automatically reset:
\setcounter{section}{20}
\begin{sthm}
Fourth Theorem
\end{sthm}
\end{document}

And compiles to:

A Demonstration of the Section Command

Note taking

Today's worksheet is to replicate what you see as accurately as possible in $\LaTeX$. If you finish it early, feel free to move on to the next day's worksheet.

Department of Mathematics, Purdue University
150 N. University Street, West Lafayette, IN 47907-2067
Phone: (765) 494-1901 - FAX: (765) 494-0548
Contact the Webmaster for technical and content concerns about this webpage.
Copyright© 2018, Purdue University, all rights reserved.
West Lafayette, IN 47907 USA, 765-494-4600
An equal access/equal opportunity university
Accessibility issues? Contact the Web Editor (webeditor@math.purdue.edu).