CSS

Sunday, November 22, 2009

Haskell Annotations

I am starting to play around with the Haskell programming language, it is based off of category theory (well very loosely). There are a few questions I have for any Haskell gurus and category theory mathematicians, namely with regards to the existence of a terminal object in Hask (the category of Haskell data types and programs).

The underlying aim, I must confess, is to write up some abstract algebra in Haskell. For the scheme of things, I must give a reference:

  • Saunders Mac Lane, Categories for the Working Mathematician, Springer-Verlag (2000).
The question I want to answer is this: to what extent can we use Haskell to model abstract algebra in a universal algebraic manner?

First let us review some concepts from category theory (and a little Haskell), then we will dive into my rambling questions.

Review Of Category Theory

To repent for my sins, I will review category theory first before launching into my twisted approach to Haskell.

Let us review two propositions, the first from chapter III "Universals and Limits, section 5 "Categories with Finite Products":

Proposition. If a category C has a terminal object T and a product diagram AA×BB for any objects A,BC, then C has all finite products.

For an introduction to category theory in Haskell syntax, the reader is referred to:

There are a few reservations I have about those series of blog posts, for example in Part I the author states "In Haskell, a terminal object may be a phantom type: data T since T is containing only the element undefined." There is a different way to present the terminal object, it's the unit object ().

In fact, now that I think of it, the unit object () as the terminal object is more in line with category theory than the phantom type. If we refer to part II, we have the product correspond to (A,B). If we use Mac Lane's insight:

In particular, C has a product of no objects, which is simply a terminal object T in C, as well as a product for any two objects.
Source: Saunders Mac Lane, Categories for the Working Mathematician
Chapter III Universals and Limits, Section 5 "Categories with Finite Products".

It would follow that the terminal object would correspond to the unit object. Well, to be strict, it would be (,) which is isomorphic to ().

We use Mac Lane's other insight about monoids and groups from Chapter III, section 6 "Groups in Categories", which boils down to this: a monoid in C is a triple (C, μ:C×CC, η:TC) where T is the terminal object of C, μ is multiplication, and η is the "identity element" of C.

Using the jargon of "stuff, structure, and properties", we have the stuff be C an object of a category, the structure be two morphisms μ and η, and the properties be diagrams.

A group in C is a monoid (C, μ, η) together with a morphism ζ:CC which makes a diagram commute (intuitively it sends each element of C to its (right) inverse).

Now for something slightly different: if we take the product of two objects A and B in Hask to be (A,B), then Hask is closed under finite products.

Conjecture. The category Hask is closed under finite products.

Haskell Question

Now, we have precisely enough knowledge to consider the questions relevant to abstract algebraic Haskell programming.

Naively, I would want to say that in Haskell typeclasses are structure-types. That is, we can write

TerminalObject :: ()

class Monoid C where
    mu :: C -> C -> C
    eta :: TerminalObject -> C

However, we cannot specify anything about the implementation of class Monoid. It is frustratingly close, we have the stuff C, we have the structure, but alas no properties!

Question 1: Is there any way around this or am I doomed? More precisely, is there any way to specify behavior without specifying implementation?

Now, consider the following additional code snippet:

class (Monoid C) => Group C where
    zeta :: C -> C

Question 2: Can I specify the behavior of zeta without specifying implementation, while assuming that mu and eta have been implemented already?

Haskell is so frustratingly close to being a good language, but it fails at some of these key things.

Saturday, September 26, 2009

School Starts!

Just a small update, for those that are interested in me rambling some more on category theory. Sadly school has started. So I need to focus on that.

So I will be posting irregularly henceforth, don't expect regular updates, etc. etc. etc.

But when time allows, I shall resume writing some notes.

Thursday, August 27, 2009

Morphisms of...Morphisms? A First Look at Slice Categories

So to basically summarize the pattern that we have been using: we introduced mathematical objects, then some generalized notion of mappings between objects which we called "morphisms". We then collected a bunch of objects and morphisms together such that a bunch of nice properties were true, and we called it a category.

A category "is-a" mathematical object, so we figured out what morphisms between categories were: functors, or mathematical procedures. We then said "Hey, a functor 'is-a' mathematical object, what's a morphism between them?" It turns out it's a natural transformation.

But a morphism is-a mathematical object. So what's a "morphism" between morphisms? Is this a "meaningful" question? (It is, but first some references!)

  • Saunders Mac Lane, Categories for the Working Mathematician, Chapter II.6 "Comma Categories"
  • The Catsters, Youtube Lecture Series "Slice and Comma Categories", 1, 2

(We hope that the reader can see the pattern here, in category theory we started out with remarkably simple, almost comical, premises. We have objects, and we have morphisms. We group all objects of the same sort together into a category. But we can apply this powerful notion of a morphism to any object, and everything in math "is-a" mathematical object. We can then ask, for each object, what's the morphism of this thing? What's a morphism of this morphism? And so on...)

Category of Objects Under and Over a Specific Object

Now, to discuss morphisms between objects, we need to demand that they are the "same sort" of objects. How to do this? Well, we can demand that the morphisms are from one fixed object or dually they go to one fixed object.

More precisely, let bC be a fixed object, we can construct "the category of objects under $b$" denoted by $(b\downarrow\mathbf{C})$. Basically the objects are ordered pairs $\langle f,c\rangle$ consisting of morphisms from $b$ and the target of the morphism, and the morphisms are morphisms of the target $h:\langle f,c\rangle\to\langle f',c'\rangle$...well, this doodle should summarize things nicely:

Composition of morphisms is given by composition of these triangles. More precisely, the composition of the morphisms in the base of the triangle. To be formal, lets give the full definition:

Definition 1. Let C be a category, bC be some element. Then a "category of objects under b" consists of
  1. (Objects) the collection of ordered pairs ⟨f,c⟩ where $f:b\to{c}$ is a morphism in C, and cC is an object;
  2. (Morphisms) the morphisms are h: ⟨f,c⟩ → ⟨f',c'⟩
such that the following diagram specifies this

Consider any one point set $*$, and any other set $X$. The function $*\to{X}$ is a single element of $X$. The category of objects under $*$ consist of objects $\langle *\to{X},X\rangle$ which is a selected element of $X$, and the set $X$. This is precisely Set*, the first part of the ordered pair (the morphism) is the "base point", the second part of the ordered pair is the set itself.

Now, as alluded to earlier, we can reverse the direction of the arrows by "duality". This ends up letting us consider morphisms with the same target.

That is, if we have a category C and an object aC, then we can construct a category with the objects being ⟨c,f⟩ where f : ca is a morphism.

The morphisms in this category would be "morphisms of morphisms". More precisely, it would make the following diagram (on the right) commute:

As we reversed the direction of the arrows, we should probably distinguish this sort of category of objects over a from the category of objects under b. We denote it by (Ca), compared to (bC) for objects under b.

Formally, this is summarized in the following definition:

Definition 2. Let C be a category, aC be some element. Then a "category of objects over a" denoted by (Ca) consists of
  1. (Objects) the collection of ordered pairs ⟨f,c⟩ where $f:c\to{a}$ is a morphism in C, and cC is an object;
  2. (Morphisms) the morphisms are h: ⟨f,c⟩ → ⟨f',c'⟩
such that the following summarizes the category:

Observe that this is just the same as the category of objects under b, except we have the arrows be reversed.

We should consider, to be kosher, what (Set*) is. The objects are ordered pairs, as before, consisting of ⟨X → *,*⟩ where the first component of the ordered pair is unique. There is always a map in Set from X to a singleton *, it's very boring though -- it's the constant map. It's a unique map too (this is kind of trivial, where else could it map stuff to? There's only one choice!).

The morphisms are really just morphisms between the sets.

Lets think for a second: for each object XSet there is a unique object ⟨X → *,*⟩ in (Set*); and for each morphism fhomSet(X,Y) there is a corresponding morphism in (Set*).

Since there is a unique one-to-one correspondence between objects and between morphisms in the two categories, we can represent this by a functor which is invertible. What does this mean? It's an isomorphism...i.e. (Set*) is isomorphic to Set!!!

Remark: Notation

Apparently Mac Lane uses two notations. One is what we've seen. The other is writing C/c for (C ↓ c), and (one assumes) c/C for (c ↓ C). Just be warned there are two different notations (apparently).

[More to follow...apologies to those who would've liked more, duty calls at my job]

Wednesday, August 19, 2009

Emacs Latex Macros

Oftentimes I write LaTeX notes in the emacs editor, and I often do it on an Asus EEE PC 900 HA. Unfortunately, the keyboard is rather small, and my hands rather large, which leads to some complications. To make things worse, the "\" key is as wide as a fingernail's width (which is a nightmare for LaTeX coding!).

Fortunately there is a solution with Emacs: macros! I've written up a few macros that simplifies my life. First I have an texHelp.el file in my ~/.emacs.d/ directory which consists of the following stuff:

;; macro to help me write stuff
(defun write (mystring)
  (insert-string mystring)
  (newline))

;; starts the document class
(defun docclass ()
  (interactive)
  (write "\\documentclass{amsart}"))

;; use a package
(defun use-package (package)
  (interactive)
  (write (concat
    "\\usepackage{"
    package
    "}")))

;; all the packages of interest
(defun my-packages ()
  (interactive)
  (use-package "url")
  (use-package "manfnt")
  (use-package "amsthm")
  (use-package "amsmath")
  (use-package "amsthm")
  (use-package "amssymb")
  (use-package "amsfonts")
  (use-package "amscd")
  (use-package "graphicx")
  (write "\\DeclareGraphicsRule{*}{mps}{*}{}")
  (use-package "mathrsfs")
  (newline))

;; amsthm package configuration
(defun thm-style ()
  (interactive)
  (write "\\theoremstyle{definition}")
  (write "\\newtheorem{defn}{Definition}")
  (write "\\newtheorem{thm}{Theorem}")
  (write "\\newtheorem{rmk}{Remark}")
  (write "\\newtheorem{lem}{Lemma}")
  (write "\\newtheorem{cor}{Corollary}")
  (write "\\newtheorem{ex}{Example}")
  (write "\\newtheorem{nonex}[ex]{NON-Example}")
  (write "\\newtheorem{prop}{Proposition}")
  (write "\\newtheorem{sch}{Scholium}")
  (write "\\newtheorem{axm}{Axiom}")
  (write "\\newtheorem*{prob}{Problem}")
  (newline))

;; number equations within the section
(defun eqn-numbering ()
  (interactive)
  (write "\\numberwithin{equation}{section}")
  (newline))

;; my custom defined macros
(defun macros ()
  (interactive)
  ;;;;;;;;;;;;;;;;;;;;;;
  ;; code snipped out ;;
  ;;;;;;;;;;;;;;;;;;;;;;
  (newline))

(defun doc-stuff ()
  (interactive)
  (write "\\title[]{}")
  (write (concat "\\date{" (format-time-string "%B %d, %Y") "}"))
  (write (concat "\\email{" user-email-address "}"))
  (write (concat "\\author{" user-full-name "}"))
  (write "\\begin{document}")
  (write "\\maketitle")
  (newline)
  (newline)
  (write "\\end{document}"))

;; call this function when starting a new LaTeX document
(defun start-tex ()
  (interactive)
  (docclass)
  (my-packages)
  (eqn-numbering)
  (thm-style)
  (macros)
  (doc-stuff)
  (previous-line 5))

I only need to create the directory (usually I have a ~/notebk directory, and subdirectories ordered in the way arXiv is ordered, so I usually create sub-sub-directories of ~/notebk/*), then create the file with C-x C-f path and start TeXnical stuff with M-x start-tex (remember that M-x means "Alt-x").

This does not solve the problem of the tex macros, I would still have to write "\begin{equation}...\end{equation". Woah is me!

Fortunately Emacs macros comes to the rescue yet again! I have another file, ~/.emacs.d/texMacros.el which consists of the helper functions:

;; for subequations
(defun subeqn()
  (interactive)
  (insert "\\begin{subequations}\n")
  (insert "\\begin{align}\n")
  (insert "\n")
  (insert "\\end{align}\n")
  (insert "\\end{subequations}")
  (previous-line 2))

;; for equations
(defun eqn()
  (interactive)
  (insert "\\begin{equation}%\\label{eq:}\n")
  (insert "\n")
  (insert "\\end{equation}")
  (previous-line 1))

;; all the amsthm environment
(defun thm()
  (interactive)
  (insert "\\begin{thm}%\\label{thm:}\n")
  (insert "\n")
  (insert "\\end{thm}\n")
  (insert "\\begin{proof}\n\n")
  (insert "\\end{proof}")
  (previous-line 4))
(defun defn()
  (interactive)
  (insert "\\begin{defn}%\\label{defn:}\n")
  (insert "\n")
  (insert "\\end{defn}")
  (previous-line 1))
(defun cor()
  (interactive)
  (insert "\\begin{cor}%\\label{cor:}\n")
  (insert "\n")
  (insert "\\end{cor}")
  (previous-line 1))
(defun lem()
  (interactive)
  (insert "\\begin{lem}%\\label{lem:}\n")
  (insert "\n")
  (insert "\\end{lem}")
  (previous-line 1))
(defun rmk()
  (interactive)
  (insert "\\begin{rmk}\n")
  (insert "\n")
  (insert "\\end{rmk}")
  (previous-line 1))
(defun prop()
  (interactive)
  (insert "\\begin{prop}%\\label{prop:}\n")
  (insert "\n")
  (insert "\\end{prop}")
  (previous-line 1))
(defun ex()
  (interactive)
  (insert "\\begin{ex}\n")
  (insert "\n")
  (insert "\\end{ex}")
  (previous-line 1))
(defun nonex()
  (interactive)
  (insert "\\begin{nonex}\n")
  (insert "\n")
  (insert "\\end{nonex}")
  (previous-line 1))

;; lists macros
(defun enumerate()
  (interactive)
  (insert "\\begin{enumerate}\n")
  (insert "\\item \n")
  (insert "\\end{enumerate}")
  (previous-line 1))
(defun itemize()
  (interactive)
  (insert "\\begin{itemize}\n")
  (insert "\\item \n")
  (insert "\\end{itemize}")
  (previous-line 1))
(defun item()
  (interactive)
  (insert "\\item "))

;; proofs, from the amsthm package
(defun pf()
  (interactive)
  (insert "\\begin{proof}\n")
  (insert "\n")
  (insert "\\end{proof}")
  (previous-line 1))
(defun axm()
  (interactive)
  (insert "\\begin{axm}%\\label{axm:}\n")
  (insert "\n")
  (insert "\\end{axm}")
  (previous-line 1))
(defun prob()
  (interactive)
  (insert "\\begin{prob}\n")
  (insert "\n")
  (insert "\\end{prob}")
  (previous-line 1))

;; reference to an equation
(defun eqref()
  (interactive)
  (insert "\\eqref{}")
  (backward-char 1))
(defun pmatrix()
  (interactive)
  (insert "\\begin{pmatrix}\n")
  (insert "\n")
  (insert "\\end{pmatrix}")
  (previous-line 1))
(defun diagram()
  (interactive)
  (insert "\\begin{figure}[H]\n")
  (insert "\\includegraphics{}\n")
  (insert "\\end{figure}")
  (previous-line 2)
  (forward-char 23))
(defun fig()
  (interactive)
  (diagram))
(defun figure()
  (interactive)
  (fig))
(defun src()
  (interactive)
  (insert "\\begin{Verbatim}[fontsize=\\footnotesize,frame=lines,numbers=left,numbersep=3pt,firstnumber=]\n")
  (insert "\n")
  (insert "\\end{Verbatim}")
  (previous-line 2)
  (forward-char 77))
(defun quotation()
  (interactive)
  (insert "\\begin{quote}\n")
  (insert "\n")
  (insert "\\end{quote}")
  (previous-line 1))

And shazam! I'm off to the races, avoiding usage of the "\" key with only a handful of exceptions.

If one wants to have a bunch of keybindings for these functions, one can use the following in their ~/.emacs file:

;; load texMacros when doing LaTeX stuff
(load "~/.emacs.d/texHelp.el")
(load-file "~/.emacs.d/texMacros.el")  ; load these LaTeX preferences
(defun my-LaTeX-startup ()
  (interactive)
  (local-unset-key "\C-c\C-e")
  (local-set-key "\C-c\C-e" 'eqn)
  (local-unset-key "\C-c\C-s")
  (local-set-key "\C-c\C-s" 'subeqn)
  (local-unset-key "\C-c\C-t")
  (local-set-key "\C-c\C-t" 'thm)
  (local-unset-key "\C-c\C-d")
  (local-set-key "\C-c\C-d" 'defn)
  (local-unset-key "\C-c\C-r")
  (local-set-key "\C-c\C-r" 'rmk)
  (local-unset-key "\C-c\C-l")
  (local-set-key "\C-c\C-l" 'lem)
  (local-unset-key "\C-c\C-k")
  (local-set-key "\C-c\C-k" 'cor)
  (local-unset-key "\C-c\C-p")
  (local-set-key "\C-c\C-p" 'prop)
  (local-unset-key "\C-c\C-x")
  (local-set-key "\C-c\C-x" 'ex)
  (local-unset-key "\C-c\C-n")
  (local-set-key "\C-c\C-n" 'nonex))
(add-hook 'LaTeX-mode-hook 'my-LaTeX-startup)
(add-hook 'latex-mode-hook 'my-LaTeX-startup)
(add-hook 'TeX-mode-hook 'my-LaTeX-startup)

Of course, one is more than welcome to change the keybindings to their own preference!

Addendum: Making the Definition Macro Better!

Recall for our object oriented approach, we have a mathematical object consist of "stuff" equipped with some "structure" such that a bunch of "properties" hold. We can modify the definition environment to simplify this format. In ~/.emacs.d/texMacros.el, change the (defun defn() ...) lines to be:

(defun defn()
  (interactive)
  (insert "\\begin{defn}%\\label{defn:}\n")
  (insert "A \\textbf{} consists of\n")
  (insert "\\begin{enumerate}\n\\item\n\\end{enumerate}")
  (insert "\n equipped with\n")
  (insert "\\begin{enumerate}\n\\item\n\\end{enumerate}")
  (insert "\n such that\n")  
  (insert "\\begin{enumerate}\n\\item\n\\end{enumerate}")
  (insert "\n\\end{defn}")
  (previous-line 10))

You're set to start defining objects left and right!

Thursday, August 13, 2009

LaTeX Macros...

So here are some LaTeX3 macros I've found or written that are pretty useful. This post covers the following packages I've written/patched together:

  • Bourbaki inspired dangerous bend environments
  • Macros for underbrackets and overbrackets, similar to underbrace and overbrace
  • Misner, Thorne, and Wheeler type equations
  • Exercises and Answers in the style of the TeXbook
  • Style like the TeXbook in LaTeX macros

Addendum (9:30 AM (PST) 15 December 2011): I have modified the danger.sty code, and it is now more robust. For more macros, see my Notebk's wiki page for others and documentation.

Dangerous Bends!

Bourbaki used Dangerous Bend symbols to indicate some tricky reasoning. Knuth used this too in his TeX book, among other places.

I too use it in my notes...but I use it as an environment. It's safer this way ;) At any rate the code is contained a file danger.sty, reproduced below:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{danger}[2009/08/06 Danger and Double Danger Environments]

\usepackage{manfnt}
% or if manfnt is unavailable, uncomment the next two lines
%\font\manual=manfnt
%\def\dbend{{\manual\char127}} % dangerous bend sign

%%
% This macro header is what controls the ``dangerous bend''
% paragraph
%%

% Danger, Will Robinson!
\newenvironment{danger}{\medbreak\noindent\hangindent=2pc\hangafter=-2%
  \clubpenalty=10000%
  \hbox to0pt{\hskip-\hangindent\dbend\hfill}\small\ignorespaces}%
  {\medbreak\par}

% Danger! Danger!
\newenvironment{ddanger}{\medbreak\noindent\hangindent=3pc\hangafter=-2%
  \clubpenalty=10000%
  \hbox to0pt{\hskip-\hangindent\dbend\kern2pt\dbend\hfill}\small\ignorespaces}%
  {\medbreak\par}

The above code is just literally cut/paste from Knuth's TeXbook macros. On the other hand, in LaTeX a better implementation might be:

\font\manual=manfnt
\def\dbend{{\manual\char127}} % dangerous bend sign

% Danger, Will Robinson!
\newenvironment{danger}{\medbreak\noindent\hangindent=2pc\hangafter=-2%
  \clubpenalty=10000%
  \hbox to0pt{\hskip-\hangindent\dbend\hfill}\small\ignorespaces}%
  {\medbreak\par}

% Danger! Danger!
\newenvironment{ddanger}{\medbreak\noindent\hangindent=3pc\hangafter=-2%
  \clubpenalty=10000%
  \hbox to0pt{\hskip-\hangindent\dbend\kern2pt\dbend\hfill}\small\ignorespaces}%
  {\medbreak\par}

Underbrackets and Overbrackets

In math mode, you can use underbrace and overbrace, but there are no corresponding brackets macros. This is sad, because I'm more fond of brackets than I am of braces.

So I fiddled around and pieced together the brackets.sty file:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{brackets}[2009/08/06 Overbracket and Underbracket racket]

\makeatletter
\def\overbracket{\@ifnextchar [ {\@overbracket} {\@overbracket
[\@bracketheight]}}
\def\@overbracket[#1]{\@ifnextchar [ {\@over@bracket[#1]}
{\@over@bracket[#1][0.3em]}}
\def\@over@bracket[#1][#2]#3{%\message {Overbracket: #1,#2,#3}
\mathop {\vbox {\m@th \ialign {##\crcr \noalign {\kern 3\p@
\nointerlineskip }\downbracketfill {#1}{#2}
                              \crcr \noalign {\kern 3\p@ }
                              \crcr  $!\hfil$ \displaystyle {#3}\hfil $%
                              \crcr} }}\limits}
\def\downbracketfill#1#2{$!\m@th$ \setbox \z@ \hbox {$!\braceld$$}
                  \edef\@bracketheight{\the\ht\z@}\downbracketend{#1}{#2}
                  \leaders \vrule \@height #1 \@depth \z@ \hfill
                  \leaders \vrule \@height #1 \@depth \z@ \hfill
\downbracketend{#1}{#2}$}
\def\downbracketend#1#2{\vrule depth #2 width #1\relax}


\def\underbracket{%
  \@ifnextchar[{\@underbracket}{\@underbracket [\@bracketheight]}%
}
\def\@underbracket[#1]{%
  \@ifnextchar[{\@under@bracket[#1]}{\@under@bracket[#1][0.4em]}%
}
\def\@under@bracket[#1][#2]#3{%\message {Underbracket: #1,#2,#3}
 \mathop{\vtop{\m@th \ialign {##\crcr $!\hfil$ \displaystyle {#3}\hfil $!$%
 \crcr \noalign {\kern 3\p@ \nointerlineskip }\upbracketfill {#1}{#2}
       \crcr \noalign {\kern 3\p@ }}}}\limits}
\def\upbracketfill#1#2{$!\m@th$ \setbox \z@ \hbox {$!$\braceld$!$}
                    \edef\@bracketheight{\the\ht\z@}\bracketend{#1}{#2}
                    \leaders \vrule \@height #1 \@depth \z@ \hfill
                    \leaders \vrule \@height #1 \@depth \z@ \hfill \bracketend
               {#1}{#2}$}
\def\bracketend#1#2{\vrule height #2 width #1\relax}
\makeatother


% Makes limits on sums and integrals pretty
\def\mathllap{\mathpalette\mathllapinternal}
\def\mathllapinternal#1#2{\llap{$\mathsurround=0pt#1{#2}$}}
\def\clap#1{\hbox to 0pt{\hss#1\hss}}
\def\mathclap{\mathpalette\mathclapinternal}
\def\mathclapinternal#1#2{\clap{$\mathsurround=0pt#1{#2}$}}
\def\mathrlap{\mathpalette\mathrlapinternal}
\def\mathrlapinternal#1#2{\rlap{$\mathsurround=0pt#1{#2}$}}

The other part of the code, which % Makes limits pretty, allows stuff to be written on top of the sum's limits. It's taken from Voss's math mode notes.

Equations with Misner, Thorne and Wheeler type Arrows

This is going to be a bit harder to explain (it's one of those "You have to have seen it to understand what I'm talking about" type situations). So look at page 139, equation 5.15a for example. Note the arrows pointing to the terms in the equation? Yeah, I'd like to do that in LaTeX, but how?

Unfortunately you have to use the picture environment (or, at least, that's the only way I know how to do it!). Consider the following example document:

\documentclass{amsart}
\usepackage{brackets}

\newlength\textwidthcm
\textwidthcm=.03514598035146\textwidth

\begin{document}
{\catcode`p=12 \catcode`t=12 \gdef\cm#1pt{#1cm}}
{\catcode`p=12 \catcode`t=12 \gdef\dimensionless#1pt{#1}}

\begin{equation}\label{eq:one}
E=mc^{2}
\end{equation}


\setlength{\unitlength}{1cm}
\begin{picture}(\expandafter\dimensionless\the\textwidthcm, 2.5)(0,0)
  \linethickness{0.5pt}
  \put(5,1.5){$\displaystyle Z[0]=\underbracket[0.25pt]{\int ~\mathcal{D}\phi\;\;\; }
               \!\!\!\exp(\int\mathcal{L}d^{4}x)$}
  \put(-.4,1.5){\refstepcounter{equation}{(\arabic{equation})\label{eq:four}}}
  \put(6.75,0.55){\vector(0,1){0.4}}
  \put(6.75,0.55){\line(1,0){1.4}}
  \put(8.15,0.5){\makebox{$\begin{pmatrix}
                        $!sum$$\ 
                        $!over$$\\ 
                        $!histories$$
                        \end{pmatrix}$}}
\end{picture}
Woah what is eq \eqref{eq:four} again? Don't forget the text width is \the\textwidth ~or 
equivalently \expandafter\cm\the\textwidthcm ~or 
\expandafter\dimensionless\the\textwidthcm
\end{document}

This produces the following text:

The only disadvantage is that for each equation you want to do, you have to do this by hand.

Exercises and Answers Macros From The TeXbook

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{exercises}[2009/08/06 LaTeX version of exercise macros from the TeXbook]
%%%%%%
% Options: number within either the chapter or the part
%          default is to number the exercises/answers via sections
%%%%%%%%
\newif\if@dump
\@dumpfalse
\newif\if@section
\newif\if@ch@pter
\newif\if@p@rt
\@sectiontrue\@ch@pterfalse\@p@rtfalse
\DeclareOption{chapter}{\@sectionfalse\@ch@pterfalse\@p@rtfalse}
\DeclareOption{part}{}
\def\dump@nswer{0}
\DeclareOption{dump}{\@dumptrue}
\ProcessOptions\relax

\usepackage{manfnt}


\newcounter{sectionCtr}
\newcounter{exno}
\setcounter{exno}{0}


\refstepcounter{sectionCtr}
\if@section
\newcommand{\upd@teCtr}{\ifnum \value{sectionCtr}=\value{section}%
\refstepcounter{exno}\else\setcounter{exno}{1}\setcounter{sectionCtr}{\value{section}}\fi}
\else\if@ch@pter%
\newcommand{\upd@teCtr}{\ifnum \value{sectionCtr}=\value{chapter}
\refstepcounter{exno}\else\setcounter{exno}{1}\setcounter{sectionCtr}{\value{chapter}}\fi}
\else\if@p@rt%
\newcommand{\upd@teCtr}{\ifnum \value{sectionCtr}=\value{part}%
\refstepcounter{exno}\else\setcounter{exno}{1}\setcounter{sectionCtr}{\value{part}}\fi}
\else\@sectiontrue\setcounter{exno}{1}\setcounter{sectionCtr}{\value{section}}
\newcommand{\upd@teCtr}{\ifnum \value{sectionCtr}=\value{section}%
\refstepcounter{exno}\else\setcounter{exno}{1}\setcounter{sectionCtr}{\value{section}}\fi}
\fi\fi\fi

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Exercise Environment
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% well, to make it an environment, one would instead do the following:
% \newenviornment{exercise}{\medbreak\upd@teCtr%
%  \noindent\llap{\mantriangleright\kern.15em}% triangle in margin
%  \small{\textbf{EXERCISE \thesection.\arabic{exno}}}\\%
%  \noindent}{}
% that is, stuff the command into an environment
\newcommand{\exercise}{\medbreak\upd@teCtr%
  \noindent\llap{\mantriangleright\kern.15em}% triangle in margin
  \small{\textbf{EXERCISE \thesection.\arabic{exno}}}\\%
  \noindent}
\newcommand{\dexercise}{\medbreak\upd@teCtr%
  \noindent\llap{\mantriangleright\kern.15em}% triangle in margin
  \small{\textbf{EXERCISE \arabic{sectionCtr}.\arabic{exno}}}\\%
  \noindent}
\newcommand{\dangerexercise}{\dbend \dexercise}
\newcommand{\ddangerexercise}{\dbend\dbend \dexercise}


% formatting macro
\if@dump
  \def\ansno#1.#2:{\medbreak\noindent%
    \hbox to\parindent{\bf\hss(Answer to #1.#2)\enspace}\ignorespaces}
\else
  \def\ansno#1.#2:{\medbreak\noindent%
    \hbox to \parindent{\bf\hss #1.#2.\enspace}\ignorespaces}
\fi

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Answers Command
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\if@dump % if we are dumping the answers directly where they're placed
  \def\ans{} % ans is defined as nothing
  \newcommand{\answer}[1]{\par\medbreak % \answer simply
      \ansno\arabic{sectionCtr}.\arabic{exno}: \\% prints directly
      #1} % out when it's called 
  \newcommand{\dumpanswers}{} % \dumpanswers is empty
\else % else we are dumping it into a file
  \newwrite\ans%
  \immediate\openout\ans=answers % file for answers to exercises
  \newcommand{\answer}[1]{\par\medbreak
    \immediate\write\ans{}%
    \immediate\write\ans{\string\ansno\arabic{sectionCtr}.\arabic{exno}:}%
    \immediate\write\ans{ \detokenize{#1}}}
  \newcommand{\dumpanswers}{\immediate\closeout\ans\input{answers}}
\fi

One could easily turn the exercises command into an environment, but it'd be trickier to turn the \answer command into an environment. The \answer spits out all the answers to a file answers.tex which can be included at the end of the main document.

To write the answers, one should call the \dumpanswers command in its own section/chapter. It does everything necessary to include the answers in the document.

The Poor Man's TeXbook Style

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{TeXbook}[2009/08/06 Poor man's \TeX{}book style for \LaTeX]

% the following is the TeXbook's exact specifications
%\usepackage[textheight=38pc,headsep=10pc,top=16pc,%
% textwidth=30pc,inner=6pc,marginparwidth=8pc,marginparsep=1cm]{geometry}
% the following is OUR preferred specifications!
\usepackage[top=6pc,textwidth=30pc,inner=6pc,%
marginparwidth=10pc,marginparsep=1cm]{geometry}
\normalbaselineskip=12pt
\baselineskip=12pt
\abovedisplayskip=6pt plus 3pt minus 1pt
\belowdisplayskip=6pt plus 3pt minus 1pt
\abovedisplayshortskip=0pt plus 3pt
\belowdisplayshortskip=4pt plus 3pt

\usepackage{fancyhdr,marginnote}

\pagestyle{fancy}
\if@twoside
\fancyhead[LE,RO]{\thepage}
\fancyheadoffset[OR,EL]{8pc}
\else
\fancyhead[L]{\S\thesection~\nouppercase{\rightmark}}
\fancyhead[R]{\thepage}
\fancyheadoffset[R]{8pc}
\renewcommand{\sectionmark}[1]{\markboth{}{#1}}
\fi
\cfoot{}
\renewcommand{\headrulewidth}{0.4pt}

\if@twoside
\renewcommand\marginpar[1]{\-\marginnote{\footnotesize{\emph{#1}}}}
\else
\renewcommand\marginpar[1]{\-\marginnote{\raggedright\footnotesize{\emph{#1}}}}%
\fi

This has a decent sized margin, the header is also slightly extended. It's really just a poor man's TeXbook style file, using the exact specifications from the manmac.tex macros file that is freely available from CTAN.

Note that the style file is not exactly as the TeXbook specifies, I thought the \headsep was too large. The textwidth and placement from the inner margin are the same, the marginparwidth is slightly larger so one can write annotations in the margins (I love using \marginpar).

I'm writing up some notes on slice and comma categories, as well as adjoint functors, so sit tight while I polish them up in the next couple of days...