% ____________________________________________________________________________
% MICROFOUNDATIONS OF FINANCIAL ECONOMICS
% An Introduction to General Equilibrium Asset Pricing

% Yvan Lengwiler
% for Princeton University Press
% ____________________________________________________________________________

\listfiles

% ____________________________________________________________________________
% LAYOUT

\documentclass[12pt,titlepage,fleqn,a4paper,twoside,openright,final]{book}

% ____________________________________________________________________________
% LANGUAGE

% This is not necessary, so I don't load it.
% \usepackage[english]{babel}

% ____________________________________________________________________________
% TYPEFACES

\usepackage[cmex10]{amsmath}           % AMS-LaTeX math extensions and improvements
\usepackage{amssymb}                   % AMS symbols

\usepackage{pxfonts,mathpazo}          % Palladino fonts for text and math
\usepackage{eucal}                     % Euler \mathcal

\usepackage{url}                       % easily typeset uniform resource locators

% ____________________________________________________________________________
% GEOMETRY

\setlength{\topmargin}{-0.3in}
\setlength{\headsep}{1in}

% TWO-SIDED LAYOUT:
% make equal margins on even and odd pages...
\addtolength{\oddsidemargin}{\evensidemargin}
\setlength{\oddsidemargin}{.5\oddsidemargin}
\setlength{\evensidemargin}{\oddsidemargin}
% ...then add a binding offset
\addtolength{\oddsidemargin}{0.4cm}

\setlength{\parindent}{0.35in}
\newlength\keepparindent
\setlength{\keepparindent}{\parindent}

\setlength{\mathindent}{\parindent}    % use only with option fleqn
\setlength{\leftmargini}{\parindent}

% ____________________________________________________________________________
% GENERAL MACROS

% vertical spacing
\def\spacing#1{\renewcommand{\baselinestretch}{#1}\rm}
\long\def\note#1{\footnote{\spacing{1}#1}}

% comments (for draft versions)
%\usepackage[normalem]{ulem}         % \sout: strikeout; \uwave: waveline
%\def\cmt#1{{\sf\textcolor{red}{[\protect\uwave{#1}]}}}  % cannot make this \long\def
%\long\def\hide#1{\relax\ignorespaces}

% use triangle in lists instead of default bullet
\renewcommand{\labelitemi}{{\small$\blacktriangleright$}}
\renewcommand{\labelitemii}{{\small$\triangleright$}}

% shortcuts
\def\ie{i.e.\ }
\def\eg{e.g.,\ }

% ____________________________________________________________________________
% SECTIONING and NUMBERING

\usepackage{sectsty}

\def\extremeraggedright{\raggedright\let\rightskip\textwidth}
    \chapternumberfont{\chapterheading}
    \sectionfont{\color{dark}\extremeraggedright}
    \subsectionfont{\bfseries\slshape\color{dark}\extremeraggedright}
    \subsubsectionfont{\bfseries\color{dark}\extremeraggedright}
\def\chapterheading#1{\addtocontents{lob}{\removelastskip\vskip0.5\baselineskip}% add an empty half line to lost of boxes
    \thispagestyle{plain}\normalsize\spacing{1}\vspace*{-2cm}\noindent\huge\color{dark}{\it #1}\\[-12pt]}

% style of chapter, section, subsection numbering
\renewcommand{\thechapter}{\arabic{chapter}}
\renewcommand{\thesection}{\mbox{\thechapter\,--\,\arabic{section}}}
\renewcommand{\thesubsection}{\upshape\emph{\alph{subsection}})}
\setcounter{secnumdepth}{2}
\setcounter{tocdepth}{2}
% ... alternative
%\setcounter{secnumdepth}{1}     % number only chapters and sections, not subsections
%\setcounter{tocdepth}{1}        % chapters and sections in toc, no subsections

% running headline
% ONE-SIDED LAYOUT:
% \renewcommand{\chaptermark}[1]{\markright{\scshape\lowercase{\chaptername~\thechapter\ --- #1}}}
% \renewcommand{\sectionmark}[1]{\relax}
% TWO-SIDED LAYOUT:
\renewcommand{\chaptermark}[1]{\markboth{\scshape\lowercase{\chaptername~\thechapter}}{\scshape\lowercase{#1}}}
\renewcommand{\sectionmark}[1]{\relax}

% equation numbers
\renewcommand{\theequation}{\thechapter.\arabic{equation}}

% prevent split of section number
\makeatletter
    \renewcommand{\@seccntformat}[1]{\protect\makebox[0pt][r]{\csname the#1\endcsname\quad}}
\makeatother

% flush left figure and table captions
\makeatletter
    \renewcommand{\@makecaption}[2]{\bigskip\normalcolor\noindent {\bf #1}\quad #2\par}
\makeatother

% ____________________________________________________________________________
% COLORS --- implement color scheme

\ifx\pdfoutput\undefined
    \usepackage{color}              % color support
    \usepackage{colortbl}           % color in tables
    \let\pdfoutput\undefined
\else
    \usepackage{color}              % color support
    \usepackage{colortbl}           % color in tables
\fi

% gray:
    \definecolor{light}{gray}{0.88}
    \definecolor{dark}{gray}{0}

% alternatively, the "light" and "dark" colors can be
% defined to actually produce color:
% ---------------------------------------------------
%
% green:
%    \definecolor{dark}{rgb}{0,0.55,0}
%    \definecolor{light}{rgb}{0.8,1,0.8}
%
% blue:
%    \definecolor{dark}{rgb}{0,0,0.6}
%    \definecolor{light}{rgb}{0.88,0.88,1}
%
% red:
%    \definecolor{dark}{cmyk}{0,1,1,0.5}
%    \definecolor{light}{cmyk}{0,0.17,0.17,0}

% ____________________________________________________________________________
% GRAPHICS

% this package is needed only for the rotation commands
% no graphics are imported
\ifx\pdfoutput\undefined
    \usepackage{graphicx}
    \let\pdfoutput\undefined
\else
    \usepackage{graphicx}
\fi

% ____________________________________________________________________________
% PICTEX

\usepackage{pictex}
    \renewcommand{\topfraction}{1}
    \renewcommand{\textfraction}{0}
    \def\stdarrow from #1 #2 to #3 #4 {%
        \put{\null} at #1 #2
        \put{\null} at #3 #4
        \arrow <2mm> [.2,.67] from #1 #2 to #3 #4
    }
    \def\trstroke[#1,#2]#3{\put {$\diagup$} [bl] at #1 #2 \put {#3} [b] <4mm,4.2mm> at #1 #2 }
    \def\tlstroke[#1,#2]#3{\put {$\diagdown$} [br] at #1 #2 \put {#3} [b] <-4mm,4.2mm> at #1 #2 }
    \def\blstroke[#1,#2]#3{\put {$\diagup$} [tr] at #1 #2 \put {#3} [t] <-4mm,-4.2mm> at #1 #2 }
    \def\brstroke[#1,#2]#3{\put {$\diagdown$} [tl] at #1 #2 \put {#3} [t] <4mm,-4.2mm> at #1 #2 }
\newenvironment{pictex}{\mbox\bgroup\normalcolor\beginpicture}{\endpicture\egroup}

% ___________________________________________________________________________________
% MACROS FOR DRAWING INFORMATION TREES

\def\rootstate#1 #2 #3 {
    \put {$\bullet$} at #2 #3
    \put {#1} [r] <-3mm,0pt> at #2 #3
    }
\def\event#1 #2 #3 {
    \put {$\bullet$} at #2 #3
    \put {event #1} [b] <0pt,3mm> at #2 #3
    }
\def\bevent#1 #2 #3 {
    \put {$\bullet$} at #2 #3
    \put {event #1} [t] <0pt,-3mm> at #2 #3
    }
\def\finalstate#1 #2 #3 {
    \put {$\bullet$} at #2 #3
    \put {state #1} [l] <3mm,0pt> at #2 #3
    }

% ____________________________________________________________________________
% MATHEMATICS

% equations with labels on both sides
\def\lrdisplay#1#2#3{\hbox to\hsize{\hbox to0pt{#1\hss}%
    \hss$\displaystyle{#2}$\hss\hbox to0pt{\hss$#3$}}}

% slash fraction
\def\sfrac#1#2{\leavevmode\raise.5ex\hbox{$\scriptstyle{#1}$}
    \kern-.15em/\kern-.15em
    \lower.25ex\hbox{$\scriptstyle{#2}$}}

% math symbols
\def\Real{\mathbb R}                        % AMS-TeX version
\let\lesseq\leqslant    \let\gtreq\geqslant % AMS-TeX version
\let\vlless\ll          \let\vggtr\gg
\let\vless\leq          \let\vgtr\geq
\let\vlesseq\leqq       \let\vgtreq\geqq

\let\nothing\varnothing                     % AMS-TeX version

\def\inv{^{-1}}
\def\bigtimes{\mathop{\diagup\mkern-16mu\diagdown}}
\def\var{{\rm var}}
\def\cov{{\rm cov}}
\def\corr{\textrm{corr}}
\def\argmax{\mathop{\hbox{\rm arg\,max}}}
\def\argmin{\mathop{\hbox{\rm arg\,min}}}

\let\implies\Rightarrow \let\imply\implies \let\sufficientfor\implies \let\OnlyIf\implies
\let\impliedby\Leftarrow \let\necessaryfor\impliedby \let\If\impliedby
\let\Iff\Leftrightarrow

% greek letters
\let\dd\d   % save dot accent
\let\a\alpha \let\b\beta \let\d\delta \let\g\gamma \let\eps\varepsilon \let\k\kappa
\let\l\lambda \let\t\tau \let\w\omega \let\p\pi \let\f\phi \let\r\rho \let\s\sigma
\let\m\mu \let\x\xi \let\z\zeta
\let\D\Delta \let\L\Lambda \let\W\Omega \let\X\Xi

% very special macros
\def\e{e}
\def\eye{\mathbf{e}}    % identity matrix
\def\agr{\sum_{i=1}^I}  % sum over all agents
\def\period#1{\langle#1\rangle}
\def\hiperiod#1{^{\period{#1}}}
\def\loperiod#1{_{\period{#1}}}

% ____________________________________________________________________________
% PROCLAIMS

\usepackage{theorem}
    \theoremstyle{plain}
    \theoremheaderfont{\normalfont\bfseries}

\theorembodyfont{\itshape}
\newtheorem{defn}{Definition}
    \renewcommand{\thedefn}{\ignorespaces\rm}    % not numbered
\newtheorem{thm}{Theorem}
    \renewcommand{\thethm}{\thechapter--\arabic{thm}}
\newtheorem{prop}[thm]{Proposition}
\newtheorem{lem}[thm]{Lemma}
\newtheorem{cor}[thm]{Corollary}
\newtheorem{conj}[thm]{Conjecture}
\newtheorem{rmk}{Remark.}
    \renewcommand{\thermk}{\ignorespaces\rm}    % not numbered
\newenvironment{proof}[1][Proof]{\bigbreak\noindent\textit{#1.}\quad}{\nobreak\hfil\penalty50\parfillskip=0pt
    \hskip1.5em\null\hfil\textrm{QED}\finalhyphendemerits=0\par\bigbreak}

% ____________________________________________________________________________
% REFERENCES

\def\boxref#1{Box~\ref{box:#1}}
\def\figref#1{Figure~\ref{fig:#1}}
\def\tblref#1{Table~\ref{tbl:#1}}
\def\chapref#1{chapter~\ref{chap:#1}}
\def\sectref#1{section~\ref{sect:#1}}
\def\Chapref#1{Chapter~\ref{chap:#1}}
\def\Sectref#1{Section~\ref{sect:#1}}

\let\originaleqref\eqref
\def\eqref#1{\originaleqref{eq:#1}}

% ____________________________________________________________________________
% PROBLEMS AND SOLUTIONS

\usepackage{answers}
    \Newassociation{solution}{Solution}{ans}

\theorembodyfont{\normalfont}
\newtheorem{example}{Example.}
\renewcommand{\theexample}{}    % no numbers

\theorembodyfont{\normalfont\sl}
\newtheorem{problem}{Problem}[chapter]
\renewcommand{\theproblem}{\thechapter--\arabic{problem}}

\def\subproblem#1{\smallskip{\rm(\it #1\/\rm)}\hbox to1ex{\hss}}
\def\resetproblem{\setcounter{problem}{0}}

% ____________________________________________________________________________
% INDEX

%\usepackage{makeidx}
\usepackage{index}
    \makeindex

%\usepackage[biblabels]{authorindex}    % doesn't work

%\proofmodetrue                         % print indexed words as marginal notes

% ____________________________________________________________________________
% BIBLIOGRAPHIES

%\usepackage{citeref}   % doen't work with natbib

\newcommand{\bysame}{\leavevmode\hbox to3em{\hrulefill\,\,}}

\usepackage{natbib}
    \bibliographystyle{AER}

\usepackage{chapterbib}

\renewcommand{\bibsection}{\section*{References}\addcontentsline{toc}{section}{References}}

\let\simplebibliography\bibliography

\def\makelocalbib{%
    \citeindexfalse
    \bibliographystyle{AER}
    \simplebibliography{book}
    \citeindextrue
    }

\def\citesy#1{\citeauthor{#1}'s (\citeyear{#1})}

% ____________________________________________________________________________
% LEARNING BOX

    \reversemarginpar   % I want marginal paragraphs to be on the left side
    \addtolength{\oddsidemargin}{0.375in}
    \setlength{\marginparwidth}{1in}

\setlength{\fboxsep}{1.5mm}
\newlength{\fboxwidth}
    \fboxwidth=\hsize
    \advance\fboxwidth by-2\fboxsep

\newcounter{learnbox}[chapter]
\renewcommand{\thelearnbox}{\hbox{\arabic{chapter}\,--\,\arabic{learnbox}}}

\long\def\learnbox#1#2{%
    \refstepcounter{learnbox}%
    \bigbreak\noindent%
    {\llap{\colorbox{dark}{\textcolor{white}{\hbox to0.8in{\hss\bf\vphantom{$/^b_b$}BOX~\thelearnbox\hss}}}\hspace{\marginparsep}}}
    \hspace*{-0.8mm}%     WHY IS THIS NECESSARY???
    \hbox{\ignorespaces\colorbox{light}{\ignorespaces\protect\begin{minipage}[t]{\fboxwidth}
    \noindent\vphantom{$/^b_b$}\textbf{#1.}\quad\setlength{\parindent}{\keepparindent} #2
    \addcontentsline{lob}{box}{\protect\numberline{\thelearnbox}{#1}}
    \end{minipage}\par}%
    }%
    \bigbreak}

% ____________________________________________________________________________
% HACKS

\makeatletter

% --- \cleardoublepage -------------------------------------------------------

% make pagestyle "empty" on empty pages
\def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else
    \hbox{}\thispagestyle{empty}\newpage\if@twocolumn\hbox{}\newpage\fi\fi\fi}

% here's the opposite command: it forces the next page to be even
\def\forceleftpage{\clearpage\ifodd\c@page\hbox{}\thispagestyle{empty}\newpage\fi}

% --- TOC-spacing ------------------------------------------------------------

% add horizontal space between dotted line and page number
% in table of content and list of boxes:
% \newcommand\@pnumwidth{1.55em}     % this is LaTeX's original
\renewcommand\@pnumwidth{1.8em}

% reduce space between subsection number and subsection title in the TOC
%\newcommand*\l@subsection{\@dottedtocline{2}{3.8em}{3.2em}}   % this is LaTeX's original
\renewcommand{\l@subsection}{\@dottedtocline{2}{3.8em}{1.5em}}

% --- \tableofcontents -------------------------------------------------------

\renewcommand\tableofcontents{%
    \if@twocolumn
      \@restonecoltrue\onecolumn
    \else
      \@restonecolfalse
    \fi
    \chapter*{\contentsname
        \@mkboth{\scshape\lowercase{\contentsname}}{\scshape\lowercase{\contentsname}}}%
    \@starttoc{toc}%
    \if@restonecol\twocolumn\fi
    }

% --- list of boxes ----------------------------------------------------------

\newcommand{\makelistofboxes}{%
    \chapter*{List of boxes\@mkboth{\scshape list of boxes}{\scshape list of boxes}}
    \@starttoc{lob}}

\newcommand{\l@box}{\@dottedtocline{1}{0pt}{4em}}

% --- "theindex" environment -------------------------------------------------

\renewenvironment{theindex}{%
    \edef\indexname{Subject index}%
    \columnseprule \z@
    \columnsep 35\p@
    \refstepcounter{chapter}
    \addcontentsline{toc}{chapter}{\numberline {D}Subject index}
    \markboth{\scshape\lowercase{\chaptername~\thechapter}}{\scshape\lowercase{\indexname}}
    \twocolumn[\vspace*{50\p@}\@makechapterhead{\indexname}]%
    \thispagestyle{plain}%
    \parindent\z@
    \parskip\z@ \@plus .3\p@\relax
    \let\item\@idxitem
}{\onecolumn}

\makeatother

% ___________________________________________________________________________________
% PDF information

\ifx\pdfoutput\undefined\else
    \pdfinfo{
        /Author (Yvan Lengwiler)
        /Title (Macrofinance --- The Microfoundations of Asset Pricing Theory and the Equity Premium Puzzle)
%        /Subject (JEL classification: ...)
%        /Keywords (...)
        /Creator (MikTeX 2.2)
    }
\fi

% ___________________________________________________________________________________
% TEXT BEGINS HERE

\begin{document}

%    \let\cmt\hide               % turn off comments
    \let\citeindextrue\relax    % turn off \citeindex

    % COVER PAGE
    \include{title}

    % PREFACE
    \pagenumbering{roman}
    \citeindextrue
    % turn off TOC entry for local references
    \renewcommand{\bibsection}{\section*{References}}
    \include{preface}

    % allow chapters to open on left side
    \makeatletter
    \@openrightfalse
    \makeatother

    % TOC
    \tableofcontents
    \newpage

    % LIST OF BOXES
    % make sure "List of Boxes" starts on left side
    \forceleftpage
    \makelistofboxes
    \newpage

    % force chapters to open on right side
    \makeatletter
    \@openrighttrue
    \makeatother

    % put at least one empty page between TOC and beginning of first chapter
    \null\thispagestyle{empty}
    \cleardoublepage

    % CHAPTERS
    % turn on TOC entry for local references
    \renewcommand{\bibsection}{\section*{References}\addcontentsline{toc}{section}{References}}
    \pagenumbering{arabic}

    \include{book1}
    \include{book2}
    \include{book3}
    \include{book4}
    \include{book5}
    \include{book6}
    \include{book7}
    \include{book8}
    \include{book9}

    % APPENDICES
    % ... insert an empty page before starting appendices
    \newpage\thispagestyle{empty}\null
    % ... allow appendices to start on left pages
    \makeatletter
    \@openrightfalse
    \makeatother
    % ... start appendices
    \appendix
    \renewcommand{\chaptername}{Appendix}
    % ... add an empty page
    \newpage\thispagestyle{empty}\null
    % ... add possible another one to put the "Appendices" cover page on right-hand side
    \ifodd\thepage\else\newpage\thispagestyle{empty}\null\fi
    % ... cover page for appendices
    \vfil\vfil\begin{center}{\huge\itshape\bfseries Appendices}\end{center}\vfil\vfil\vfil
    \newpage

    % -- LIST OF SYMBOLS AND NOTATION
     \include{symbols}

    % -- SOLUTIONS TO END OF CHAPTER PROBLEMS
    \include{solutions}

    % -- BIBLIOGRAPHY
    \newpage
    \citeindexfalse
    \renewcommand{\bibsection}{\chapter{Bibliography}}
    %\def\bibintro{The numbers in square braces indicate the page numbers where the respecive works are cited.}
    %    \nocite{*}      % include all items in the database
    \bibliography{book}

%    % -- AUTHOR INDEX
%    \newpage\addcontentsline{toc}{chapter}{\numberline {D}Author index}
%    \printauthorindex

    % -- KEYWORD INDEX
    \newpage
    \printindex

\end{document}
