% Turabian Formatting for LaTeX
%
% Based on the Chicago Manual of Style (16th edition) and Kate Turabian's A Manual for Writers of Research Papers, Theses, and Dissertations (8th edition)
%
% ==============================
% Copyright 2013 Omar Abdool
%
% This work may be distributed and/or modified under the conditions of the LaTeX
% Project Public License (LPPL), either version 1.3 of this license or (at your
% option) any later version.
%
% The latest version of this license is in:
%	http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX version
% 2005/12/01 or later.
%
% LPPL Maintenance Status: maintained (by Omar Abdool)
%
% This work consists of the files: turabian-formatting.sty,
% turabian-researchpaper.cls, turabian-thesis.cls, turabian-formatting-doc.tex, % and turabian-formatting-doc.pdf (in addition to this README file).
%
% ==============================
%
% Documented errors from required packages (ignore):
%	- LaTeX warning: "xparse/redefine-command"
%	  Redefining document command \oldstylenums with arg. spec. 'm' on line 128.
%
% Implementation notes:
%	- package must be loaded before biblatex-chicago to work properly;
%	- known bug: use of geometry showframe option and plain page style with emptymargins option
%
%


\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{turabian-formatting}[2014/12/27 Turabian Formatting]


% Flags adjusted with package options

\newif\if@draftwork\@draftworkfalse

\newif\if@optraggedright\@optraggedrightfalse

\newif\if@optemptymargins\@optemptymarginsfalse

\newif\if@maketitlepage\@maketitlepagetrue

\newif\if@appendrefformat\@appendrefformattrue


% Options for package

\DeclareOption{draft}{\@draftworktrue}

\DeclareOption{raggedright}{\@optraggedrighttrue}

\DeclareOption{emptymargins}{\@optemptymarginstrue}

\DeclareOption{notitlepage}{\@maketitlepagefalse}

\DeclareOption{noadjustbib}{\@appendrefformatfalse}

\ProcessOptions\relax


% Packages used throughout

\RequirePackage{setspace, xifthen, etoolbox}


% Page geometry and layout

\RequirePackage{geometry}

\geometry{margin=1in, ignoreall}
\geometry{headheight=14.5pt, headsep=0.5in-\headheight}
\geometry{footskip=0.5in-\headheight}

\if@optemptymargins
	\geometry{includehead}
\fi


% Text formatting

\doublespacing
\setlength{\parindent}{0.5in}

\if@optraggedright
	\newlength{\@parindentraggedright}
	\setlength{\@parindentraggedright}{\parindent}
	\raggedright
	\setlength{\parindent}{\@parindentraggedright}
\fi

\RequirePackage[defaultlines=2, all]{nowidow}

\RequirePackage{url}
\urlstyle{same}


% Footnote layout and formatting

\RequirePackage[bottom,marginal]{footmisc}

\setlength{\skip\footins}{12pt}
\setlength{\footnotesep}{1\baselineskip}

\renewcommand{\footnoterule}{%
	\vspace*{-3pt}
	\hrule width 2in height 0.4pt
	\vspace*{-10.4pt}
}

% Footnote marker indent
\setlength{\footnotemargin}{0.5in}

% footnotemark
\newcommand{\@turabianfnmarkformat}{%
	\renewcommand{\@makefntext}[1]{%
		\if@optraggedright
			\raggedright%
		\fi
		\setlength{\parindent}{\footnotemargin}%
		\@thefnmark.\,\,##1%
	}
}

% keep footnote lines together
\interfootnotelinepenalty=10000


% Header and footer style layouts

\RequirePackage{fancyhdr}

% Page identifier (author name, title, etc.)
\newcommand{\pageidentifier}{}

\newlength{\@pageidentifierspace}
\setlength{\@pageidentifierspace}{0em}

\newcommand{\setpageidentifier}[2][3ex]{%
	\setlength{\@pageidentifierspace}{#1}
	\renewcommand{\pageidentifier}{%
		#2\hspace*{\@pageidentifierspace}}
}
\setpageidentifier{}

% draft option: draft indicator text
\newcommand{\draftindicator}{}

\newcommand{\setdraftindicator}[1]{%
	\if@draftwork
		\renewcommand{\draftindicator}{#1}
	\fi
}
\setdraftindicator{Draft (\today)}

% fancyhdr: page styles
\fancyhf{}
\fancyhead[R]{\normalfont\singlespace \pageidentifier\thepage}
\fancyhead[L]{\normalfont\singlespace \small\draftindicator}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\fancypagestyle{plain}{%
	\fancyhf{}
	\fancyfoot[C]{\normalfont\singlespace \thepage}
	\fancyfoot[R]{\normalfont\singlespace \small\draftindicator}
	\if@optemptymargins
		\setlength{\headheight}{0pt}
		\setlength{\headsep}{0pt}
	\fi
}
\renewcommand{\plainheadrulewidth}{0pt}
\renewcommand{\plainfootrulewidth}{0pt}

\fancypagestyle{headings}{%
	\fancyhf{}
	\fancyhead[R]{\normalfont\singlespace \pageidentifier\thepage}
	\fancyhead[L]{\normalfont\singlespace \small\draftindicator}
}

% fancy page style: allow customization of geometry first
\AtEndPreamble{%
	\pagestyle{fancy}
}

% Thesis-related front and main matter formatting
\newlength{\@bindingoffsetfm}
\setlength{\@bindingoffsetfm}{0in}

\appto{\frontmatter}{%
	\singlespacing
	\newgeometry{margin=1in, bindingoffset=\@bindingoffsetfm, ignoreall, noheadfoot}
	\pagestyle{empty}
}
\if@optemptymargins
	\preto{\tableofcontents}{%
		\renewcommand{\contentsname}{Table of Contents}
		\newgeometry{margin=1in, bindingoffset=\@bindingoffsetfm, ignoreall, nohead, includefoot, bottom=1in+\headheight}
		\pagestyle{plain}
	}
\else
	\preto{\tableofcontents}{%
		\renewcommand{\contentsname}{Table of Contents}
		\restoregeometry
		\pagestyle{plain}
	}
\fi
\appto{\mainmatter}{%
	\doublespacing
	\restoregeometry
	\pagestyle{headings}
}


% Title section formatting (including chapter)

\PassOptionsToPackage{compact}{titlesec}

\RequirePackage{titlesec}

\titleformat{\chapter}[display]%
	{\normalfont\bfseries\normalsize\singlespacing\centering}%
	{Chapter \thechapter}%
	{1\baselineskip}%
	{}
\titlespacing*{\chapter}%
	{0em}%
	{-1.2\baselineskip}%
	{1\baselineskip}

\titleformat{\section}[block]%
	{\normalfont\bfseries\normalsize\singlespacing\centering}%
	{}%
	{0pt}%
	{}
\titlespacing*{\section}%
	{0em}%
	{0.5\baselineskip}%
	{0pt}

\titleformat{\subsection}[block]%
	{\normalfont\mdseries\itshape\normalsize\singlespacing\centering}%
	{}%
	{0pt}%
	{}
\titlespacing*{\subsection}%
	{0em}%
	{0.5\baselineskip}%
	{0pt}

\titleformat{\subsubsection}[block]%
	{\normalfont\mdseries\normalsize\singlespacing\raggedright}%
	{}%
	{0pt}%
	{}
\titlespacing*{\subsubsection}%
	{0em}%
	{0.5\baselineskip}%
	{0pt}


% Title Page: optional text

\newcommand{\mytitlepage}{}

\newcommand{\subtitle}[1]{\def\@subtitle{#1}}
\subtitle{}

\newcommand{\course}[1]{\def\@course{#1}}
\course{}

\newcommand{\institution}[1]{\def\@institution{#1}}
\institution{}

\newcommand{\setwordcount}[1]{\def\@wordcount{#1}}
\setwordcount{}


% Title Page default: reformat \maketitle (Research Paper)

\newcommand{\@makewordcount}{%
	\ifthenelse{\equal{\@wordcount}{}}%
		{}
		{\hspace*{\fill}\@wordcount}
}

\if@maketitlepage

	\renewcommand{\thanks}[1]{\def\@thanks{#1}}
	\thanks{}

	\renewcommand{\mytitlepage}{%
		\vspace*{-1.2\baselineskip}
		\vspace*{0.333\paperheight}
		\vspace*{-1in}
		\ifthenelse{\equal{\@subtitle}{}}%
			{%
				{\bfseries\@title}\par%
			}%
			{%
				{\bfseries\@title :}\par
				\vspace*{0.833\baselineskip}
				{\bfseries\@subtitle}\par%
			}
		\vspace*{\stretch{1}}
		\parskip=0.833\baselineskip
		\@author\par
		\@thanks\par
		\@course\par
		\@institution\par
		\@date\par
		\parskip=0pt
		\vspace*{\stretch{1}}
		\@makewordcount%
	}
	\renewcommand{\maketitle}{%
		\clearpage
		\newgeometry{margin=1in, ignoreall, noheadfoot}
		\thispagestyle{empty}
		{\normalfont\normalsize\centering\singlespacing
			\mytitlepage}
		\clearpage
		\restoregeometry
		\setcounter{page}{1}
	}
\fi


% Block quotations/quotes formatting

\RequirePackage{quoting}

\renewcommand*{\quotingfont}{\singlespace}
\quotingsetup{%
	vskip=0pt,
	leftmargin=0.5in,
	rightmargin=0.5in}

\if@optraggedright	
	\quotingsetup{rightmargin=0in}
\fi

\renewcommand{\quotation}{\quoting}
\renewcommand{\quote}{\quoting}


% Tables and Figures: float positioning

\renewcommand\fps@table{!htp}
\renewcommand\fps@figure{!htp}

\setlength{\textfloatsep}{1\baselineskip}
\setlength{\intextsep}{0.5\baselineskip}

\RequirePackage{flafter}


% Tables and Figures: caption formatting

\setlength{\abovecaptionskip}{0pt}
\setlength{\belowcaptionskip}{0pt}

\RequirePackage{caption}

\captionsetup{%
	format=plain,
	font=small,
	singlelinecheck=false,
	justification=justified,
	labelsep=period,
	skip=0.167\baselineskip}

\captionsetup[table]{%
	position=top}

\captionsetup[figure]{%
	position=bottom}


% Tables: source and note formatting commands

\newcommand{\tablesource}[1]{\vspace{1\baselineskip}#1\par}

\newcommand{\tablenote}[1]{\vspace{1\baselineskip}#1\par}


% endnotes: formatting support
% 	Note: Error when cite keys contain “_”

\newcommand{\@turabianendnoteformat}{%
	\@ifpackageloaded{endnotes}%
		{%
			\renewcommand{\enoteformat}{%
				\if@optraggedright
					\raggedright%
				\fi
				\setlength{\parindent}{\footnotemargin}
				\vspace*{1\baselineskip}
				\theenmark.\,\,%
			}
			\preto{\theendnotes}{%
				\clearpage
				\begingroup
					\singlespacing
					\def\enotesize{\normalsize}
					\setlength{\parskip}{0pt}
			}
			\appto{\theendnotes}{%
				\endgroup
			}
		}%
		{}
}


% biblatex-chicago: references formatting

\PassOptionsToPackage{%
	notes, isbn=false, autolang=other, backend=biber}{biblatex-chicago}

\newcommand{\@turabianbibformat}{%
	\@ifpackageloaded{biblatex-chicago}%
		{%
			\@ifclassloaded{article}%
				{%
					\defbibheading{bibliography}[\bibname]{%
						\clearpage
						\section*{##1}
						\singlespacing}
		
					\defbibheading{subbibliography}[\bibname]{%
						\subsection*{##1}
						\singlespacing}
				}%
				{%
					\defbibheading{bibliography}[\bibname]{%
						\chapter{##1}
						\singlespacing}
		
					\defbibheading{subbibliography}[\bibname]{%
						\section{##1}
						\singlespacing}
				}
			\renewcommand{\bibitemsep}{1\baselineskip}
			\renewcommand{\bibhang}{0.5in}
		}%
		{}
}


% Append references reformatting at end of preamble (noappends option)

\AtEndPreamble{%
	\if@appendrefformat
		\@turabianfnmarkformat
		\@turabianbibformat
		\@turabianendnoteformat
	\fi
}


