# Can my LaTeX (maths) code be improved?

I feel that my code isn't written very efficiently.

\documentclass[a4paper,11pt]{article}
\usepackage[english]{babel}
\usepackage[pdftex]{graphicx}
\usepackage{tikz}
\usepackage{wasysym}
\usepackage[all]{xy}
\usepackage{amsfonts}

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

\begin{document}

\maketitle

\section{Disjoint definition}
Sets A and B are \underline{disjoint} if A $\cap$ B = \{ c,d,e \}
\\[2mm]
But if C=\{ x,y,z \} then A and C are disjoint, B and C are disjoint.
\begin{center}
\line(1,0){250}
\end{center}

Note: A $\subseteq$ A so A $\in$  $\mathcal{P}$(A)
\\[2mm]
$\mathcal{P}$ $\subseteq$ A so $\emptyset$ $\in$ $\mathcal{P}$(A)
\\[2mm]
x $\in$ A, \{x\} A, \{x\} $\in$ $\mathcal{P}$(A)

\section{Cartesian definition}
The \underline{cartesian product} of the set $A_1$, $A_2$, ..., $A_n$ is $A_1$ $\times$ $A_2$ $\times$ ... $\times$ $A_n$
\newline
=\{($a_1$, $a_2$, ..., $a_n$)(a, $\in$$A_1)\land(a_2$$\in$$A_2)\land...\land(a_n$$\in$$A_n)\} \\[2mm] The element (a_1, a_2, ..., a_n) is an \underline{ordered n-tuple}. \\[2mm] E.g.: (x,y) in cartesian plan (i.e.: the coordiantes of a point in the plane) is an ordered pair, the Cartesian plane is the product \mathbb{R}x\mathbb{R} or \mathbb{R}^2. \section{Cardinality definition} In general if \|A\|=n, \|B\|=m then \|A \times B\| = \|A\| \|B\| \\[2mm] \|$$A^n$$\|=\|A\| \|A\| (<n times>) \|A\| = \|$$A^n$$\|$
\end{document}

\documentclass[a4paper,11pt]{article}
\usepackage[english]{babel}
\usepackage[pdftex]{graphicx}
\usepackage{tikz}
\usepackage{wasysym}
\usepackage[all]{xy}
\usepackage{amsfonts}

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

\begin{document}

\maketitle

\section{Laws}
Let A = \{ x $\in$ $\mathbb{Z}$ $\|$  x $\le$ 5 \}
\newline
Let B = \{ x $\in$ $\mathbb{Z}$ $\|$ x $>$ 0 \}
\\[2mm]
A $\cap$ B = \{ x $\in$ $\mathbb{Z}$ $\|$ (x $\le$ 5) $\land$ (x $>$0) \}
\newline
= \{ 1,2,3,4,5 \}
\\[2mm]
A $\cup$ B = \{ x $\in$ $\mathbb{Z}$ $\|$ (x $\le$ 5) $\lor$ (x $>$ 0) \} = $\mathbb{Z}$
\\[2mm]
$\bar{A}$ = \{ x $\in$ $\mathbb{Z}$  $\|$ x $>$ 5 \}
\newline
= \{x $\in$ $\mathbb{Z}$ $\|$-(x$\le$5) \}
\\[2mm]
A $\cap$ $\mathbb{P}$ = A
\\[2mm]
A $\cap$ $\bar{A}$ = $\diameter$
\\[2mm]
A $\cap$ $\diameter$ = $\diameter$
\\[2mm]
A $\cup$ (A $\cap$ B) $\equiv$ A
\\[2mm]
A $\cap$ B
\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}
\\[2mm]
$\overline{A \cap B}$ \tikz \fill[even odd rule] (0,0) circle (1) (1,0) circle (1);

\section{Equivalency}
Two expressions involving sets are equivalent if ew can represent them with the same venn diagram. This corresponds to using truthg tables to establish equivalence of logical expressions.

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

\subsubsection{Example 2}
Find B $\cap$ $\bar{C}$
\newline
$\bar{C}$=\{1,2,4,6,8,10\}
\newline
So B $\cap$ $\bar{C}$ = \{1,4,6,10\}

\subsubsection{Example 3}
Find (A $\cap$ $\bar{B}$) $\cup$ C = (A $\cap$ \{ 2,3,5,9,9 \}) $\cup$ C = \{ 2,9 \} $\cup$ C = \{ 2,3,5,7,9 \}
\subsubsection{Example 4}
Find $\overline{B \cup C}$ $\cap$ C = \{ $\overline{1,3,4,5,6,7,8,10}$\} $\cap$ C = \{ 2,8 \} $\cap$ C = $\diameter$

\subsubsection{Example 5}
Show that  (A $\cup$ $\bar{B}$) $\cap$ (A $\cup$ B) = A
\newline
(A $\cup$ $\bar{B}$) $\cap$ (A $\cup$B) = ((A $\cup$ $\bar{B}$) $\cap$ A) $\cup$ ((A $\cup$ $\bar{B}$) $\cap$ B) \underline{\bf distr}
\newline
=((A $\cup$ $\bar{B}$) $\cap$ A) $\cup$ ((A $\cap$ B) $\cup$ ($\bar{B}$ $\cap$ B)) \underline{\bf distr}
\newline
= A $\cup$ ((A$\cap$B)$\cup$($\bar{B}$ $\cap$ B)) \underline{\bf absorption}
\newline
= A $\cup$ (A $\cap$ B) \underline{\bf identity}
\newline
= {\bf A}
\end{document}


Is there a better way of writing it?

FYI: I type up these notes during the lectures.

-
Too much bouncing back and forth between math mode and text mode. Math mode isn't limited to things like \cup An expression that includes letters to me looks better when the entire expression is in math mode. For example, $(A \cup \bar{B}) \cap (A \cup B) = ((A \cup \bar{B}) \cap A) \cup ((A \cup \bar{B}) \cap B)$ It looks a lot better, and it is a lot less typing. –  David Hammen Aug 25 '11 at 5:22
Also, I believe that mixing math/text modes with operators may lead to spacing issues. I also suggest you try "$(1,\ 2,\ \dots,\ n)$" for tuples. –  Axioplase Aug 25 '11 at 5:28
If you are typing during the actual lecture then my first recommendation is not to worry about getting the formatting correct. That can be fixed later. My second is to get a camera and taking pictures of the board when you get behind. –  Loop Space Aug 25 '11 at 7:19
I've read one suggestion on this site earlier about making up commands as you go, and then later actually define them (for example \P for \mathcal{P}) for speeding up the writing. –  morbusg Aug 26 '11 at 4:27

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 (relative to the initial form at least). Some \mathstruts were inserted to raise the overline parts a bit. The command \varnothing now denotes the empty set. Finally, I've placed the two diagrams into center environments so that they're each centered on the page (along with their respective captions).

\documentclass[a4paper,11pt]{article}
%%\usepackage[english]{babel}
\usepackage{graphicx,tikz}
%%\usepackage{wasysym}
\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}

\begin{align*}
\text{Let }\mathbb{P} &= \{ 1,2, \dots, 10 \}\\
A &= \{ 2,4,7,9 \}\\
B &= \{ 1,4,6,7,10 \}\\
C &= \{ 3,5,7,9 \}
\end{align*}

\begin{example}
Find $B \cap \bar{C}$.

\noindent
$\bar{C}=\{1,2,4,6,8,10\}$.
So $B \cap \bar{C} = \{1,4,6,10\}$.
\end{example}

\begin{example}
Find $(A \cap \bar{B}) \cup C$.

\noindent
$A \cap \bar{B} = A \cap \{ 2,3,5,9 \} = \{ 2,9 \}$.
Hence $(A \cap \bar{B})\cup C = \{ 2,3,5,7,9 \}$.

\end{example}

\begin{example}
Find $\overline{\mathstrut B \cup C} \cap C$.

\noindent
$\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)
&= \bigl((A \cup \bar{B}) \cap A\bigr) \cup
\bigl((A \cap B) \cup (\bar{B} \cap B) \bigr)
&= A \cup \bigl((A\cap B)\cup(\bar{B} \cap B)\bigr)
&= A \cup (A \cap B)
&= A
\end{align*}
\end{example}

\end{document}


-
Perfect! - Thanks a heap... now I just need to figure out which post to mark as answer... they're all soo good! –  A T Aug 28 '11 at 5:44

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):

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.

-
Thank you very much! - You've just made my code much easier to write :] (don't really like the italics, but I suppose that's a changeable setting somewhere) –  A T Aug 28 '11 at 5:34
@AT: Yes. Under xelatex (a different compiler), you can follow: Change math font only in some parts of a document. Or you can typeset x using \mathrm{x} - upright roman math. Or, define a macro that will do all that for you: \def\x{\mathrm{x}} and then use \{\x\in ...\}. That latter option is easy, but is definitely not considered standard use of math symbols in LaTeX. Note that some single-letter controls like \b (bar) and \c (cedilla) already have a different meaning. –  Werner Aug 28 '11 at 5:50
Perfect, thanks. Can I put multiple answers in stackoverflow as the best answer? - Your answer has been great, but so has Mico's. Stefan's simplifies some things as well... !!! –  A T Aug 28 '11 at 7:02
@AT: Sadly, no. Question posters can only select one answer as the accepted/most helpful one. Read the end of the FAQ entry: How do ask questions here?. –  Werner Aug 28 '11 at 7:09
• 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.
-
Hmm, text mode? - I like that idea. Much neater. Thanks. I'll use \left and \right (wow, it truly is amazing the configuration latex gives you) –  A T Aug 28 '11 at 5:35

And here's my nomination for how to rework Lecture 15. Note that I've defined a command \card{} to denote the cardinality of a set. That way, if you ever want to change the notation, you only have to do it once, in the document's preamble. Plus, I believe it makes the code far more readable too.

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

\theoremstyle{definition}
\theoremstyle{break}
\theorembodyfont{\upshape}
\newtheorem{definition}{Definition}

%%\usepackage{wasysym}
\usepackage[all]{xy}

%% cardinality
\newcommand{\card}[1]{\ensuremath{\left\|#1\right\|}}

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

\begin{document}

\maketitle

\section{Definitions}

Sets $A$ and $B$ are \emph{disjoint} if $A \cap B = \varnothing$.
Suppose $C=\{ x,y,z\}$. Then if $A$ and $C$ are disjoint, $B$ and $C$
are disjoint as well.

Notes:
\begin{itemize}
\item $A \subseteq A$, so $A \in\mathcal{P}(A)$
\item $\mathcal{P}$ $\subseteq$ A so $\varnothing\in\mathcal{P}$(A) (huh?!)
\item $x \in A$, $\{x\}\subseteq A$, $\{x\} \in \mathcal{P}(A)$
\end{itemize}

\begin{definition}[Cartesian Product]
The \emph{Cartesian product} of the sets $A_1, A_2,\dots, A_n$ is
\begin{equation*}
A_1 \times A_2 \times\dots \times A_n
=\bigl\{(a_1, a_2, \dots, a_n) \bigm| (a_1 \in A_1) \land(a_2\in A_2)
\land\dots \land(a_n\in A_n)\bigl\}\,.
\end{equation*}
Each element $(a_1, a_2, \dots, a_n)$ is an \emph{ordered $n$-tuple}.

\noindent
Example: $(x,y)$ in the Cartesian plane, i.e., the coordinates of
a point in the plane, is an ordered pair. The Cartesian plane is
the product $\mathbb{R}\times\mathbb{R}$ or~$\mathbb{R}^2$.
\end{definition}

\begin{definition}[Cardinality]
Denote the cardinality of a set~$A$ by~$\card{A}$.
In general, if $\card{A}=n$ and $\card{B}=m$,
then $\card{A\times B} = \card{A} \cdot \card{B}=mn$, and
$\card{A^n}=\underbrace{\card{A}\cdot\card{A}\dots \card{A}}_{\text{$n$times}} =\card{A}^n$.
\end{definition}

\end{document}

-
Thanks, looks much better :] –  A T Aug 28 '11 at 5:41