%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%	dbp-book.sty
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\dbpVERSION{5 July 2017}
%\typeout{^^JLOADING dbp-book.sty^^J}
\typeout{^^JLOADING dbp-book.sty, version: \dbpVERSION^^J}

\usepackage[fleqn,centertags]{amsmath}
	%\usepackage[fleqn]{amsmath,amssymb,amsfonts}
\usepackage{amssymb}
\usepackage{amsfonts}
	%\usepackage{eufrak}   % included in amsfonts ???
\usepackage{ntheorem}
\usepackage{graphicx}
	%\usepackage{times}
\usepackage{natbib}
	%\usepackage{chicago}
\usepackage{fancyhdr}
\usepackage{array}
\usepackage{boxedminipage}
\usepackage{verbatim}
%\expandafter\ifx\csname color\endcsname\relax% maybe color package already loaded
%\else
	\usepackage[x11names,svgnames]{xcolor}
%\fi
	%\usepackage{amsmath,amssymb,amsfonts}
%\usepackage{boxedminipage}
\usepackage{titlesec}



\def\version{\relax}	% repeated in pagestyle
\def\bookNAME{\relax}	% repeated in pagestyle
\def\isnotdefined{\relax}
\def\coursename{\relax}
\def\memoNAME{\relax}


%%%%%%% flags:
\newif\ifDRAFT
\DRAFTfalse

\newif\ifEDIT
\EDITfalse

\newif\ifFINAL
\FINALtrue

\newif\ifGLOBALS	% to switch on global section refs 
\GLOBALSfalse


%\newcommand{\incomplete}{\quad{\normalfont\small\scshape [incomplete]}}





%%%%%%	\input dbp-misc-macros.sty  %\typeout{JUST READ dbp-misc-macros.sty}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% miscellaneous macros, fonts
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\newcommand\xbar{\overline x}
\makeatletter

%\def\@setref#1#2#3{%
\def\DBP@setref#1#2#3{% 13 July 2017
  \ifx#1\relax
   \protect\G@refundefinedtrue
   \nfss@text{\reset@font\bfseries #3}%
   \@latex@warning{Reference `#3' on page \thepage \space
             undefined}%
  \else
   \expandafter#2#1\null
  \fi}

\makeatother

\usepackage{bbm}

\makeatletter

\newif\ifshowlabels	% added 11 sep 2012
\def\showlabels{%
	\showlabelstrue	% added 11 sep 2012
	\usepackage[notcite,notref,color]{showkeys}%
	\definecolor{labelkey}{cmyk}{0.25,0.2,1,0}%
	\renewcommand\showkeyslabelformat[1]{%
	\fbox{\normalfont\footnotesize\ttfamily##1}%
	}%

	\hoffset.8in
	\reversemarginpar%	6 jan 2013

	\setlength{\marginparwidth}{1.5in}%
	\setlength{\marginparsep}{.8in}%
}

%\newcommand{\cid}{\rightsquigarrow}
\def\cid{\rightsquigarrow}
%\newcommand{\cidfidi}{\underset{fidi}{\rightsquigarrow}}
%\newcommand{\cidfidi}{\rightsquigarrow_{fidi}}
\def\cidfidi{\rightsquigarrow_{fidi}}

%\def\Framenote#1{\bigskip\framepara\hsize{#1}\bigskip}

\def\Norm#1{\left\Vert#1\right\Vert}
\def\bignorm#1{\left\Vert#1\right\Vert}
\def\norm#1{\left\lVert#1\right\rVert}
%\newcommand{\normT}[1]{\bigl\|#1\bigr\|_T}
\def\psinorm#1{\norm{#1}_\psi}
\def\Psinorm#1{\Norm{#1}_\Psi}
\def\normTV#1{\norm{#1}_{\rm TV}}
\def\normBL#1{\norm{#1}_{\rm BL}}
\def\normLip#1{\norm{#1}_{\rm Lip}}

\def\FrameNote#1{\begin{boxedminipage}{4in}#1\end{boxedminipage}}

\let\citeNP=\citealt
\let\citeN=\citet
%%%\let\Label=\label

%from:  http://en.wikibooks.org/wiki/LaTeX/Mathematics#Roots
% New definition of square root:
% it renames \  as \oldsqrt
\let\oldsqrt\sqrt
% it defines the new \sqrt in terms of the old one
\def\Sqrt{\mathpalette\DHLhksqrt}
\def\DHLhksqrt#1#2{%
\setbox0=\hbox{$#1\oldsqrt{#2\,}$}\dimen0=\ht0
\advance\dimen0-0.2\ht0
\setbox2=\hbox{\vrule height\ht0 depth -\dimen0}%
{\box0\lower0.4pt\box2}}

%\newcommand{\DSqrt}[1]{\displaystyle\Sqrt{#1}}
\def\DSqrt#1{\displaystyle\Sqrt{#1}}
%\def\half{1/2}
\def\Pover#1#2{\left(\frac{#1}{#2}\right)}

% Slashed fraction: see Knuth Exercise 11.6
\def\Sfrac#1#2{\kern.1em\raise.5ex\hbox{\the\scriptfont0 #1}\unskip
\kern-.1em/\kern-.15em\lower.25ex\hbox{\the\scriptfont0 #2}}

\def\trp{{\scriptscriptstyle\intercal}}	% transpose symbol

\def\half{\Sfrac12}
\def\third{\Sfrac13}
\def\quarter{\Sfrac14}
\def\fifth{\Sfrac15}

\newcommand{\bone}{\mathbbm1}

\def\dbyd#1#2{\frac{\partial#1}{\partial #2}}

\def\text#1{\hbox{\rm#1}}
\def\qt#1{\qquad\text{#1}}
\def\argmin{\mathop{\rm argmin}\limits}
\def\argmin{\mathop{\rm argmin}}
\def\argmax{\mathop{\rm argmax}}

\def\NL{\nolimits}
\def\MAX{\max\nolimits}
\def\ARGMAX{\argmax\nolimits}
\def\MIN{\min\nolimits}
\def\ARGMIN{\argmin\nolimits}
\def\SUP{\sup\nolimits}
\def\CUP{\cup\nolimits}
\def\INF{\inf\nolimits}
\def\LIM{\lim\nolimits}
\def\LIMSUP{\limsup\nolimits}
\def\LIMINF{\liminf\nolimits}
\def\SUM{\sum\nolimits}
\def\PROD{\prod\nolimits}
\def\BIGCUP{\bigcup\nolimits}
\def\BIGCAP{\bigcap\nolimits}

\def\siln{\sum\NL_{i\le n}}
\def\sumnl{\sum\nolimits}
\def\prodnl{\prod\nolimits}
\def\maxnl{\max\nolimits}
\def\minnl{\min\nolimits}

\def\and/{\qquad\text{\scshape and}\qquad}
\def\AND/{\quad\text{\scshape and}\quad}
\def\cov{\text{cov}}
\def\diag{\text{diag}}
\newcommand{\diam}{\text{diam}}
\newcommand{\rad}{\text{rad}}
\def\osc{\text{\scshape osc}}
\def\ucts{\text{\scshape U\kern-1.5pt C}}
\def\ucts{\text{\scshape u\kern-1.5pt c}}
\newcommand{\co}{\text{co}}	% convex hull
\def\inner#1#2{\langle#1,#2\rangle}
\def\Rad{\mathfrak{s}}  % Rademacher
\def\Radspace{\{-1,+1\}} 

\def\bitem{\item[\textbullet]}

\newcommand{\irootn}{{1/\sqrt{n}}}
\newcommand{\rootn}{{\sqrt{n}}}


\def\trace{\text{trace}}
\def\var{\text{var}}
\def\vol{\text{vol}}
\newcommand{\hamming}{\text{hamm}}
\newcommand{\hamm}{{\mathfrak h}}
\newcommand{\Hamm}{{\mathfrak H}}
\def\bsl{\backslash}
\def\wrt/{with respect to}
\def\Hrule{\bigskip\hrule\bigskip}
\newcommand\Cdot{\,\cdot\,}

\def\Unif{\mathrm{Unif}}
\def\Wolog/{Without loss of generality}
\def\wolog/{without loss of generality} % changed to avoid clash with Latex and BoxedEPS.tex, 
	%BoxedEPS.tex, line 67:  \let\wlog@ld\wlog 
	%BoxedEPS.tex, line 68:  \def\wlog#1{\relax} 
	%BoxedEPS.tex, line 772:  \let\wlog\wlog@ld %%restore logging 

\def\LIST{\showboxdepth=40\showboxbreadth=45\showlists}

\def\today{\number\day \space \ifcase\month\or
January\or February\or March\or April\or May\or
June\or July\or August\or September\or October\or
November\or December\fi  \space \number\year}

\def\newdef#1{{\itshape\bfseries #1}}

\def\Ber{\textrm{Ber}}
\def\Binom{\text{Bin}}
\def\Poisson{\text{Poisson}}
\def\barPhi{\bar\Phi}   % normal tails
\newcommand{\Psii}{\Psi^{-1}}
\newcommand{\Psitwonorm}[1]{\norm{#1}_{\Psi_2}}

%\def\mnote#1{\marginpar{#1}}
\def\mnote#1{\marginpar{\par\tiny#1}}

\def\bullitem{\item[$\bullet$]}
\def\qed{\ifmmode\square\else$\square$\fi}
\def\QED{\ifmmode\hfill{\square}\else\hfill{$\square$}\fi}

\def\framemath#1{\setbox0=\hbox{$\displaystyle#1$}\fbox{\box0}}

\let\frameword=\fbox

\def\Heading{\begin{center}\scshape\large}
\def\endHeading{\end{center}}

\def\subheading#1{\par\noindent{\bf #1}\newline}
\def\subheading#1{\par\bigskip\noindent{\bf #1}\newline}
\def\outdent#1{\noindent{\llap{#1\quad}}} 

\def\outdentsubheading#1{\par\bigskip\noindent{\kern-2em\bf #1}\newline}
\def\SCsubheading#1{\par\bigskip\noindent{\scshape #1}\newline}
%\renewcommand{\subheading}{\@startsection
%						 {subheading}%     the name
%						 {10}%	the level
%						 {\z@}%	the indent
%                         {-\baselineskip}%	the beforeskip
%                         {0.5\baselineskip}%	the afterskip
%                         {\normalfont\normalsize\bfseries}}%		the style
%

\def\subsubheading#1{\par\noindent{\bf #1}}

\def\bubble#1{\raise8pt\hbox to 0pt{$\mkern6mu\scriptscriptstyle\circ$\hss}#1}
\def\bdot{\bullet}

\def\UGMTP#1{{\small\sc UGMTP~\S#1}}
\def\ugmtp#1{{\small\sc UGMTP~#1}}

\chardef\slashedO="1F
\chardef\slashedo="1C
\chardef\eszet="19

%%%%%%%% blackboard bold
\def\AA{{\mathbb A}}\def\BB{{\mathbb B}}\def\CC{{\mathbb C}}
\def\DD{{\mathbb D}}\def\EE{{\mathbb E}}\def\FF{{\mathbb F}}
\def\GG{{\mathbb G}}\def\HH{{\mathbb H}}\def\II{{\mathbb I}}
\def\JJ{{\mathbb J}}\def\KK{{\mathbb K}}\def\LL{{\mathbb L}}
\def\MM{{\mathbb M}}\def\NN{{\mathbb N}}\def\OO{{\mathbb O}}
\def\PP{{\mathbb P}}\def\QQ{{\mathbb Q}}\def\RR{{\mathbb R}}
\def\SSS{{\mathbb S}}\def\TT{{\mathbb T}}\def\UU{{\mathbb U}}
\def\VV{{\mathbb V}}\def\WW{{\mathbb W}}\def\XX{{\mathbb X}}
\def\YY{{\mathbb Y}}\def\ZZ{{\mathbb Z}}


% Note: hyperref redefines \ss and \ss

\def\NNbar{\overline\NN}
\def\RRbar{\overline\RR}
\def\PPbar{\overline\PP}
\def\barR{{\overline\RR}}
\def\barT{\overline T}
\def\xbar{\overline x}
\newcommand{\pbar}{\overline p}
\newcommand{\wbar}{\overline w}
\newcommand{\Zbar}{\overline Z}
\newcommand{\Lambar}{\overline \Lambda}
\newcommand{\ttheta}{\widetilde \theta}

\newcommand\tf{\widetilde f}
\newcommand\tF{\widetilde F}
%\newcommand\xbar{\overline x}
%\newcommand{\wbar}{\overline w}
%\newcommand{\ttheta}{\widetilde \theta}


%%%%%%%% caligraphic
%\usepackage{eufrak}
\usepackage{euscript}
\let\Cal=\EuScript
\let\muchmore=\gg
\let\muchless=\ll
\let\typewriter=\tt  % for turning on the font
\let\eszett=\ss


\def\aa{{\Cal A}}\def\bb{{\Cal B}}\def\cc{{\Cal C}}\def\dd{{\Cal D}}
\def\ee{{\Cal E}}\def\ff{{\Cal F}}\def\gg{{\Cal G}}\def\hh{{\Cal H}}
\def\ii{{\Cal I}}\def\jj{{\Cal J}}\def\kk{{\Cal K}}\def\ll{{\Cal L}}
\def\mm{{\Cal M}}\def\nn{{\Cal N}}\def\oo{{\Cal O}}\def\pp{{\Cal P}}
\def\qq{{\Cal Q}}\def\rr{{\Cal R}}\def\sss{{\Cal S}}\def\tt{{\Cal T}}
\def\uu{{\Cal U}}\def\vv{{\Cal V}}\def\ww{{\Cal W}}\def\xx{{\Cal X}}
\def\yy{{\Cal Y}}\def\zz{{\Cal Z}}

% note: \ss is redefined by hyperref to the eszet char
%%%%%%%% Greek
\def\a{\alpha}
%\def\b{\beta}  % clashes with hyperref
\def\gam{\gamma}	\def\Gam{\Gamma}
\def\del{\delta}	\def\Del{\Delta}
\def\eps{\epsilon}
\def\k{\kappa}
\def\lam{\lambda}	\def\Lam{\Lambda} \def\Lambdab{\mathbf\Lambda}
\def\om{\omega}		\def\Om{\Omega}
\def\sig{\sigma}
%\def\th{\theta}		% clashes with hyperref
\def\Th{\Theta}

\def\alphab{\boldsymbol\alpha}
\def\betab{\boldsymbol{\beta}}
\def\thetab{\boldsymbol\theta}
\def\lambdab{\boldsymbol\lambda}
\def\mub{\boldsymbol\mu}
\def\pib{\boldsymbol\pi}
\def\sigb{\boldsymbol\sigma}
\def\taub{\boldsymbol\tau}
\def\deltab{\boldsymbol\delta}
\def\Deltab{\boldsymbol\Delta}
\newcommand{\grad}{\nabla}
%%%%	abbreviations

\def\Matrix{\begin{matrix}}
\def\endMatrix{\end{matrix}}

\def\bMatrix{\begin{bmatrix}}
\def\endbMatrix{\end{bmatrix}}

\def\pMatrix{\begin{pmatrix}}
\def\endpMatrix{\end{pmatrix}}


\newcommand{\beq}{\begin{equation}}  % expects a label
\newcommand{\eeq}{\end{equation}}

\newcommand{\beqN}{\begin{equation*}}  %no label, same as $$ .... $$ except for alignment
\newcommand{\eeqN}{\end{equation*}}



\newcommand{\bea}{\begin{eqnarray*}}
\newcommand{\eea}{\end{eqnarray*}}
\newcommand{\bean}{\begin{eqnarray}}
\newcommand{\eean}{\end{eqnarray}}
\newcommand{\bear}{\begin{array}}
\newcommand{\eear}{\end{array}}

%Note:  With \Showlabels the labels must be \Labels
%\def\bAlignL#1\eAlignL{\begin{align}#1\end{align}}  % Labels
\def\bAlign#1\eAlign{\begin{align*}#1\end{align*}}	% no labels

%\def\bAlignL#1\eAlignL{%
%	\let\tagform@=\old@tagform@
%	\begin{align}#1\end{align}%
%	\let\tagform@=\new@tagform@
%	\showmarginlabels    %does nothing if no Showlabels	
%}% end bAlignL

\def\bAlignL#1\eAlignL{\begin{align}#1\end{align}}
%\def\eAlignL{\end{align}}

\def\bxAlignat#1#2\exAlignat{%
	\let\tagform@=\old@tagform@
	\begin{xalignat}{#1}#2\end{xalignat}%
	\let\tagform@=\new@tagform@
	\showmarginlabels   %does nothing if no Showlabels		
}%

\def\NT{\notag\\}  


\newcommand{\STARS}{\begin{center}***\end{center}}
\def\cases@#1{\left\{\,\vcenter{\normalbaselines\m@th
    \ialign{$##$\hfil&\quad##\hfil\crcr#1\crcr}}\right.}

\def\Cases#1\endCases{\let\\=\cr\cases@{#1}}



\def\sc#1{{\scshape#1}}
\renewcommand{\theenumi}{(\roman{enumi})}
\renewcommand{\labelenumi}{(\roman{enumi})}

\let\majm=\mu
\def\hangbdot{\noindent\llap{$\bdot$\quad}}



% 20 April 2016
\newenvironment{little}{\bigskip\bgroup\tiny}{\egroup\bigskip}
%\newcommand{\xlogx}{\psi_{\rm \tiny ent}}

%\def\framemath#1{\fboxsep=0pt\fbox{\vbox{#1}}}
%\def\framemath#1{\vskip2pt\fboxsep=0pt\fbox{\vbox{#1}}}

\newcounter{task}

\newenvironment{task}%
%{\refstepcounter{task}\par\medskip\noindent%
{\refstepcounter{equation}\par\medskip\noindent%
%\hrule\vskip1pt\hrule\medskip
%\Hrule
%	\begin{boxedminipage}{\textwidth}%

%	{\noindent\bfseries Task \thetask : }%
%	{\noindent\bfseries Task \theequation : }%
	{\noindent\llap{\hbox to5.5pt{\hss$<$\theequation$>$\shim{20}}}\bfseries Task:}%
}
%{\end{boxedminipage}\medskip}%\egroup}
%{\medskip\hrule height 2pt\smallskip}%\egroup}
{\vskip-\lastskip\makeQED\smallskip}%\egroup}



%\newcommand{\link}{\ell}% might be changed

\makeatother

%%%%%%	End of dbp-misc-macros.sty


%%%%%%	\input dbp-pagedims.sty
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% General page dimensions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newdimen\marginindent
\newdimen\marginpoke
\marginindent=3pc
\marginpoke=\marginindent

%\newdimen\widehsize
%\newdimen\headwidth  % clashes with fancyhdr
%\newdimen\headframewidth


\abovedisplayskip=5pt plus1pt minus1pt
\belowdisplayskip=5pt plus1pt minus1pt

\newskip\proclaimskipamount
\newskip\ritemskipamount

\proclaimskipamount=6pt  plus1pt minus1pt %12pt  plus1pt minus1pt
\ritemskipamount=2pt %plus1pt minus1pt

\def\proclaimbreak{\par \ifdim\lastskip<\proclaimskipamount
		   \removelastskip \penalty-200\vskip\proclaimskipamount\fi}

\def\marginflush#1{\vbox{\moveleft\marginindent\hbox{#1}}}

%\rightskip=0ptplus1em minus1pt
\newskip\AboveChapterHead
\AboveChapterHead=1in

%%%%%%	End of dbp-pagedims.sty

	%\input dbp-toc.sty
	%\input dbp-parts.sty

%%%%%%	\input dbp-globals.sty	% contains tokens and ifDRAFT etc
% dbp-globals.sty

\makeatletter

\newtoks\tok@Chap	%prefix for global references
\tok@Chap={} % added 10 aug 2014 
\newtoks\tok@Sect
\tok@Sect={S:}
\newtoks\tok@Prob
\tok@Prob={P:}
\def\E@{}% to keep equation labels shorter

%\newtoks\tok@App	\tok@App={} % added 1 July 2015

\global\let\oldnewlabel\newlabel% in case hyperref not loaded
%\let\LABEL=\label	% default
%\let\REF=\ref

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% equation and theorem numbers,
% moved from eqn-num.sty, 27 Sept 2017
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\makeatletter

\def\maketag@@@#1{\llap{\m@th\normalfont#1\qquad}}

\def\tagform@#1{%
	\maketag@@@{\Leqn\ignorespaces#1\unskip\@@italiccorr\Reqn}%
}
%\makeatother

\def\Leqn{\hbox{$<$}}
\def\Reqn{\hbox{$>$}}

\def\cref#1{{\normalsize%
	\Leqn\hbox{\normalfont\small\REF{#1}}\normalsize\Reqn}}
\def\CREF#1#2{\normalsize%
	\Leqn\hbox{\normalfont\small\ref{#1::#2}}\normalsize\Reqn}

\def\LABEL#1{\label{#1}}
\def\REF#1{\hbox{\ref{#1}}}

\def\makeGLOBALS{%  create file for global section crossrefs
	\GLOBALStrue%
	\expandafter\newwrite\csname tf@gbl\endcsname%
	\immediate\openout \csname tf@gbl\endcsname \jobname.gbl\relax%
}
%%\def\getGLOBALS{\relax}% reset in MiniEmpirical.tex
\def\getGLOBALS{\relax}% reset in mini-latex.sty

%\let\LABEL\label
%\tok@Chap={fred}
%\def\LABEL#1{\label{\the\tok@Chap#1}}

\def\LOCALlabels{%
	\def\E@{\the\tok@Chap}
	% the next two lines need to follow reading of aux file
	\def\LABEL##1{\label{\E@##1}}
%	\def\REF##1{\ref{\E@##1}}
	\def\REF##1{\hbox{\ref{\E@##1}}}
%	\let\@@@newlabel\newlabel
%	\def\newlabel##1##2{\@@@newlabel{##1}{\the\tok@Chap##2}}%
%	\let\OLDLABEL\label
% 	\edef\LABEL##1{\label{\the\tok@Chap##1}}
%	\gdef\label##1{\OLDLABEL{\the\tok@Chap##1}}
}


\makeatother

%	The following macros assume there is a root directory containing a file
%	BOOK.tex together with sudirectories SUBDIR1, SUBDIR2, ... , one for
%	each chapter.
%
%	SUBDIRj should contain SUBDIRj.tex and chapter.tex. The only job of
%	SUBDIRj.tex is to allow separate processing of chapters of the book; its
%	main purpose is to run \makechapter and created a .bbl file.
%
%	To process the whole book run LaTeX on BOOK.tex, which includes each
%	chapter using \bookINCLUDE.
%
%	For example, the directory MiniEmpirical contains 
%	MiniEmpirical.tex = BOOK.tex 
%	WhatWhy = SUBDIR1
%	containing WhatWhy.tex and chapter.tex
%	Basic = SUBDIR2
%	containing Basic.tex and chapter.tex
%	...
%

\def\path{.}	% gets changed by each Chapter 
\def\INPUT#1{\input \path/#1}
\def\bookINCLUDE#1{% process a chapter = SUBDIR
	\def\path{#1}%
	\input \path/chapter.tex
	\input \path/#1.bbl	% pick up reference list created by processing SUBDIR.tex 
}

\newcommand{\makechapter}{%
\DRAFTtrue	% used by \Chapter to turn on draft pagestyle
\getGLOBALS	% defined in dbp-globals.sty
\showlabels	% defined in dbp-misc-macros.sty
\LOCALlabels
\useHYPERREF
\begin{document}%

\TOC
\setcounter{page}{0}%
\ifDRAFT	% seems automatic
	\vskip1in
	Printed: \today
	\thispagestyle{startchap}%
\fi
\input chapter.tex
\end{document}%
}

%\newif\ifDRAFT
%\DRAFTfalse
%
%\newif\ifEDIT
%\EDITfalse
%
%\newif\ifFINAL
%\FINALtrue


%%%%%%	End of dbp-globals.sty


%%%%%%	\input dbp-pagestyle.sty %\usepackage{fancyhdr}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Page styles
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{fancyhdr}

\makeatletter

\newif\ifnormal
\normaltrue

%\def\version{\relax}
%\def\bookNAME{\relax}
%\def\coursename{\relax}
%\def\undefined{\relax}

\fancyhf{}
%\usepackage{fancyhdr}
\def\sectionmark#1{%
  \markboth {{%
	\ifnum \c@secnumdepth >\z@
	  \thesection\quad
	\fi
	#1}}{}}%
	
\fancyhead{}
%\fancyhead[LO]{\S\ChapterNumber.\normalsize\scshape\leftmark}
%\fancyhead[LO]{\S\ChapterNumber.\normalsize\scshape\botmark}
%%%%%\fancyhead[LO]{\ifnormal\S\ChapterNumber.\thesection\fi\normalsize\scshape\SectionName}
%\fancyhead[LO]{\llap{\S\ChapterNumber\qquad}.\leftmark}
\fancyhead[RO]{\thepage}
%\fancyhead[RE]{\small Chapter \ChapterNumber: \ChapterName}
%%%%%%%\fancyhead[RE]{\normalsize\scshape \ChapterName}
\fancyhead[LE]{\thepage}
\fancyfoot{}
\addtolength{\headheight}{\baselineskip}

%% normal
\fancypagestyle{normal}{%
	\normaltrue
	\fancyhf{} % clear all header and footer fields
	\fancyhead[LO]{%
%		\ifnormal\S\ChapterNumber.\thesection\quad\fi
		\ifnormal\S\thesection\quad\fi
		\normalsize\scshape\SectionName
	}%
	\fancyhead[RO]{\thepage}
	\fancyhead[LE]{\thepage}
	\fancyhead[RE]{\scshape \normalsize\ChapterName}
	\fancyfoot{}
	\renewcommand{\headrulewidth}{0.5pt}
	\renewcommand{\footrulewidth}{0pt}%
}


%% memo
\fancypagestyle{memo}{%
%	\normaltrue
	\fancyhf{}% clear all header and footer fields
%	\fancyhead[LO]{%
%		\memoNAME
%%		\ifnormal\thesection\quad\fi
%%		\normalsize\scshape\SectionName
%	}%
%	\fancyhead[RO]{\thepage}
	\renewcommand{\headrulewidth}{0.5pt}%
	\lhead{\nouppercase{\rightmark}}%
	\rhead{\thepage}%

%	\fancyhead[LE]{\thepage}
	%\fancyhead[RE]{\scshape \normalsize\ChapterName}
	%\fancyfoot[L]{\vtop{\hbox{\scshape David Pollard}\hbox{\today}}}
	% changed 26 March 2016
%   \fancyfoot[L]{{\setlength{\fboxsep}{1.5mm}	
%	   \fbox{\itshape Draft: \version\ \copyright David Pollard}%
%   }}	
%	\fancyfoot[R]{\thepage}
	\setlength\footskip{70pt}%
	\fancyfoot[C]{\fbox{\hbox to\textwidth{%
	\itshape \memoNAME 
	\hss $\bowtie$\hss
	Draft: \version\ \copyright David Pollard}}}%

%	\renewcommand{\footrulewidth}{0.5pt}%
}

%	\newcommand{\makememo}{% 15 July 2015, 30 June 2017
%	   \pagestyle{memo}%
%	   \renewcommand\sectionmark[1]{\markright{\S\thesection\ ##1}}%
%	%	\renewcommand\subsectionmark[1]{\markright{\S\thesubsection\ ##1}}%
%
%	   \let\bookNAME = \memoNAME
%	   \thispagestyle{startchap}% 
%	   \renewcommand{\thesection}{\@arabic\c@section}%
%	}

\newcommand{\makememo}[1]{% 15 July 2015, 30 June 2017
	\def\memoNAME{#1}%
	\pagestyle{memo}%
	\renewcommand\sectionmark[1]{\markright{\S\thesection\ ##1}}%
%	\renewcommand\subsectionmark[1]{\markright{\S\thesubsection\ ##1}}%

	\let\bookNAME = \memoNAME
	\thispagestyle{startchap}% 
	\renewcommand{\thesection}{\@arabic\c@section}%
}


%% oldmemo
\fancypagestyle{oldmemo}{%
	\normaltrue
	\fancyhf{} % clear all header and footer fields
	\fancyhead[LO]{%
		\coursename
%		\ifnormal\thesection\quad\fi
%		\normalsize\scshape\SectionName
	}%
%	\fancyhead[RO]{\thepage}
%	\fancyhead[LE]{\thepage}
	%\fancyhead[RE]{\scshape \normalsize\ChapterName}
	%\fancyfoot[L]{\vtop{\hbox{\scshape David Pollard}\hbox{\today}}}
	% changed 26 March 2016
	\fancyfoot[L]{{\setlength{\fboxsep}{1.5mm}	
		\fbox{\itshape Draft: \version\ \copyright David Pollard}%
	}}	
	\fancyfoot[R]{\thepage}
	\renewcommand{\headrulewidth}{0pt}
%	\renewcommand{\footrulewidth}{0.5pt}%
	\renewcommand{\footrulewidth}{0pt}%
}



%% olddraft
\fancypagestyle{olddraft}{%
	\normaltrue
	\fancyhf{} % clear all header and footer fields
	\fancyhead[LO]{%
%		\ifnormal\S\ChapterNumber.\thesection\quad\fi
		\ifnormal\S\thesection\quad\fi
		\normalsize\scshape\SectionName
	}%
	\fancyhead[RO]{\thepage}
	\fancyhead[LE]{\thepage}
%	\fancyhead[RE]{\scshape \normalsize\ChapterName}
	\fancyfoot[L]{{\setlength{\fboxsep}{1.5mm}	
		\fbox{\itshape Draft: \version\ \copyright David Pollard}%
	}}
	\renewcommand{\headrulewidth}{0.5pt}
	\renewcommand{\footrulewidth}{0pt}%
}
%% draft
\fancypagestyle{draft}{%
	\normaltrue
	\setlength\footskip{70pt}%
	\fancyhf{} % clear all header and footer fields
	\lhead{\nouppercase{\rightmark}}
	\rhead{\thepage} 
%	\rhead{\ChapterName}

%  \fancyhead[LO]{%
%%		\ifnormal\S\ChapterNumber.\thesection\quad\fi
%	  \ifnormal\S\thesection\quad\fi
%	  \normalsize\scshape\SectionName
%  }%
%	\fancyhead[RO]{\thepage}
%	\fancyhead[LE]{\thepage}
%	\fancyhead[RE]{\scshape \normalsize\ChapterName}
%   \fancyfoot[R]{{\setlength{\fboxsep}{1.5mm}	
%	   \fbox{\itshape Draft: \version\ \copyright David Pollard}%
%   }}
%   \fancyfoot[L]{\fbox{\itshape Chap\ \thechapter. \ChapterName}}%
	\fancyfoot[C]{\fbox{\hbox to\textwidth{%
	\itshape Chap\ \thechapter. \ChapterName 
	\hss $\bowtie$\hss
	Draft: \version}}}
%	\copyright David Pollard}}}
	\renewcommand{\headrulewidth}{0.5pt}%
	\renewcommand{\footrulewidth}{0pt}%
}

%% handout
\fancypagestyle{handout}{%
	\normaltrue
	\fancyhf{} % clear all header and footer fields
	\fancyhead[LO]{%
		\coursename
%		\ifnormal\thesection\quad\fi
%		\normalsize\scshape\SectionName
	}%
	\fancyhead[RO]{\thepage}
	\fancyhead[LE]{\thepage}
	%\fancyhead[RE]{\scshape \normalsize\ChapterName}
	\fancyfoot{}
	\renewcommand{\headrulewidth}{0.5pt}
	\renewcommand{\footrulewidth}{0pt}%
}



%% YPNG
\def\YPNGdate{YPNG \today}% default
\fancypagestyle{YPNG}{%
	\normaltrue
	\fancyhf{} % clear all header and footer fields
	\fancyhead[L]{%
		\YPNGdate
%		\ifnormal\thesection\quad\fi
%		\normalsize\scshape\SectionName
	}%
%	\fancyhead[RO]{\thepage}
%	\fancyhead[LE]{\thepage}
	%\fancyhead[RE]{\scshape \normalsize\ChapterName}
	%\fancyfoot[L]{\vtop{\hbox{\scshape David Pollard}\hbox{\today}}}
	% changed 26 March 2016
%  \fancyfoot[L]{{\setlength{\fboxsep}{1.5mm}	
%	  \fbox{\itshape Draft: \version\ \copyright David Pollard}%
%  }}	\fancyfoot[R]{\thepage}
	\renewcommand{\headrulewidth}{0pt}
%	\renewcommand{\footrulewidth}{0.5pt}%
	\renewcommand{\footrulewidth}{0pt}%
}

\newcommand{\makeYPNG}{% 8 June 2016
	\pagestyle{YPNG}%
%	\thispagestyle{startchap}% 
	\renewcommand{\thesection}{\@arabic\c@section}%
}

%% blank
\fancypagestyle{blank}{%
	\normaltrue
	\fancyhf{} % clear all header and footer fields
%	\fancyhead[LO]{%
%		\coursename
%		\ifnormal\thesection\quad\fi
%		\normalsize\scshape\SectionName
%	}%
%	\fancyhead[RO]{\thepage}
%	\fancyhead[LE]{\thepage}
	%\fancyhead[RE]{\scshape \normalsize\ChapterName}
%	\fancyfoot{}
	\renewcommand{\headrulewidth}{0pt}%
	\renewcommand{\footrulewidth}{0pt}%
}


\pagestyle{normal}

%\def\bookNAME{\relax} % causes trouble with empty line
\def\bookNAME{{\scshape unknown bookNAME}}
\def\SectionName{\relax}
\def\AUTHOR{David Pollard}

\fancypagestyle{startchap}{%
	\fancyhf{} % clear all header and footer fields
	\fancyfoot[L]{%
		\vtop to0pt{%
			\small\itshape 
			version: \version\\
			printed: \today%
			\vss
		}}
	%\fancyfoot[C]{\thepage}
	\fancyfoot[R]{%
		\vtop to0pt{%
			\small\itshape 
			\bookNAME\\%
			\copyright \AUTHOR%
			\vss 
		}}%
	\renewcommand{\headrulewidth}{0pt}
	\renewcommand{\footrulewidth}{0.5pt}%
}

%\newcommand{\ps@references}{}
\fancypagestyle{references}{%
	\fancyhf{}
	\fancyhead[LO]{\scshape
	\ChapTitle/ \ChapterNumber\ references}
	%\fancyhead[LO]{\llap{\S\ChapterNumber\qquad}.\leftmark}
	\fancyhead[RO]{\thepage}%
	\fancyhead[RE]{\scshape \ChapterName}%
	\fancyhead[LE]{\thepage}%
}

%% problems
\fancypagestyle{problems}{%
	\fancyhf{}
	\fancyhead[LO]{\scshape
	\ChapTitle/ \ChapterNumber\ problems}
	%\fancyhead[LO]{\llap{\S\ChapterNumber\qquad}.\leftmark}
	\fancyhead[RO]{\thepage}%
	\fancyhead[RE]{\scshape \ChapterName}%
	\fancyhead[LE]{\thepage}%
}

\fancypagestyle{notes}{%
	\fancyhf{}
	\fancyhead[LO]{\scshape\normalsize Chapter \ChapterNumber\ notes}
	%\fancyhead[LO]{\llap{\S\ChapterNumber\qquad}.\leftmark}
	\fancyhead[RO]{\thepage}%
	\fancyhead[RE]{\scshape \ChapterName}%
	\fancyhead[LE]{\thepage}%
}

\fancypagestyle{roman}{%
	\fancyhf{}
	\fancyhead[RO]{\roman{page}}
	\fancyhead[LE]{\roman{page}}
	\renewcommand{\headrulewidth}{0pt}
	\renewcommand{\footrulewidth}{0pt}% added 5 July 2017
}

\fancypagestyle{HWnumbered}{%
	\fancyhf{}
	\fancyhead[RO]{\thepage}
	\fancyhead[LE]{\thepage}
	\renewcommand{\headrulewidth}{0pt}
}

\fancypagestyle{preface}{%
%	\normaltrue
	\fancyhf{} % clear all header and footer fields
%	\fancyhead[LO]{%
%%		\ifnormal\S\ChapterNumber.\thesection\quad\fi
%		\ifnormal\S\thesection\quad\fi
%		\normalsize\scshape\SectionName
%	}%
	\fancyhead[RO]{\thepage}
	\fancyhead[LE]{\thepage}
%	\fancyhead[RE]{\scshape \normalsize\ChapterName}
	\fancyfoot{}
	\renewcommand{\headrulewidth}{0pt}
	\renewcommand{\footrulewidth}{0pt}%
}


\makeatother

%%%%%%	End of dbp-pagestyle.sty


%%%%%%	\input dbp-chapter.sty	%\usepackage{titlesec}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Chapter and Appendix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{titlesec}%	31 July 2017

\titleformat{\chapter}[display]
{\bfseries\Huge}
%{\filright{\large\bfseries\textsc{\chaptertitlename~\Large\thechapter}}}
%{\filright{\large\textsc{Chapter}~\Large\thechapter}}
{\filright{\large\textsc{\chaptertitlename}~\Large\thechapter}}
{0.5ex}
{%\titlerule
%\vspace{2ex}%
\filright}
[%\vspace{2ex}%
%\titlerule
]

\titleformat{\appendix}[display]
{\bfseries\Huge}
%{\filright{\large\bfseries\textsc{\chaptertitlename~\Large\thechapter}}}
%{\filright{\large\textsc{Chapter}~\Large\thechapter}}
{\filright{\large\textsc{\chaptertitlename}~\Large\theAppendix}}
{0.5ex}
{%\titlerule
%\vspace{2ex}%
\filright}
[%\vspace{2ex}%
%\titlerule
]

\titleformat{\Appendix}[display]
{\bfseries\Huge}
%{\filright{\large\bfseries\textsc{\chaptertitlename~\Large\thechapter}}}
%{\filright{\large\textsc{Chapter}~\Large\thechapter}}
{\filright{\large\textsc{\chaptertitlename}~\Large\Alph{Appendix}}}
{0.5ex}
{%\titlerule
%\vspace{2ex}%
\filright}
[%\vspace{2ex}%
%\titlerule
]

\makeatletter

\def\ChapTitle/{}
\def\ChapAbbrev/{}
\def\ChapterNumber{0}
\def\ChapterName{}

%\newcount\count@Chapter
%\newcount\count@Appendix
\newcounter{Chapter}
\newcounter{Appendix}
%%\setcounter{Appendix}{20}% so that App and Chap numbers don't overlap
%\count@Chapter= 0  
%\count@Appendix=`\A   %   Capital A
%\count@Appendix= 0   %   Capital A

\def\GiveChapNumber#1{%
	\stepcounter{Chapter}%
	%\expandafter\xdef\csname Chap#1\endcsname{\the\count@Chapter}%
	\expandafter\xdef\csname Chap#1\endcsname{\arabic{Chapter}}%
	%\advance\count@Chapter by1}
}

\gdef\GiveAppLetter#1{%
	\stepcounter{Appendix}%
	%\expandafter\xdef\csname App#1\endcsname{\char\the\count@Appendix}%
	\expandafter\xdef\csname refApp#1\endcsname{\Alph{Appendix}}% 1 Oct 2012, to allow ref by letter
	\expandafter\xdef\csname App#1\endcsname{\theAppendix}%   changed 21 Sept 2017
	%\advance\count@Appendix by1}
}

%\def\Chapter#1#2{%
%	\gdef\ChapTitle/{Chapter}%
%	\gdef\ChapAbbrev/{Chap}%
%	\gdef\runninghead{#2}%   % added15 Aug 2011 for 241
%	\@Chapter{#1}{#2}%
%}


%\def\Chapter#1#2{%  EXPERIMENT 20 April 2014
%	\ifDRAFT
%		\pagestyle{draft}\else\pagestyle{normal}%
%	\fi	
%	\thispagestyle{startchap}%
%	\setcounter{ProbNum}{0}% added7aug14
%%	\gdef\ChapTitle/{Chapter}%
%%	\gdef\ChapAbbrev/{Chap}%
%	\global\tok@Chap={#1::}% added :: 21 april 2014
%%	\gdef\runninghead{#2}%   % added15 Aug 2011 for 241
%%	\@Chapter{#1}{#2}%
%%	\gdef\ChapterNumber{\csname \ChapAbbrev/#1\endcsname}%
%%  	\setcounter{chapter}{\ChapterNumber}% for Latex numbering of sections
%	\gdef\TheNumber{\csname Chap#1\endcsname}%
%  	\setcounter{chapter}{\TheNumber}% for Latex numbering of sections
%  	\advance\c@chapter\m@ne
%	\renewcommand{\thechapter}{\arabic{chapter}}% 8 July 2015
%	\renewcommand{\@chapapp}{Chapter}%
%	\gdef\ChapterName{#2}%
%	\chapter{#2}%
%%\ifDRAFT
%%\pagestyle{draft}
%%\thispagestyle{startchap}%
%%\fi
%}

%Old version in book.cls
%\newcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
%				   \thispagestyle{plain}%
%				   \global\@topnum\z@
%				   \@afterindentfalse
%				   \secdef\@chapter\@schapter}
%\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
%                       \if@mainmatter
%                         \refstepcounter{chapter}%
%                         \typeout{\@chapapp\space\thechapter.}%
%                         \addcontentsline{toc}{chapter}%
%                                   {\protect\numberline{\thechapter}#1}%
%                       \else
%                         \addcontentsline{toc}{chapter}{#1}%
%                       \fi
%                    \else
%                      \addcontentsline{toc}{chapter}{#1}%
%                    \fi
%                    \chaptermark{#1}%
%                    \addtocontents{lof}{\protect\addvspace{10\p@}}%
%                    \addtocontents{lot}{\protect\addvspace{10\p@}}%
%                    \if@twocolumn
%                      \@topnewpage[\@makechapterhead{#2}]%
%                    \else
%                      \@makechapterhead{#2}%
%                      \@afterheading
%                    \fi}
%\def\@makechapterhead#1{%
%  \vspace*{50\p@}%
%  {\parindent \z@ \raggedright \normalfont
%    \ifnum \c@secnumdepth >\m@ne
%      \if@mainmatter
%        \huge\bfseries \@chapapp\space \thechapter
%        \par\nobreak
%        \vskip 20\p@
%      \fi
%    \fi
%    \interlinepenalty\@M
%    \Huge \bfseries #1\par\nobreak
%    \vskip 40\p@
%  }}

\renewcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
%	\thispagestyle{plain}% 15 July 2015, to allow different pagestyle
	\global\@topnum\z@
	\@afterindentfalse
	\secdef\@chapter\@schapter}

\def\Chapter#1#2{
	\clearpage%	added 24 nov 2015
	\global\tok@Chap={#1::}% added :: 1 July 2015
	\gdef\ChapterName{#2}%
	\gdef\chaptertitlename{Chapter}%
	\gdef\ChapTitle/{Chapter}%	added 20 nov 2015
	%\gdef\chaptertitlename{Appendix}%

  	\setcounter{chapter}{\csname Chap#1\endcsname}% for Latex numbering of sections
	\renewcommand{\thechapter}{\arabic{chapter}}% 8 July 2015
	\renewcommand{\@chapapp}{Chapter}%
	\@Chapter{#2}%
	\LABEL{#1}%26Sept2017

}

\def\@Chapter#1{% #1 = chap/app title
	\ifDRAFT
		\pagestyle{draft}\else\pagestyle{normal}%
	\fi	
	\thispagestyle{startchap}%
	\setcounter{ProbNum}{0}% added7aug14
	\advance\c@chapter\m@ne% needed because \chapter does a refstep
	\chapter{#1}%
	\markboth{#1}{}%

}


%\def\Appendix#1#2{
%	\ifDRAFT
%		\pagestyle{draft}\else\pagestyle{normal}%
%	\fi	
%	\thispagestyle{startchap}%
%	
%	\setcounter{ProbNum}{0}% added7aug14
%	\global\tok@Chap={#1::}% added :: 1 July 2015
%	\gdef\TheNumber{\csname App#1\endcsname}%
%  	\setcounter{chapter}{\TheNumber}% for Latex numbering of sections
%  	\advance\c@chapter\m@ne% needed because \chapter does a refstep
%	\renewcommand{\thechapter}{\Alph{chapter}}% 8 July 2015
%	\renewcommand{\@chapapp}{Appendix}%
%	\gdef\ChapterName{#2}%
%	\chapter{#2}%
%}

%\def\Appendix#1#2{
%   \clearpage%	added 24 nov 2015
%   
%   \global\tok@Chap={#1::}% added :: 1 July 2015
%   \gdef\ChapterName{#2}%
%   \gdef\ChapTitle/{Appendix}%	added 20 nov 2015
%   \gdef\chaptertitlename{Appendix}%
%   \setcounter{chapter}{\csname App#1\endcsname}% for Latex numbering of sections
%   \renewcommand{\thechapter}{\Alph{chapter}}% 8 July 2015
%   \renewcommand{\@chapapp}{Appendix}%
%   \@Chapter{#2}%
%}
\def\Appendix#1#2{
	\clearpage%	added 24 nov 2015
%	\addcontentsline{toc}{Appendix}{\protect\numberline{\Alph{Appendix} #2}}%
  	\global\tok@Chap={#1::}% added :: 1 July 2015
%  	\pdfbookmark[0]{#2}{\the\tok@Chap}
	\gdef\ChapterName{#2}%
	\gdef\ChapTitle/{Appendix}%	added 20 nov 2015
	\renewcommand{\@chapapp}{Appendix}%

	\gdef\chaptertitlename{Appendix}%
	\thispagestyle{startchap}%
	\setcounter{section}{0}
  	\setcounter{Appendix}{\csname App#1\endcsname}% for Latex numbering of sections
	\def\thechapter{\Alph{Appendix}}%
	\LABEL{#1}%
	\phantomsection	
   	\addcontentsline{toc}{Appendix}%
           {\protect\numberline{\Alph{Appendix}}#2}%
	\@makeAppendixhead{#2}%
	\@afterheading

%   \renewcommand{\thechapter}{\Alph{chapter}}% 8 July 2015
%   \renewcommand{\@chapapp}{Appendix}%
%	\@Chapter{#2}%
}

\def\@makeAppendixhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright \normalfont
    \ifnum \c@secnumdepth >\m@ne
      \if@mainmatter
        \huge\bfseries \@chapapp\space \Alph{Appendix}
        \par\nobreak
        \vskip 20\p@
      \fi
    \fi
    \interlinepenalty\@M
    \Huge \bfseries #1\par\nobreak
    \vskip 40\p@
  }}

\newcommand*\l@Appendix[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      #1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
      \penalty\@highpenalty
    \endgroup
  \fi}

\def\first@page#1#2{\relax}  %written to .aux file by \@Chapter
\def\last@page#1#2{\relax} 
\def\endChapter{%
	\protected@write \@auxout {}{\string \last@page{\thisChap}{\thepage}}%
%	\protected@write \@auxout {}{^^J}%
}
\def\endChapter{%  EXPT 20 April 2014
	\relax
	%\protected@write \@auxout {}{\string \last@page{\thisChap}{\thepage}}%
%	\protected@write \@auxout {}{^^J}%
}
\let\endAppendix=\endChapter


%\makeatletter
\newcommand{\TOC}{ \@starttoc{toc}}
%\makeatother

\newcommand\TableofContents{%
\bgroup
\pagestyle{roman}%
\tableofcontents
\vskip1in
\ifDRAFT Printed: \today\fi
%\vfil\newpage
\egroup
}

%\newcommand{\oldmakechapter}{%
%%\showlabels
%\begin{document}%
%%\setcounter{page}{0}%		4 Mar 2014, causes empty first page?
%%\thispagestyle{startchap}%
%%\pagestyle{startchap}%
%%\TableofContents
%\TOC
%\setcounter{page}{0}%
%%\bigskip
%%Version: \version
%\ifDRAFT
%	\vskip1in
%	Printed: \today
%	\thispagestyle{startchap}%
%\fi
%\input chapter.tex
%\end{document}%
%}


%\newcommand{\makechapter}{%
%%\input dbp-hyperref.sty
%\DRAFTtrue
%\getGLOBALS
%\showlabels
%
%\begin{document}%
%\LOCALlabels
%
%\TOC
%\setcounter{page}{0}%
%\ifDRAFT
%	\vskip1in
%	Printed: \today
%	\thispagestyle{startchap}%
%\fi
%\input chapter.tex
%\end{document}%
%}
%

\newcommand{\Preface}[1]{%
%	\pagestyle{roman}%
	\pagestyle{preface}%
	\thispagestyle{startchap}%
	\noindent{\Huge \textbf {Preface #1}\hfil}\par% 
	\bigskip
	\addcontentsline{toc}{chapter}{\qquad Preface #1}%
}
\makeatother


\renewcommand{\thechapter}{\arabic{chapter}}


%%%%%%	End of dbp-chapter.sty


%%%%%%	\input dbp-sections.sty	%\usepackage{titlesec}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Sections 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\usepackage{titlesec}
\makeatletter

%\newcommand{\makememo}{%
%	\pagestyle{memo}%
%	\renewcommand{\thesection}{\@arabic\c@section}%
%}
%\newcommand{\makememo}{% 15 July 2015
%   \pagestyle{memo}%
%   \thispagestyle{startchap}% 
%   \renewcommand{\thesection}{\@arabic\c@section}%
%}

\def\SectionName{\relax}
%\def\Notes{\pagestyle{notes}\section*{Notes}}
\def\Notes{%
	\section*{Notes}%
%	\mark{{Notes for Chapter \ChapterNumber}}%
	\gdef\SectionName{Notes for \ChapTitle/ \ChapterNumber}
	\normalfalse
}

\@addtoreset{section}{Chapter}%
\@addtoreset{section}{Appendix}% 21 Sept 2017
%\@addtoreset{section}
%\def\Sref#1{\ref{Sect.#1}}
%\let\sref=\Sref

\def\sect@star{\relax}

\titlelabel{\llap{\sect@star\thetitle\qquad}}

%\renewcommand\sectionmark[1]
%{\markboth{\thesection\ #1}{}}
%\renewcommand\subsectionmark[1]
%{\markright{\thesubsection\ #1}}



\def\Section[#1]#2{%
	\gdef\SectionName{#2}%
	\def\sect@star{\relax}%
	\section{#2}%
	\markright{\normalfont\S\thesection\ #2}% added 9 July 2107
%	\label{\the\tok@Chap#1}%
%	\label{\the\tok@Chap#1}%
	\label{\the\tok@Chap\the\tok@Sect#1}%
%	\if@GLOBAL
	\ifGLOBALS
		%\edef\tmp@gbl{\string\gslabel{\the\tok@Chap#1}{\@currentlabel}{\thepage}}%
\edef\tmp@gbl{\string\gslabel{\the\tok@Chap\the\tok@Sect#1}{\@currentlabel}{\thepage}}%
		\protected@write\@auxout{}{\string\@writefile{gbl}{\tmp@gbl}}%
	\fi
%	\label{\the\tok@Chap..#1}%  for hyperref
	\check@for@SN@abbrev
}

%\def\subSection#1{%  added 9 July 2107
%   \gdef\subSectionName{#1}%
%   \def\sect@star{\relax}%
%   \subsection{#1}%
%   \markright{\normalfont\S\thesubsection\ #1}% 
%}
%


\newcommand\starsection{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\Large\bfseries}}

%\newcommand{\starsection}[1]{%
%%	\addtocounter{section}{1}%
%%\addcontentsline{toc}{section}{\llap{*}\thesection\hspace{1em}#1}%
%\addcontentsline{toc}{section}{\texorpdfstring{\llap{*}\thesection\hspace{1em}}{*}#1}%
%	{\vspace{1.5em}\par\noindent\bfseries\Large \llap{*\thesection\qquad}#1}\par\vspace{1em}\noindent%
%}

\def\starSection[#1]#2{%
%	\addtocounter{section}{1}%
	\gdef\SectionName{#2}%
	\def\sect@star{*}%
%	\setcounter{starsection}{\value{section}}%
%	\stepcounter{section}%
	\starsection{*#2}%
%	\section{#2}%
%	\section{#2}%
	\label{\the\tok@Chap\the\tok@Sect#1}%
%	\label{#1}%
%	\if@GLOBAL
	\ifGLOBALS
		\edef\tmp@gbl{\string\gslabel{\the\tok@Chap\the\tok@Sect#1}{\thesection}{\thepage}}%
		\protected@write\@auxout{}{\string\@writefile{gbl}{\tmp@gbl}}%
	\fi
%	\label{\the\tok@Chap--#1}%  hyperref doesn't seem to like ::
	\check@for@SN@abbrev
}

\def\check@for@SN@abbrev{\@ifnextchar[{\resetSN}{\relax}}

\def\resetSN[#1]{\def\SectionName{#1}}

\def\gslabel#1#2#3{\expandafter\gdef\csname#1\endcsname{#2}}
\def\gsref#1{\csname#1\endcsname}
%%\let\starSection=\Section
%\def\starSection[#1]#2{%
%	\gdef\SectionName{#2}%
%	\def\sect@star{*}%
%	\starsection{#2}%
%	\section{#2}%
%	\label{\the\tok@Chap::#1}%
%	\check@for@SN@abbrev
%}



\def\Sref{%
	\@ifnextchar[{\@Sref}%	
	{\@Sref[\the\tok@Chap]}%
}
\def\@Sref[#1]#2{\ref{#1::#2}}
%\def\@Sref[#1]#2{\ref{#1--#2}}%  hyperref trouble
%\let\sref=\Sref
\def\sref#1{\ref{\the\tok@Chap\the\tok@Sect#1}}
\def\sref#1{\REF{\the\tok@Sect#1}}

\def\GSREF#1#2{\csname#1::S:#2\endcsname}

%\def\starSection[#1]#2{\section*{#2}\label{Sect.#1}}

\def\beginPreamble{\bgroup\itshape}
\def\endPreamble{\bigskip\egroup}

%\def\SectionDescribe#1{\par\hangindent\parindent\hangafter1\noindent
%\expandafter SECTION~{\ref{Sect.#1}}
%% 4 april 2014: seems to mess with hyperref
\def\SectionDescribe#1{\par
\hangindent\parindent\hangafter1\noindent
{\scshape Section}~{\ref{\the\tok@Chap\the\tok@Sect#1}}}
%{\scshape Section}~{\ref{#1}}}

%\def\SectionDescribe#1{Section~\sref{#1}}


\def\starSectionDescribe#1{\par
\hangindent\parindent\hangafter1\noindent
\expandafter {\scshape \llap{*}Section}{\ref{\the\tok@Chap\the\tok@Sect#1}}}
%\expandafter {\scshape \llap{*}Section}~{\ref{#1}}}

%\def\PreambleSref#1{\getcref{Sect.#1}\crefsection}
%
%\def\beginPreamble{%
%\bgroup\SDfont\baselineskip=11.5pt\let\sref=\PreambleSref}
%\def\endPreamble{\bigskip\egroup}

\makeatother

%%%%%%	End of dbp-sections.sty


%%%%%%	\input dbp-theorems.sty %\usepackage{ntheorem}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Theorems, Examples, ...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\usepackage{ntheorem}

\makeatletter

\newtheoremstyle{DBPrem}%
  {\item[\theorem@headerfont  ##1\theorem@separator]}%
  {\item[\theorem@headerfont  ##1\ (##3)\theorem@separator]}

\def\shim#1{\hskip#1pt\relax}

\newtheoremstyle{DBP4}%
  {\item[\hbox to5.5pt{\hss$<$##2$>$\shim{20}}\theorem@headerfont  ##1\theorem@separator]}%
  {\item[\hbox to6pt{\hss$<$##2$>$\shim{21}}\theorem@headerfont  ##1\ [##3]\theorem@separator]}

\newtheoremstyle{named}%
  {\item[\hbox to5.5pt{\hss$<$##2$>$\shim{20}}\theorem@headerfont  ##1\theorem@separator]}%
  {\item[\hbox to6pt{\hss$<$##2$>$\shim{21}}\theorem@headerfont  ##1\ [##3]\theorem@separator]}




\theoremstyle{DBP4}
\theoremseparator{.}
\theoremheaderfont{\normalfont\bfseries}
%\theoremheaderfont{\sc} %commented out 9 nov09
\theorembodyfont{\itshape}
\newtheorem{theorem}[equation]{Theorem}
%\newtheorem{theorem}[equation]{$|$Theorem}  % for testing

\newtheorem{definition}[equation]{Definition}
\newtheorem{lemma}[equation]{Lemma}
\newtheorem{corollary}[equation]{Corollary}
\newtheorem{assumption}[equation]{Assumption}

\newcommand{\thmname}{\relax}
\newtheorem{named}[equation]{\thmname}

\theorembodyfont{\normalfont}

\newtheorem{example}[equation]{Example}
\newtheorem{exercise}[equation]{Exercise}


\theoremstyle{DBPrem}
\theoremindent2em
\theorembodyfont{\small}
\newtheorem{remark}{Remark}
\newtheorem{remarks}{Remarks}


\theoremindent0pt

%\theoremheaderfont{\sc}
\theoremheaderfont{\scshape}
\theorembodyfont{\upshape}
\theoremstyle{nonumberplain}
\theoremseparator{}
\theoremsymbol{\rule{1ex}{1ex}}
\newtheorem{proof}{Proof}
\newtheorem{solution}{{\scshape Solution:}}

\def\makeQED{\noindent\vadjust{\llap{\qedsymbol\qquad}}}
%\def\Remark{\begin{remark}\rightskip=3em plus2em minus2pt}
\def\Remark{% 15 aug 2014
	\vskip-\lastskip\smallskip
	\begin{remark}\rightskip=3em plus2em minus2pt}
\def\endRemark{\end{remark}}

\def\Remarks{\begin{remarks}\rightskip=3em plus2em minus2pt}
\def\endRemarks{\end{remarks}}

\def\Corollary{\begin{corollary}}
\def\endCorollary{\end{corollary}}

\def\Definition{\begin{definition}}
\def\endDefinition{\end{definition}}

\def\Theorem{\begin{theorem}}
\def\endTheorem{\end{theorem}}

\def\NamedTheorem{\begin{theorem}}
\def\endNamedTheorem{\end{theorem}}

\def\Lemma{\begin{lemma}}
\def\endLemma{\end{lemma}}

\def\Assumption{\begin{assumption}}
\def\endAssumption{\end{assumption}}

\def\Example{\begin{example}}
%\def\endExample{\end{example}}
\def\endExample{\noindent\vadjust{\llap{\qedsymbol\qquad}}\end{example}}

\def\Exercise{\begin{exercise}}
%\def\endExample{\end{example}}
\def\endExercise{\noindent\vadjust{\llap{\qedsymbol\qquad}}\end{exercise}}


%\def\Proof{\begin{proof}}
%31 aug 2014: 
\def\Proof{%
	\vskip-\lastskip\smallskip
	\begin{proof}}
\def\endProof{\end{proof}}

\def\Solution{\begin{solution}}
\def\endSolution{\end{solution}}

\def\mnote#1{\marginpar{\par\tiny#1}}
\def\msymbol#1{\marginpar{\par\tiny#1}}


\def\qed@{\ifmmode{\square}\else{$\square$}\fi}
\let\qedsymbol=\qed@
%\def\endProof{\noindent\vadjust{\llap{\qedsymbol\qquad}}\end{proof}}
\def\endProof{\vskip-\lastskip\noindent\vadjust{\llap{\qedsymbol\qquad}}\end{proof}}


\makeatother

%%%%%%	End of dbp-theorems.sty

	%\input dbp-eqn-num.sty moved into globals.sty
	%\input dbp-labelstyle.sty
	%\input dbp-showlabels.sty \usepackage{showkeys} instead
	%\input dbp-headings.sty

%%%%%%	\input dbp-pictures.sty
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% wrapping text around pictures
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% dbp-pictures.sty 9 July 2014
%	WrapPara
%	PlaceBox
%	WrapPicture

\usepackage{graphicx}

\makeatletter

\newtoks\pic@toks
\newtoks\pic@line
\newtoks\as@is

\newcount\indent@lines
\newcount\skip@lines
\newcount\count@
%\newcount\count@@
\newcount\dbp@count	% scratch

\newdimen\para@indent
\newdimen\dbp@dimen	% scratch

\newsavebox{\piccy}
\newsavebox{\Piccy}
\newsavebox{\dbpbox}	% scratch


\newif\ifcenter@box
\center@boxfalse

% \Scrunched is defined in BoxedEPS
% \Scrunched#1 puts #1 in an hbox then zeros the dimensions of the box
% Based on boxedeps.tex
\newbox\scrunchbox
\def\Scrunched#1{{\setbox\scrunchbox\hbox{#1}%
  \wd\scrunchbox=0pt
  \ht\scrunchbox=0pt
  \dp\scrunchbox=0pt
  \box\scrunchbox}}


\as@is=\expandafter{ 0pt \the\hsize }	% for non-indented lines
% to accumulate for \parshape:
\def\addto@pic@toks#1{\pic@toks=\expandafter{\the\pic@toks \the#1 }} 
\def\Lower@Box#1{\setbox#1\hbox{\raise-\ht#1\hbox{\box#1}}}


\def\dbpPic@topPad{0}
\def\dbpPic@sidePad{0}
\def\dbpPic@botPad{0}
\def\dbpPic@indent{0}
\def\dbpPic@height{1in}
\def\dbpPic@skip{0}

\define@key{dbpPic}{height}[1in]{\def\dbpPic@height{#1}}
\define@key{dbpPic}{skip}[2]{\def\dbpPic@skip{#1}}
\define@key{dbpPic}{indent}[0]{\def\dbpPic@indent{#1}}

\define@key{dbpPic}{topPad}[0]{\def\dbpPic@topPad{#1}}
\define@key{dbpPic}{sidePad}[1pc]{\def\dbpPic@sidePad{#1}}
\define@key{dbpPic}{botPad}[0]{\def\dbpPic@botPad{#1}}
\define@key{dbpPic}{center}[F]{\center@boxtrue}

%
% \WrapPara#1#2#3
% #1 = num of lines before indentation; -> \skip@lines
% #2 = num of lines in indentation; -> \indent@lines
% #3 = width of indentation; -> \para@indent

\def\WrapPara#1#2#3{%
	\skip@lines =  #1
	\indent@lines =  #2
	\para@indent = #3
	\dimen2 = \hsize
	\advance \dimen2 by -\para@indent 
	\pic@line=\expandafter{ \the\para@indent\space \the\dimen2 }%
	%
	\count@= #2
	\advance\count@ by #1
	\advance\count@ by 1
	%Total number of lines involved in parshape:
	\pic@toks=\expandafter{\the\count@}%
	%
	\count@ = #1 % num of lines before picture
	\loop
	\ifnum\count@>0
	\addto@pic@toks{\as@is}%
	\advance\count@ by -1
	\repeat
	%
	\count@ = #2 % num of lines in picture
	\loop
	\ifnum\count@>0
	\addto@pic@toks{\pic@line}%
	\advance\count@ by -1
	\repeat
	%
	\addto@pic@toks{\as@is}  % rest of para
	\parshape\the\pic@toks
}

% \PlaceBox[#1]#2#3
%
% #1 = number of lines skipped
% #2 = the box number
% #3 = indentation
\def\PlaceBox[#1]#2#3{%
	% Number of lines before indentation:
	\skip@lines = #1
	%
	% Calculate number of lines to indent:
	\setbox0=\hbox{\copy#2}%
	\Lower@Box0
	\dimen0 = \dp0
	\dbp@dimen = \dp0
	\divide\dimen0 by \baselineskip
	\dbp@count = \dimen0	%convert to integer
	\advance\dbp@count by 1  
	%
	\dimen8=0.5\baselineskip
	\advance\dimen8 by \prevdepth
	\advance\dimen8 by -#1\baselineskip
	% Build paragraph:
	\WrapPara{\skip@lines}{\dbp@count}{#3}\noindent%
	% Insert picture box:
	\vadjust{\Scrunched{\raise\dimen8\copy0}}% 
	%
	\ignorespaces
}

%\WrapPicture[#1=options]#2
% options: 
%	height, indent, skip,
%	topPad, sidePad, botPad,
%	center=F (not implemented)
%	#2 = pdf file w/o extension
\def\WrapPicture{\@ifnextchar[\@WrapPicture{\@WrapPicture[]}}
\def\@WrapPicture[#1]#2{
	\setkeys{dbpPic}{#1}%
	\sbox{\piccy}{\includegraphics[%
		height=\dbpPic@height]
		{#2}%
	}%
	%
	% How far to move horizontally:
	\dbp@dimen = \dbpPic@indent
	\advance\dbp@dimen by -\wd\piccy
	\advance\dbp@dimen by -\dbpPic@sidePad
	%
	\savebox{\Piccy}{%
	\vbox{\moveright\dbp@dimen
	\vbox{\vskip\dbpPic@topPad\copy\piccy\vskip\dbpPic@botPad}}}%
	%
	\PlaceBox[\dbpPic@skip]{\Piccy}{\dbpPic@indent}%
}

\def\endWrapPicture{\parshape=0}
\makeatother



%%%%%%	End of dbp-pictures.sty


%%%%%%	\input dbp-problems.sty
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Problems
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\usepackage{comment}
%\usepackage{verbatim}
\makeatletter

\newcounter{ProbNum}
%\setcounter{ProbNum}{0}
\newcounter{pPartNum}[ProbNum]

%\def\Problems{\pagestyle{problems}\section*{Problems}\setcounter{ProbNum}{0}}

\def\Problems{%
	\section*{Problems}%
	\setcounter{ProbNum}{0}%
	\mark{{Problems for \ChapTitle/ \ChapterNumber}}%
	\gdef\SectionName{Problems for \ChapTitle/ \ChapterNumber}
	\normalfalse
}

%\def\Pref#1{\ref{[#1]}}% the [] are now part of the label
%\def\Pref#1{[\ref{Prob.#1}]}% 30 april 2014, removed [] 
%\def\Pref#1{[\ref{\the\tok@Prob#1}]}% 6 sept2015
%\def\Pref#1{[\REF{\the\tok@Prob#1}]}% 6 sept2015
\def\Pref#1{[\REF{\the\tok@Prob#1}]}% 6 sept2015

\def\problem[#1]{%
	\gdef\star@{}%
	\problem@[#1]%
}

\def\starproblem[#1]{%
	\gdef\star@{*}%
	\problem@[#1]%
}
	
\def\problem@[#1]{%
	\bigskip
%	\stepcounter{ProbNum}%
	\refstepcounter{ProbNum}%
	\bgroup
%	\def\@currentlabel{[\theProbNum]}%
	\def\@currentlabel{\theProbNum}%
%	\ifshowlabels\label{ #1}\fi%    added 11 sep 2012; note the space
%	\label{Prob.#1}%    
	%\protected@write \@auxout {}{\string \newlabel {[#1]}{{\theProbNum}{\thepage}}}%
	\noindent
%	\llap{\star@[\ref{Prob.#1}]\qquad}\ignorespaces%
	\llap{\star@[\ref{\the\tok@Chap\the\tok@Prob#1}]\qquad}\ignorespaces%
	\LABEL{\the\tok@Prob#1}%    
	\egroup
	\ignorespaces% added 1 May 2014
	%\ifshowlabels\marginpar{\small\itshape #1}\fi
}

%\def\ppart{% \smallskip\item{(\romannumeral\thepPartNum)}\stepcounter{pPartNum}}
%\def\ppart{\smallskip(\romannumeral\thepPartNum)\stepcounter{pPartNum}}
%
%\def\ppart{\smallskip\stepcounter{pPartNum}\noindent\llap{(\roman{pPartNum})\quad}}

\def\ppart{\par\smallskip\stepcounter{pPartNum}\noindent\llap{(\roman{pPartNum}) }}
\def\starppart{\par\smallskip\stepcounter{pPartNum}\noindent\llap{$^*$(\roman{pPartNum}) }}


\makeatother



% The following macros are intended for use with problem sets and their solutions


\ifx\coursename\isnotdefined
	\def\coursename{COURSENAME?}%
\fi




\newenvironment{nohide}[0]{\vskip-\lastskip\smallskip\indent\color{black}\normalfont}{\vskip-\lastskip\bigskip}

\def\HW#1#2{%
\def\HWsheetNum{##1}
\pagestyle{blank}%
\begin{flushleft}%
\normalsize\textbf
\coursename\newline
Homework \# #1\newline
Due: #2
\end{flushleft}\small
}

\def\HW#1#2{%
\gdef\HWsheetNum{#1}%
\let\soln=\comment
\pagestyle{blank}%
\gdef\StateProb{\relax}
\gdef\endStateProb{\relax}
%\setlength{\marginparwidth}{0.5in}%
\oddsidemargin = 1in
\begin{flushleft}%
\normalsize\textbf
\coursename\newline
Homework \# #1\newline
Due: #2
\end{flushleft}\small
}

\def\Orchidit{\color{DarkOrchid3}\it}

\def\StartSolutionSheet#1#2{%
\global\def\HWsheetNum{#1}%
\let\soln=\nohide
\pagestyle{blank}%
%\setlength{\marginparwidth}{0.5in}%
\oddsidemargin = 1in
\begin{flushleft}%
\normalsize\textbf
\coursename\newline
Solutions to sheet #1  \hfil
\end{flushleft}
%\small
%%\line{\bf Statistics 241/541 \hfil}
%\line{\bf2011: Solutions to sheet #1  \hfil}
\bigskip
%\color{DarkOrchid3}\it
\Orchidit
}	

\let\solveHW=\StartSolutionSheet

\def\bQuote{\begin{quotation}\noindent\itshape}
\def\eQuote{\end{quotation}}

\long\def\StateProb{\bgroup\color{DarkOrchid3}\it\small\noindent}
%\def\endStateProb{\smallskip\egroup\par}
\def\endStateProb{\vskip-\lastskip\smallskip\egroup\par}


%%%%%%	End of dbp-problems.sty


%%%%%%	\input dbp-quote.sty
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Quotations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newdimen\quotationindent
\quotationindent=3em
\def\Quotation{
\par\bgroup\advance\leftskip by\quotationindent
%\rightskip=0pt plus 1pt minus1pt\spaceskip=0pt\xspaceskip=0pt
\advance\rightskip by\quotationindent
\medskip\noindent\nobreak
\ninerm\baselineskip11pt}

\def\Quotation{
\par\bgroup\small\advance\leftskip by\quotationindent
%\rightskip=0pt plus 1pt minus1pt\spaceskip=0pt\xspaceskip=0pt
\advance\rightskip by\quotationindent
\medskip\noindent\nobreak
\baselineskip11pt}

\def\endQuotation#1{\par\nobreak
\hbox to\hsize{\hfil[#1]\kern\quotationindent}\smallskip
\egroup\noindent\ignorespaces}

%\let\quote=\Quotation
%\def\endquote{\medskip\egroup\noindent\ignorespaces}
\newenvironment{Quote}[1]%
{\def\thecite{\small\rm#1}\begin{quotation}\noindent\itshape``}%
{\unskip''\begin{flushright}\vskip-\lastskip\thecite\end{flushright}\end{quotation}}%
%%%%%%	End of dbp-quote.sty


%%%%%%	\input dbp-bibliography.sty

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Bibliography
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%/Users/pollard/Library/texmf/bibtex/bib/DBP.bib
%\def\References{\bibliographystyle{chicago}\bibliography{DBP}}

%\def\References{\pagestyle{references}\bibliographystyle{chicago}\bibliography{DBP}}


\usepackage{url}%	added 13 july 2015
%\bibliographystyle{plainnat}
\bibliographystyle{chicago}%
%\setcitestyle{round}

\def\References{%
%	\bibliographystyle{chicago}%
%	\bibliographystyle{plainnat}%
	\bibliography{DBP}%
%	\gdef\SectionName{References for \ChapTitle/ \ChapterNumber}%
%	\gdef\SectionName{References for \ChapTitle/ \thechapter}%
	\normalfalse

}

%\def\citeNthree#1#2#3{\citeANP{#1}~(\citeyearNP{#1}, \citeyearNP{#2}, \citeyearNP{#3})}

\makeatletter
\def\refname{References}
\renewcommand\bibsection{%
   \section*{\refname\@mkboth{\MakeUppercase{\refname}}{\MakeUppercase{\refname}}}%
  }%
\makeatother

%%%%%%	End of dbp-bibliography.sty


%%%%%%	\input dbp-hyperref.sty
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% hyperref
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%% trouble with hyperref
% in case hyperref not loaded
\def\texorpdfstring#1#2{\llap{*}\thesection\hspace{1em}}
\def\phantomsection{\relax}



\makeatletter
\def\useHYPERREF{%
	\usepackage{hyperref}%
	\hypersetup{%backref,
		colorlinks=true,
		linkcolor=DeepPink3,
		citecolor=blue,
%		bookmarks=true, % default
		hypertexnames=false,% 26 Sept 2017
		pdfpagelayout=OneColumn, % added 22 Aug 2016
		pdfview=FitH, % added 22 Aug 2016
		pdfstartview=FitH, % added 22 Aug 2016
		implicit=false % needed for showkeys?
		}%
	\global\def\@setref##1##2##3{% csname, extract group, refname
	  \ifx##1\relax
		\protect\G@refundefinedtrue
		\nfss@text{\reset@font\bfseries \color{red}##3}%
		\@latex@warning{%
		  Reference `##3' on page \thepage \space undefined%
		}%
	  \else
		\expandafter\Hy@setref@link##1\@empty\@empty\@nil{##2}%
	  \fi
%	  \usepackage[%
%		open,%
%		openlevel=2,%
%		atend
%		]{bookmark}[2016/05/17]%

%	  \gdef\HyperDestNameFilter##1{\the\tok@Chap##1}%
	  %\show\HyperDestNameFilter
	}

%\typeout{^^JDid we change setref?^^J}
%\show\@setref 
%\typeout{^^JAgain, did we change setref?^^J}

}

\makeatother
%\makeatletter
%\renewcommand*{\HyperDestNameFilter}[1]{\the\tok@Chap-#1}
%\makeatother

%\def\@setref#1#2#3{% csname, extract group, refname
%\ifx#1\relax
%  \protect\G@refundefinedtrue
%  \nfss@text{\reset@font\bfseries #3}%
%  \@latex@warning{%
%	Reference `#3' on page \thepage \space undefined%
%  }%
%\else
%  \expandafter\Hy@setref@link#1\@empty\@empty\@nil{#2}%
%\fi
%}

%%%%%%	End of dbp-hyperref.sty


\typeout{^^JCOMPLETED dbp-book.sty^^J}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\endinput
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%












