% This is a LaTeX file.  LaTeX is a language which can be used to typeset
% documents similar to those one might get from e.g. OpenOffice or Microsoft
% Word.  The difference is that, while those are "WYSIWYG" ("what you see is
% what you get") tools, LaTeX is a markup language.  This has advantages and
% disadvantages, but one of the major advantages is in writing documents that
% involve a lot of symbols or patterns.  You are not required to use this file
% (or LaTeX in general) to complete your work, but we expect it will help.  :)

% First and foremost: LaTeX files must be compiled like C++ programs.  In order
% to compile your LaTeX file, run
%
%     pdflatex LearningLaTeX.tex
%
% This will produce a file LearningLaTeX.pdf which you may then view with a PDF
% viewer:
%
%     evince LearningLaTeX.pdf
%
% Remember that you must recompile your document if you change the .tex file!

% The rest of this document will contain some examples of how to use LaTeX.  As
% you may have guessed, the '%' character is an end-of-line comment marker, much
% like // in C++ or # in Python.  Keep reading!



% The following command is typically the first in any document.  It says that
% we want the PDF to be in an article format.  It's a sensible default for
% simple documents.  Commands in LaTeX start with a backslash.  Some commands
% take arguments, each of which is surrounded by a pair of braces.  So, in
% LaTeX,
%
%     \documentclass{article}
%
% is sort of writing this in C++:
%
%     documentclass("article");
\documentclass{article}

% Next, we need to bring in common commands, just like we would in Python or
% C++.  Rather than "import" or "#include", we use the "\usepackage" command.

\usepackage{amsmath} % Math notation from the American Mathematical Society.
\usepackage{amssymb} % Typographical symbols from the same.

% In addition to commands, LaTeX has an idea of "environments".  These are sort
% of like indentation in Python or the { ... } notation in C++, but they differ
% in what they do.  The most important environment is called "document" and
% everything that appears in your document must be inside of it.  We start an
% environment with the "begin" command and stop it with the "end" command.
% Although it is not necessary, we typically indent the contents of the
% environment to make the .tex file easier to read.
\begin{document}

    % Everything we've seen so far has been a comment or a command.  If I want
    % text, all I have to do is put it inside of this document environment.
    Hello!
    % Extra spaces in LaTeX don't matter, so that "Hello!" won't be indented.
    % Instead, it will be in a paragraph.  In fact, newlines are treated like
    % spaces, so the following is on the same line.
    How are you?

    % If you want a new paragraph, you insert a blank line.  Since the above
    % line is blank, the following will be in its own paragraph.
    This is a new paragraph.

    % Basic text formatting is pretty simple in LaTeX. The \textbf{...} command makes
    % boldfaced text; the \emph{...} is used to emphasize text; this usually means
    % making the text italic, unless the surrounding text is already italicized
    % (in which case, it usually un-italicizes the emphasized text). There is
    % also \textit{...} which italicizes text always; it does not do anything
    % different if the surrounding text is already italicized.
    \textbf{This is bold.} \emph{This is emphasized.} This is plain text.
    % If you have a good editor, the above might even be rendered to hint at the
    % font changes.

    % So far, everything we have written has been in "paragraph mode". It is
    % also possible to write LaTeX in "math mode". One way of entering math mode
    % is to write a dollar sign. Another dollar sign leaves math mode. This kind
    % of math mode is "in-line" and can be written alongside text.
    Here's something in paragraph mode: 4+2x.
    Here it is in math mode: $4+2x$.

    % Math mode is also important because it allows you to use the mathematical
    % notation from the AMS packages that we named above.  For instance, the
    % command \dots draws a sequence of dots, like an ellipse.
    Here is an example of using dots in an equation: $1+2+\dots+10 = 55$

    % In math mode (but not in paragraph mode), the symbols ^ and _ have special
    % meaning: they superscript or subscript the very next thing.  For instance,
    % the following produces the letter "x" with a subscripted "i" and a
    % superscripted "10".
    Here is an example of subscripts and superscripts: $x_{i}^{10}$

    % Notice the use of braces {...}, which indicates that everything in between
    % the braces should be treated as a one thing, so that everything inside the
    % braces appears as part of the sub- or superscript. Without them, only the
    % first symbol would be in the sub- or superscript. In the above example,
    % the superscript would be '1' instead of '10', and the '0' would appear
    % outside the superscript:
    Without braces around the subscript and superscript, only the first symbol
    appears in the sub- or superscript: $x_i^10$
    % To avoid these awkward formatting issues, it's good practice to always
    % include the braces.

    % Let's consider using those subscripts for another purpose. We might want
    % to write a summation; conveniently, this is possible with the \sum command.
    % Fractions can be written using \frac, which takes two arguments: the
    % numerator and the denomenator. The following, for instance, is a common
    % summation:
    Here is an example of a summation:
    $\sum_{i=1}^{n} i = 1 + 2 + \dots + n = \frac{n(n+1)}{2}$

    % You might notice that the limits on the summation look a little cramped.
    % That's because LaTeX is trying to squeeze it to fit in-line. We can
    % instead tell LaTeX to display the math on its own line using \[ and \].
    % This is appropriately called "display math mode", and it allows LaTeX to
    % use more vertical space to make the math look nicer.
    Here is a summation in display math mode:
    \[ \sum_{i=1}^{n} i = 1 + 2 + \dots + n = \frac{n(n+1)}{2} \]
    % Notice that it no longer appears in-line, and latex places the equation on
    % its own line, with generous vertical space to allow for nicer formatting.
    % You can also place the \[ and \] on their own line, if you'd like (as
    % shown in the next example).

    % For Big-O proofs, we'll also want to be able to write the logical
    % quantifiers "for all" and "exists" (the upside-down A and the backwards
    % E).  Conveniently, these are accessible via the \forall and \exists
    % commands.
    Here is an example using logical quantifiers:
    \[
      \forall x>0, \exists y<0, x+y = 0
    \]

    % We can also get comparison symbols such as "greater than or equal to" if
    % we know the right name.
    $x \geq y \leq z$

    % Finally, we'll want to be able to write out our proofs in an organized
    % fashion. We may want to label equations so that we can refer to them in
    % our text. We can do this using the 'equation' environment:
    \begin{equation}
      \label{eq:pythagoras}
      a^{2} + b^{2} = c^{2}
    \end{equation}

    % And we can refer to it in our proofs with \eqref, as shown in the example
    % below. When compiled, this will automatically insert the equation number.
    % In this example, ~ tells LaTeX to use a "nonbreaking space", meaning it
    % won't end the line at the space. This is typically recommended when
    % referencing equations.
    Suppose a triangle has a leg of length $x$ and a hypotenuse of length $2x$.
    Then, by~\eqref{eq:pythagoras}, the other leg has length
    $\sqrt{(2x)^{2} - x^{2}} = \sqrt{3x^2} = \sqrt{3}\cdot x$.

    % If we want to show multiple steps simplifying the same equation, we can
    % use the 'split' environment inside of 'equation'. The whole derivation
    % will get a single equation number. In a 'split' environment, \\ is used to
    % start a new line, and & is used to tell LaTeX where to align multiple
    % lines.
    %
    % Also helpful: We can refer to an equation before it appears, as shown
    % below.
    As shown in~\eqref{eq:poly-mult} below, $(2x+3)(x^{2} - x)$ is a polynomial
    of degree three.
    \begin{equation}
      \label{eq:poly-mult}
      \begin{split}
        (2x + 3)(x^{2} - x) &= 2x(x^{2}) + 3(x^{2}) - 2x(x) - 3(x) \\
                            &= 2x^{3} + 3x^{2} - 2x^{2} - 3x \\
                            &= 2x^{3} + x^{2} - 3x
      \end{split}
    \end{equation}

    % If we want the benefits of the 'split' environment without an equation
    % number, AMS Math also provides the starred environments such as 'equation*':
    \begin{equation*}
      \begin{split}
        (2x + 3)(x^{2} - x) &= 2x(x^{2}) + 3(x^{2}) - 2x(x) - 3(x) \\
                            &= 2x^{3} + 3x^{2} - 2x^{2} - 3x \\
                            &= 2x^{3} + x^{2} - 3x
      \end{split}
    \end{equation*}
    
    % Sometimes we might want to show a multi-step calculation that involves
    % multiple equations. There is the 'align' environment for this, which works
    % similarly to 'equation'+'split', except that each line receives a separate
    % equation number:
    \begin{align}
      % As with 'split', 'align' uses \\ to start a new line and & to align
      % lines. For instance, we can derive the geometric series:
      S       &= \sum_{k=0}^{\infty} ar^{k} \\
      S       &= a + ar + ar^{2} + ar^{3} + \dots \\
      rS      &= ar + ar^{2} + ar^{3} + ar^{4} + \dots \\
      S - rS  &= a \\
      (1-r)S  &= a \\
      S       &= \frac{a}{1-r}
    \end{align}

    \[
      % If you need normal text inside of a math environment, one way to render
      % it is using the \text command, as shown below. This example also shows
      % how we can get the Z symbol commonly used for the set of integers
      % using the \mathbb command from the amssymb package (the same command
      % with Q and R can give us the symbols for the sets of rationals and
      % reals, repectively).
      % We can also use \mid for the vertical bar (this gives better spacing for
      % set-builder notation than typing |).
      S = \{ x \in \mathbb{Z} \mid x \text{ is odd} \}
    \]

    % Sometimes we might want to typeset a "piecewise-defined function", which
    % we can do with the 'cases' environment in math mode.
    \[
      f(x) =
      \begin{cases}
        x & \text{if } x > 0 \\
        0 & \text{otherwise}
      \end{cases}
    \]

    % LaTeX is a complex language with a lot of community-written libraries and
    % other features.  Conference articles and even entire books are often
    % written in this format.  There's a lot one could learn about this system,
    % but the above should be a good starting point for CS 41.
    %
    % And as an added bonus, you can keep more of your homework on your hard
    % drive instead of on paper.  :)
\end{document}
