Introduction to LATEX Lesson 6: User-defined commands and theorem environments Dept. of Mathematical Sciences User-defined commands I I In addition to builtin LATEX commands, the user may define her own commands. I This is done in the preamble using \newcommand whose basic form is \newcommand{\com}{def} where \com is the newly defined command and def its definition. I For instance, \newcommand{\com}{My command} \begin{document} \com\com\com My commandMy commandMy command 2/11 User-defined commands II I The command definition may contain also math. For example, \newcommand{\Z}{\mathbb{Z}} \begin{document} $k\in\Z$ k∈Z I Note that the command above must be used in math mode. I One may use \ensuremath to ensure that the argument is processed in math mode. For example, \newcommand{\Z}{\ensuremath{\mathbb{Z}}} \begin{document} Now both $\Z$ and \Z{} work. Now both Z and Z work. 3/11 User-defined commands III I I I I I Also user-defined commands may receive arguments. To that end the command must be defined as \newcommand{\com}[n]{def} where n is the number of arguments (1-9). One refers to arguments using #k, where k= 1, 2, . . . , n. For example, command for norm with one argument: \newcommand{\norm}[1]{\left\|#1\right\|} \begin{document} $\norm{f+g}\leq\norm{f}+\norm{g}$ kf + gk ≤ kf k + kgk Default values for possible optional arguments can be given using the form: \newcommand{\com}[n][default]{defn} Existing commands may be redefined using \renewcommand in a similar fashion. 4/11 Counters I I LATEX I I I keeps track of chapter, section, equation, page etc. numbering using counters. Key counters are: part page enumi chapter equation enumii section figure enumiii subsection table enumiv subsubsection Of these, enumX keeps track of four levels of items in numbered lists (enumerate). Others are self-explanatory. The current value of a counter may be printed out using the following commands: \arabic{counter} 1,2,3,... \alph{counter} a,b,c,... \roman{counter} i,ii,iii,... \Alph{counter} A,B,C,... \Roman{counter} I,II,III,... \counter 1,2,3,... 5/11 Counters II I The value of a counter may be set using \setcounter{counter}{num} which sets the value of counter to be the integer num. I The value of a counter may be incremented or decremented using \addtocounter{counter}{num} which increments (decrements) the value of counter by num if num is positive (negative). I New counters may be created using \newcounter{counter} 6/11 Theorem environments I I I I I Theorems, lemmas, proofs etc. are best typeset using user-defined theorem environments. These are defined in the preamble using the amsthm command \newtheorem{name}{Title}. For example, how to define and use one’s own theorem environment: (square brackets are optional) \usepackage{amsthm} \newtheorem{thm}{Theorem} \begin{document} \begin{thm}[Prime number theorem] There are infinitely many primes. \end{thm} Theorem 1 (Prime number theorem). There are infinitely many primes. This creates a thm environment with title "Theorem" and with automatic numbering. Note the body of theorem in italics. 7/11 Theorem environments II I One can control the numbering by passing a counter as an optional argument. For example, \newtheorem{thm}{Theorem}[section] creates a theorem environment, which numbers theorems 1.1, 1.2, 1.3,. . . within section 1. I The form \newtheorem{lemma}[thm]{Lemma} creates a lemma environment which uses the same counter as thm environment. Then \begin{thm}...\end{thm} \begin{lemma}...\end{lemma} \begin{thm}...\end{thm} produces: Theorem 1.1, Lemma 1.2, Theorem 1.3 8/11 Theorem environments III I The command \theoremstyle{style} sets the style of theorem environments that follow. Here style must be one of plain, definition or remark. I For instance, \theoremstyle{definition} \newtheorem{defn}{Definition} creates a defn environment for definitions so that the body is not in italics (cf. the default plain style above for theorems). I Analogously \theoremstyle{remark} \newtheorem{rem}{Remark} creates a rem environment whose title ("Remark") is in italics. I amsthm package provides proof environment for proofs. Hence the user needs not define it herself. 9/11 Theorem environments IV I Similarly to other document structures, one can refer also to theorems using \label{} and \ref{}. I For example, \begin{thm}\label{thm_1} ... \end{thm} \begin{proof} ... \end{proof} It follows from Theorem \ref{thm_1} that... It follows from Theorem 1.2 that... I Unnumbered environments can be defined using \newtheorem*{}{} 10/11 Summary I \newcommand{\com}{defn}, \ensuremath{...} I \newcommand{\com}[n]{defn}, \renewcommand{...} I \setcounter{counter}{num}, \addtocounter{counter}{num} I \newtheorem{}{}, \newtheorem*{}{} I \newtheorem{thm}{Theorem}[section] I \newtheorem{lemma}[thm]{Lemma} I \theoremstyle{...} I \begin{proof}...\end{proof} I \begin{thm}\label{key} ... \end{thm} \ref{key} 11/11