Can my LaTeX (maths) code be improved?

The easy answer here is, of course, yes - there is always room for improvement. One main observation I can make is the following: Mathmode in LaTeX is not limited to symbols and operators; you are allowed to use letters in math mode as well. For example, consider the difference in style when writing

Let A = \{ x $\in$ $\mathbb{Z}$ $\|$  x $\le$ 5 \} \newline
Let B = \{ x $\in$ $\mathbb{Z}$ $\|$ x $>$ 0 \}

and writing

Let $A=\{x \in \mathbb{Z} \mid x \leq 5\}$ \newline
Let $B=\{x \in \mathbb{Z} \mid x > 0\}$

In your style of writing the change to math mode $ $ and back to text mode causes the mathematical text and symbols to be used inconsistently. The difference is visible in the respective outputs (yours on the left):

OP's mathematics Better mathmode

Notice that LaTeX takes care of the spacing between symbols, letters and operators.

If you're interested in improving your technical typesetting in LaTeX, especially in terms of mathematics, consider reading through Herbert Voß' excellent mathmode document.


Here's my nomination for how to rewrite Lecture 16. The new version makes use of constructs of the amsmath, amssymb, and ntheorem packages. I've also simplified most of the math expressions, at least relative to the initial forms. Some \mathstruts were inserted to raise the overline parts a bit. The command \varnothing now denotes the empty set. Finally, I've put the two diagrams into center environments so that they're each centered on the page (along with their respective captions).

enter image description here

enter image description here

\documentclass[a4paper,11pt]{article}
\usepackage{graphicx,tikz}
\usepackage[all]{xy}
\usepackage{amsmath,amssymb,ntheorem}

\usepackage{ntheorem}
\theoremstyle{break}
\theorembodyfont{\upshape}
\newtheorem{example}{Example}

\title{Discrete Mathematics --- Lecture 16}
\author{Alec Taylor}
\date{August 25, 2011}

\begin{document}
\maketitle

\section{Laws}

Let $\mathbb{P}=\mathbb{Z}$, $A = \{ x\in\mathbb{Z} \mid x \le 5 \}$, 
and $B = \{ x\in\mathbb{Z} \mid x > 0 \}$. Then:
\begin{align*}
     A \cap B &= \{ x\in\mathbb{Z} \mid (x \le 5) \land (x >0) \} \\
              &= \{ 1,2,3,4,5 \} \\    
     A \cup B &= \{ x\in\mathbb{Z} \mid (x \le 5) \lor (x > 0) \} =\mathbb{Z}\\    
     \bar{A}  &= \{ x\in\mathbb{Z} \mid x > 5 \}\\                  
              &= \{ x\in\mathbb{Z} \mid -(x\le5) \}\\    
 A\cap\mathbb{P} &= A\\
 A \cap \bar{A}  &= \varnothing\\    
 A \cap \varnothing  &= \varnothing\\    
 A \cup (A \cap B) &\equiv A 
\end{align*}

\begin{center}
$A \cap B$

\medskip
\begin{tikzpicture}
\filldraw[fill=gray] (-2,-2) rectangle (3,2);
\scope % A \cap B
\clip (0,0) circle (1);
\fill[white] (1,0) circle (1);
\endscope
% outline
\draw (0,0) circle (1)
      (1,0) circle (1);
\end{tikzpicture}
\end{center}

\medskip
\begin{center}
$\overline{\mathstrut A \cap B}$ 

\medskip
\tikz \fill[even odd rule] (0,0) circle (1) (1,0) circle (1);
\end{center}


\section{Equivalency}

Two expressions involving sets are equivalent if we 
can represent them with the same venn diagram. 
This corresponds to using truth tables to establish 
equivalence of logical expressions.

\subsection{Examples}
\raggedright
Let $\mathbb{P}= \{ 1,2, \dots, 10 \}$,     
$A = \{ 2,4,7,9 \}$,  
$B = \{ 1,4,6,7,10 \}$, and   
$C = \{ 3,5,7,9 \}$.

\begin{example}    
Find $B \cap \bar{C}$.
\[
\bar{C}=\{1,2,4,6,8,10\}.\quad
\text{So $B \cap \bar{C} = \{1,4,6,10\}$}\,.
\]
\end{example}

\begin{example}    
Find $(A \cap \bar{B}) \cup C$.
\[
A \cap \bar{B} = A \cap \{ 2,3,5,9 \} = \{ 2,9 \}. 
\quad\text{Hence $(A \cap \bar{B})\cup C = \{ 2,3,5,7,9 \}$}\,.
\]
\end{example}

\begin{example}    
Find $\overline{\mathstrut B \cup C} \cap C$.
\[
\overline{\mathstrut B \cup C} \cap C = \overline{\{ 1,3,4,5,6,7,8,10\} } \cap  C 
= \{ 2,8 \} \cap C = \varnothing\,.
\]
\end{example}

\begin{example}
Show that  $(A \cup \bar{B}) \cap (A \cup B) = A$.
\begin{align*}
(A \cup \bar{B}) \cap (A \cup B) 
&= \bigl((A \cup \bar{B}) \cap A\bigr) \cup 
   \bigl((A \cup \bar{B}) \cap B\bigr) 
   \quad\textbf{distr}\\    
&= \bigl((A \cup \bar{B}) \cap A\bigr) \cup 
   \bigl((A \cap B) \cup (\bar{B} \cap B) \bigr) 
   \quad\textbf{distr}\\    
&= A \cup \bigl((A\cap B)\cup(\bar{B} \cap B)\bigr) 
     \quad\textbf{absorption}\\    
&= A \cup (A \cap B) 
   \quad\textbf{identity}\\    
&= A
\end{align*}
\end{example}

\end{document}

  • Use the amsmath package for advanced math features
    • Consider using multiline environments with alignment at equal signs or relation signs.
    • Instead of frequently using \\[2mm] use blank lines for paragraph breaks, possibly increase \parskip once in the preamble. Or, in multiline math environments use their line spacing or increase \jot.
    • Instead of leaving math mode, consider using \text{...} or \intertext{...} for text within math.
  • Check out mathtools if you would like to have further sophisticated features.
  • For braces and other delimiters you could use \left and \right for automatic sizing.