<commit_before>\NeedsTeXFormat{LaTeX2e}[1994/06/01]

\ProvidesPackage{tufte-common}[2008/11/16 v3.0.0 Common code for the Tufte-LaTeX styles]

%%
% We use the `xifthen' package to handle our package option switches
\RequirePackage{xifthen}


%%
% `debug' option -- provides more information in the .log file for use in
% troubleshooting problems
\newboolean{@tufte@debug}
\DeclareOption{debug}{\setboolean{@tufte@debug}{true}}

%%
% `nofonts' option -- doesn't load any fonts
% `fonts' option -- tries to load fonts
\newboolean{@tufte@loadfonts}\setboolean{@tufte@loadfonts}{true}
\DeclareOption{fonts}{\setboolean{@tufte@loadfonts}{true}}
\DeclareOption{nofonts}{\setboolean{@tufte@loadfonts}{false}}

%%
% `nols' option -- doesn't configure letterspacing
% `ls' option -- configures letterspacing
\newboolean{@tufte@letterspace}\setboolean{@tufte@letterspace}{true}
\DeclareOption{ls}{\setboolean{@tufte@letterspace}{true}}
\DeclareOption{nols}{\setboolean{@tufte@letterspace}{false}}

%%
% `book' and `handout' options
\newcommand{\@tufte@class}{article}% the base LaTeX class (defaults to the article/handout style)
\newcommand{\@tufte@pkgname}{tufte-handout}% the name of the package (defaults to tufte-handout)
\DeclareOption{book}{%
  \renewcommand{\@tufte@class}{book}
  \renewcommand{\@tufte@pkgname}{tufte-book}
  \setboolean{@tufte@titlepage}{true}
}
\DeclareOption{handout}{%
  \renewcommand{\@tufte@class}{article}
  \renewcommand{\@tufte@pkgname}{tufte-handout}
  \setboolean{@tufte@titlepage}{false}
}
\DeclareOption{article}{% `article' is just an alias for `handout'
  \renewcommand{\@tufte@class}{article}
  \renewcommand{\@tufte@pkgname}{tufte-handout}
  \setboolean{@tufte@titlepage}{false}
}

%%
% `titlepage' option -- creates a full title page with \maketitle

\newboolean{@tufte@titlepage}
\DeclareOption{titlepage}{\setboolean{@tufte@titlepage}{true}}
\DeclareOption{notitlepage}{\setboolean{@tufte@titlepage}{false}}

%%
% `a4paper' option

\newboolean{@tufte@afourpaper}
\DeclareOption{a4paper}{\setboolean{@tufte@afourpaper}{true}}

%%
% `sfsidenotes' option -- typesets sidenotes in sans serif typeface

\newboolean{@tufte@sfsidenotes}
\DeclareOption{sfsidenotes}{\setboolean{@tufte@sfsidenotes}{true}}

%%
% `symmetric' option -- puts marginpar space to the outside edge of the page
%   Note: this option forces the twoside option (see the .cls files)

\newboolean{@tufte@symmetric}
\DeclareOption{symmetric}{
  \setboolean{@tufte@symmetric}{true}
  \PackageInfo{\@tufte@pkgname}{The `symmetric' option implies `twoside'}
  %\ExecuteOptions{twoside}
}

%%
% `twoside' option -- alternates running heads

\newboolean{@tufte@twoside}
\DeclareOption{twoside}{
  \setboolean{@tufte@twoside}{true}
}

%%
% `notoc' option -- suppresses the Tufte-style table of contents

\newboolean{@tufte@toc}
\setboolean{@tufte@toc}{true}
\DeclareOption{notoc}{\setboolean{@tufte@toc}{false}}
\DeclareOption{toc}{\setboolean{@tufte@toc}{true}}

%%
% `justified' option -- uses fully justified text (flush left and flush
% right) instead of ragged right.

\newboolean{@tufte@justified}
\DeclareOption{justified}{\setboolean{@tufte@justified}{true}}


% FIXME: should probably specify options not supported like Mittelbach's aipproc.cls

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{\@tufte@class}}
\ProcessOptions

%%
% Detect whether we're in two-side mode or not.  (Used to set up running
% heads later.)

\ifthenelse{\boolean{@twoside}}
  {\setboolean{@tufte@twoside}{true}}
  {}



%%
% Detect if we're using pdfLaTeX

\newboolean{@tufte@pdf}
\IfFileExists{ifpdf.sty}{%
  \RequirePackage{ifpdf}
  \ifthenelse{\boolean{pdf}}
    {\setboolean{@tufte@pdf}{true}}
    {\setboolean{@tufte@pdf}{false}}
}{% assume we're not using pdfTex?
  \setboolean{@tufte@pdf}{false}
}

%%
% Detect if we're using XeLaTeX

\newboolean{@tufte@xetex}
\IfFileExists{ifxetex.sty}{%
  \RequirePackage{ifxetex}
  \ifthenelse{\boolean{xetex}}
    {\setboolean{@tufte@xetex}{true}}
    {\setboolean{@tufte@xetex}{false}}
}{% not using xelatex
  \setboolean{@tufte@xetex}{false}
}

%%
% Load the `hyperref' package.  We will set more options later.
% TODO Set nice defaults for hyperref options
\ifthenelse{\boolean{@tufte@xetex}}
  {\RequirePackage[hyperfootnotes=false,xetex]{hyperref}}
  {\RequirePackage[hyperfootnotes=false]{hyperref}}

\hypersetup{%
  pdfborder = {0 0 0},
}

%%
% Set page layout geometry

\RequirePackage[letterpaper,includemp,width=6.5in,marginparsep=0.375in,marginparwidth=2in]{geometry}

\ifthenelse{\boolean{@tufte@afourpaper}}
  {\geometry{a4paper,includemp,width=170mm,marginparsep=10mm,marginparwidth=50mm}}
  {}

\ifthenelse{\boolean{@tufte@symmetric}}
  {}
  {\geometry{asymmetric}}% forces internal LaTeX `twoside'


%%
% Separation marginpars by a line's worth of space.

\setlength\marginparpush{\baselineskip}

%%
% Font for margin items

\ifthenelse{\boolean{@tufte@sfsidenotes}}
  {\newcommand{\@tufte@marginfont}{\normalfont\scriptsize\sffamily}}
  {\newcommand{\@tufte@marginfont}{\normalfont\scriptsize}}

%%
% \RaggedRight allows hyphenation

\RequirePackage{ragged2e}
\setlength{\RaggedRightRightskip}{\z@ plus 0.08\hsize}

%%
% Set the justification baesed on the `justified' class option

\newcommand{\@tufte@justification}{%
  \ifthenelse{\boolean{@tufte@justified}}
    {\justifying}
    {\RaggedRight}
}

%%
% Turn off section numbering

\setcounter{secnumdepth}{-1}

%%
% Tighten up space between displays (e.g., a figure or table) and make symmetric

\setlength\abovedisplayskip{6pt plus 2pt minus 4pt}
\setlength\belowdisplayskip{6pt plus 2pt minus 4pt}

%%
% To implement full-width display environments

\RequirePackage[strict]{chngpage}
% TODO this package may be obsolete -- see the changepage package for a replacement



% Compute length used for full-width displays

\newlength{\@tufte@overhang}
\setlength{\@tufte@overhang}{\marginparwidth}
\addtolength{\@tufte@overhang}{\marginparsep}

%%
% Modified \title, \author, and \date commands.  These store the
% (footnote-less) values in \thetitle, \theauthor, and \thedate, respectively.

\newcommand{\thetitle}{}%     plain-text-only title
\newcommand{\theauthor}{}%    plain-text-only author
\newcommand{\thepublisher}{}% plain-text-only publisher

\newcommand{\thanklesstitle}{}%     full title text minus \thanks{}
\newcommand{\thanklessauthor}{}%    full author text minus \thanks{}
\newcommand{\thanklesspublisher}{}% full publisher minus \thanks{}

\newcommand{\@publisher}{}% full publisher with \thanks{}
\newcommand{\thedate}{\today}

% TODO Fix it so that \thanks is not spaced out (with `soul') and can be
% used in \maketitle when the `sfsidenotes' option is provided.
\renewcommand{\thanks}[1]{\NoCaseChange{\footnote{#1}}}

\renewcommand{\title}[2][]{%
  \gdef\@title{#2}%
  \begingroup%
    % TODO store contents of \thanks command
    \renewcommand{\thanks}[1]{}% swallow \thanks contents
    \protected@xdef\thanklesstitle{#2}%
  \endgroup%
  \ifthenelse{\isempty{#1}}%
    {\renewcommand{\thetitle}{\thanklesstitle}}% use thankless title
    {\renewcommand{\thetitle}{#1}}% use provided plain-text title
  \hypersetup{pdftitle={\thetitle}}% set the PDF metadata title
}

\def\@author{}% default author is empty (suppresses LaTeX's ``no author'' warning)
\renewcommand*{\author}[2][]{%
  \gdef\@author{#2}%
  \begingroup%
    % TODO store contents of \thanks command
    \renewcommand{\thanks}[1]{}% swallow \thanks contents
    \protected@xdef\thanklessauthor{#2}%
  \endgroup%
  \ifthenelse{\isempty{#1}}
    {\renewcommand{\theauthor}{\thanklessauthor}}% use thankless author
    {\renewcommand{\theauthor}{#1}}% use provided plain-text author
  \hypersetup{pdfauthor={\theauthor}}% set the PDF metadata author
}

\renewcommand*{\date}[1]{%
  \gdef\@date{#1}%
  \begingroup%
    % TODO store contents of \thanks command
    \renewcommand{\thanks}[1]{}% swallow \thanks contents
    \protected@xdef\thedate{#1}%
  \endgroup%
}

%%
% Provides a \publisher command to set the publisher

\newcommand{\publisher}[2][]{%
  \gdef\@publisher{#2}%
  \begingroup%
    \renewcommand{\thanks}[1]{}% swallow \thanks contents
    \protected@xdef\thanklesspublisher{#2}%
  \endgroup%
  \ifthenelse{\isempty{#1}}
    {\renewcommand{\thepublisher}{\thanklesspublisher}}% use thankless publisher
    {\renewcommand{\thepublisher}{#1}}% use provided plain-text publisher
}

% TODO: Test \hypersetup{pdfauthor,pdftitle} with DVI and XeTeX

%%
% Require paralist package for tighter lists

\RequirePackage{paralist}

% Add rightmargin to compactenum

\def\@compactenum@{%
  \expandafter\list\csname label\@enumctr\endcsname{%
    \usecounter{\@enumctr}%
    \rightmargin=2em% added this
    \parsep\plparsep
    \itemsep\plitemsep
    \topsep\pltopsep
    \partopsep\plpartopsep
    \def\makelabel##1{\hss\llap{##1}}}}

%%
% Improved letterspacing of small caps and all-caps text.
%
% First, try to use the `microtype' package, if it's available. 
% Failing that, try to use the `soul' package, if it's available.
% Failing that, well, I give up.

\RequirePackage{textcase} % provides \MakeTextUppercase and \MakeTextLowercase
\def\allcapsspacing{\relax}
\def\smallcapsspacing{\relax}
\newcommand{\allcaps}[1]{\MakeTextUppercase{\allcapsspacing{#1}}}
\newcommand{\smallcaps}[1]{\MakeTextLowercase{\textsc{#1}}}

\newcommand{\@tufte@loadsoul}{%
  \IfFileExists{soul.sty}{%
    \RequirePackage{soul}
    \sodef\allcapsspacing{\upshape}{0.15em}{0.65em}{0.6em}
    \sodef\smallcapsspacing{\scshape}{0.075em}{0.5em}{0.6em}
  }{
    \PackageWarningNoLine{\@tufte@pkgname}{Couldn't locate `soul' package.}
  }% soul not installed... giving up.
}

% If we're using pdfLaTeX v1.40+, use the letterspace package. 
% If we're using pdfLaTex < v1.40, use the soul package.
% If we're using XeLaTeX, use XeLaTeX letterspacing options.
% Otherwise fall back on the soul package.

\ifthenelse{\boolean{@tufte@pdf}}
  {\PackageInfo{\@tufte@pkgname}{ifpdf = true}}
  {\PackageInfo{\@tufte@pkgname}{ifpdf = false}}

\ifthenelse{\boolean{@tufte@xetex}}
  {\PackageInfo{\@tufte@pkgname}{ifxetex = true}}
  {\PackageInfo{\@tufte@pkgname}{ifxetex = false}}

% Check pdfLaTeX version
\def\@tufte@pdftexversion{0}
\ifx\normalpdftexversion\@undefined \else
  \let\pdftexversion \normalpdftexversion
  \let\pdftexrevision\normalpdftexrevision
  \let\pdfoutput     \normalpdfoutput
\fi
\ifx\pdftexversion\@undefined \else
  \ifx\pdftexversion\relax \else
    \def\@tufte@pdftexversion{6}
    \ifnum\pdftexversion < 140
      \def\@tufte@pdftexversion{5}
    \fi
  \fi
\fi

\ifthenelse{\boolean{@tufte@letterspace}}
  {%
  \ifnum\@tufte@pdftexversion<6
    % pdfLaTeX version is too old or not using pdfLaTeX
    \ifthenelse{\boolean{@tufte@xetex}}
      {% TODO use xetex letterspacing
      \PackageInfo{\@tufte@pkgname}{XeTeX detected. Reverting to `soul' package for letterspacing.}
      \@tufte@loadsoul}
      {% use `soul' package for letterspacing
      \PackageInfo{\@tufte@pkgname}{Old version of pdfTeX detected.  Reverting to `soul' package for letterspacing.}
      \@tufte@loadsoul}
  \else
    \IfFileExists{letterspace.sty}{% 
      \PackageInfo{\@tufte@pkgname}{Modern version of pdfTeX detected.  Using `letterspace' package.}
      \RequirePackage{letterspace}
      % Set up letterspacing (using microtype package) -- requires pdfTeX v1.40+
      \renewcommand{\allcapsspacing}[1]{\textls[200]{##1}}
      \renewcommand{\smallcapsspacing}[1]{\textls[50]{##1}}
      \renewcommand{\allcaps}[1]{\textls[200]{\MakeTextUppercase{##1}}}
      \renewcommand{\smallcaps}[1]{\textsc{\MakeTextLowercase{##1}}}
    }{% microtype failed, check for soul
      \PackageInfo{\@tufte@pkgname}{Modern version of pdfTeX detected, but `letterspace' package not installed.  Reverting to  `soul' package for letterspacing.}
      \@tufte@loadsoul
    }
  \fi}
  {}


\DeclareTextFontCommand{\textsmallcaps}{\scshape}
\renewcommand{\textsc}[1]{\textsmallcaps{\smallcapsspacing{#1}}}


%%
% An environment for paragraph-style section

\providecommand\newthought[1]{%
   \addvspace{1.0\baselineskip plus 0.5ex minus 0.2ex}%
   \noindent\textsc{#1}%
}

%%
% Used for doublespacing, and other linespacing
% Note that setspace must be loaded before footmisc or it'll break sidenotes
\usepackage{setspace}


%%
% Citations should go in the margin as sidenotes

\RequirePackage{natbib}
\RequirePackage{bibentry}	% allows bibitems to be typeset outside thebibliography environment
\nobibliography*		% pre-loads the bibliography keys
\providecommand{\doi}[1]{\textsc{doi:} #1} % pre-defining this so it may be used before the \bibliography command it issued

%%
% Normal \cite behavior
\newcommand{\@tufte@normal@cite}[1]{%
  \@for\@temp@bibkey:=#1\do{%
    \sidenote{\bibentry{\@temp@bibkey}.}%
  }%
}

%%
% Macros for holding the list of cite keys until after the \sidenote

\gdef\@tufte@citations{}% list of cite keys
\newcommand\@tufte@add@citation[1]{\relax% adds a new bibkey to the list of cite keys
  \ifx\@tufte@citations\@empty\else
    \g@addto@macro\@tufte@citations{,}% separate by commas
  \fi
  \g@addto@macro\@tufte@citations{#1}
}

\newcommand{\@tufte@print@citations}{% puts each citation in its own margin note
  \@for\@temp@bibkey:=\@tufte@citations\do{%
    \marginpar{\@tufte@marginfont\@tufte@justification\bibentry{\@temp@bibkey}.}%
  }
}

%%
% \cite behavior when executed within a sidenote

\newcommand{\@tufte@sidenote@citations}{}% contains list of \cites in sidenote
\newcommand{\@tufte@infootnote@cite}[1]{%
  \@tufte@add@citation{#1}
}

%%
% Set the default \cite style.  This is set and reset by the \sidenote command.

\let\cite\@tufte@normal@cite

%%
% Transform existing \footnotes into \sidenotes
% Sidenote: ``Where God meant footnotes to go.'' ---Tufte

\RequirePackage{optparams}% for our new sidenote commands -- provides multiple optional arguments for commands

\providecommand*{\footnotelayout}{\@tufte@marginfont\@tufte@justification}
\renewcommand{\footnotelayout}{\@tufte@marginfont\@tufte@justification}

% Override footmisc's definition to set the sidenote marks (numbers) inside the
% sidenote's text block.
\long\def\@makefntext#1{\@textsuperscript{\@tufte@marginfont\tiny\@thefnmark}\,\footnotelayout#1}

% Set the in-text footnote mark in the same typeface as the body text itself.
\def\@makefnmark{\hbox{\@textsuperscript{\normalfont\scriptsize\@thefnmark}}}

\providecommand*{\multiplefootnotemarker}{3sp}
\providecommand*{\multfootsep}{,}

\renewcommand*\@footnotemark{%
  \leavevmode%
  \ifhmode%
    \edef\@x@sf{\the\spacefactor}%
    \@tufte@check@multiple@sidenotes%
    \nobreak%
  \fi%
  \@makefnmark%
  \ifhmode\spacefactor\@x@sf\fi%
  \relax%
}

\newcommand{\@tufte@check@multiple@sidenotes}{%
  \ifdim\lastkern=\multiplefootnotemarker\relax
    \edef\@x@sf{\the\spacefactor}%
    \unkern
    \textsuperscript{\multfootsep}%
    \spacefactor\@x@sf\relax
  \fi
}

\renewcommand\@footnotetext[2][0pt]{%
  \marginpar{%
    \vspace*{#1}%
    \def\baselinestretch {\setspace@singlespace}%
    \reset@font\footnotesize%
    \protected@edef\@currentlabel{%
       \csname p@footnote\endcsname\@thefnmark%
    }%
    \color@begingroup%
       \@makefntext{%
         \ignorespaces#2%
       }%
    \color@endgroup%
  }%
}%

%
% Define \sidenote command.  Can handle \cite.

\newcommand{\@tufte@sidenote@vertical@offset}{0pt}

% #1 = footnote num, #2 = vertical offset, #3 = footnote text
\def\@tufte@sidenote[#1][#2]#3{%
  \let\cite\@tufte@infootnote@cite%   use the in-sidenote \cite command
  \gdef\@tufte@citations{}%           clear out any old citations
  \ifthenelse{\NOT\isempty{#2}}{\renewcommand{\@tufte@sidenote@vertical@offset}{#2}}{}%
  \ifthenelse{\isempty{#1}}{%
%   % no footnote number
    \stepcounter\@mpfn%
    \protected@xdef\@thefnmark{\thempfn}%
    \@footnotemark\@footnotetext[\@tufte@sidenote@vertical@offset]{#3}%
  }{%
%   % footnote number
    \begingroup%
      \csname c@\@mpfn\endcsname #1\relax%
      \unrestored@protected@xdef\@thefnmark{\thempfn}%
    \endgroup%
    \@footnotemark\@footnotetext[\@tufte@sidenote@vertical@offset]{#3}%
  }%
  \@tufte@print@citations%            print any citations
  \let\cite\@tufte@normal@cite%       go back to using normal in-text \cite command
  \unskip\ignorespaces%               remove extra white space
  \kern-\multiplefootnotemarker%      remove \kern left behind by sidenote
  \kern\multiplefootnotemarker\relax% add new \kern here to replace the one we yanked
}

\newcommand*{\sidenote}{\optparams{\@tufte@sidenote}{[][0pt]}}
\renewcommand*{\footnote}{\optparams{\@tufte@sidenote}{[][0pt]}}

%%
% Sidenote without the footnote mark

\providecommand\marginnote[1]{%
  \let\cite\@tufte@infootnote@cite%   use the in-sidenote \cite command
  \gdef\@tufte@citations{}%           clear out any old citations
  \marginpar{\@tufte@marginfont\@tufte@justification #1}%
  \@tufte@print@citations%            print any citations
  \let\cite\@tufte@normal@cite%       go back to using normal in-text \cite command
}


% TODO: Combine sequences of citations so that 2,3,4,6 becomes 2--4,6
%       but be careful of hyperref interaction

%%
% The placeins package provides the \FloatBarrier command.  This forces
% LaTeX to place all of the floats before proceeding.  We'll use this to
% keep the float (figure and table) numbers in sequence.
\RequirePackage{placeins}

%%
% Margin figure environment

\newsavebox{\@tufte@marginfigbox}
\newenvironment{marginfigure}[1]
    [-1.2ex]
    {\FloatBarrier% process all floats before this point so the figure numbers stay in order.
    \begin{lrbox}{\@tufte@marginfigbox}
    \begin{minipage}{\marginparwidth}
        \@tufte@marginfont
        \def\@captype{figure}
        \vspace*{#1}
        \@tufte@justification
    }    
    {\end{minipage}%
     \end{lrbox}%
     \marginpar{\usebox{\@tufte@marginfigbox}}}


%%
% Margin table environment
\newsavebox{\@tufte@margintablebox}
\newenvironment{margintable}[1]
   [-1.2ex]
   {\FloatBarrier% process all floats before this point so the figure numbers stay in order.
   \begin{lrbox}{\@tufte@margintablebox}
   \begin{minipage}{\marginparwidth}
       \@tufte@marginfont
       \def\@captype{table}
       \vspace*{#1}
       \@tufte@justification
   }
   {\end{minipage}%
    \end{lrbox}%
    \marginpar{\usebox{\@tufte@margintablebox}}}


%%
% Full-width figure

\renewenvironment{figure*}[1]%
  [htbp]%
  {\@float{figure}[#1]%
   \ifthenelse{\boolean{@tufte@symmetric}}
     {\begin{adjustwidth}[]{}{-\@tufte@overhang}}
     {\begin{adjustwidth}{}{-\@tufte@overhang}}
   \begin{minipage}{\linewidth}}%
  {\end{minipage}%
   \end{adjustwidth}%
   \end@float}

%%
% Full-width table

\renewenvironment{table*}[1]
  [htbp]%
  {\@float{table}[#1]%
   \ifthenelse{\boolean{@tufte@symmetric}}
     {\begin{adjustwidth}[]{}{-\@tufte@overhang}}
     {\begin{adjustwidth}{}{-\@tufte@overhang}}
   \begin{minipage}{\linewidth}}%
  {\end{minipage}%
   \end{adjustwidth}%
   \end@float}

%%
% Full-page-width area

\newenvironment{fullwidth}
  {\ifthenelse{\boolean{@tufte@symmetric}}
     {\begin{adjustwidth}[]{}{-\@tufte@overhang}}
     {\begin{adjustwidth}{}{-\@tufte@overhang}}
  }
  {\end{adjustwidth}}

%%
% Format the captions in a style similar to the sidenotes

\RequirePackage[format=default,font={rm,scriptsize},justification=raggedright,singlelinecheck=false]{caption}

% if the `sfsidenotes' option is specified, set the captions in sf, too.
\ifthenelse{\boolean{@tufte@sfsidenotes}}
  {\captionsetup{font={sf,scriptsize}}}
  {\captionsetup{font={rm,scriptsize}}}

% if the `justified' option is specified, set the captions in flush left
% and flush right
\ifthenelse{\boolean{@tufte@justified}}
  {\captionsetup{justification=justified}}
  {\captionsetup{justification=raggedright}}

%%
% If the Palatino typeface (and its math symbol set) are installed, load
% them unless the `nofonts' class option was provided, or if we're
% compiling with XeLaTeX.

\ifthenelse{\boolean{@tufte@loadfonts}\AND\NOT\boolean{@tufte@xetex}}{%
  \IfFileExists{palatino.sty}{%
    \RequirePackage{palatino}
    \IfFileExists{mathpazo.sty}{\RequirePackage[osf,sc]{mathpazo}}{}
  }{} % if the Palatino typefaces aren't found, do nothing.
}{}


%%
% If the Bera Mono typeface is available, use it unless the `nofonts' class
% option was provided, or if we're compiling with XeLaTeX.

\ifthenelse{\boolean{@tufte@loadfonts}\AND\NOT\boolean{@tufte@xetex}}{%
  \IfFileExists{beramono.sty}{%
    \RequirePackage[T1]{fontenc}
    \RequirePackage[scaled=0.85]{beramono}
  }{}
}{}


%%
% Turns newlines into spaces.  Based on code from the `titlesec' package.

\DeclareRobustCommand{\@tufte@newlinetospace}{%
  \@ifstar{\@tufte@newlinetospace@i}{\@tufte@newlinetospace@i}%
}

\def\@tufte@newlinetospace@i{%
  \ifdim\lastskip>\z@\else\space\fi
  \ignorespaces%
}

\DeclareRobustCommand{\newlinetospace}[1]{%
  \let\@tufte@orig@cr\\% save the original meaning of \\
  \def\\{\@tufte@newlinetospace}% turn \\ and \\* into \space
  \let\newline\\% turn \newline into \space
  #1
  \let\\\@tufte@orig@cr% revert to original meaning of \\
}


%%
% Sets up the running heads and folios.

\RequirePackage{fancyhdr}

% Set the default page style to 'fancy'
\pagestyle{fancy}

% Set the header/footer width to be the body text block plus the margin
% note area.
\ifthenelse{\boolean{@tufte@symmetric}}
  {\fancyhfoffset[LE,RO]{\@tufte@overhang}}
  {\fancyhfoffset[RE,RO]{\@tufte@overhang}}

% The running heads/feet don't have rules
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

% The 'fancy' page style is the default style for all pages.
\fancyhf{} % clear header and footer fields
\ifthenelse{\boolean{@tufte@twoside}}
  {\fancyhead[LE]{\thepage\quad\smallcaps{\newlinetospace{\theauthor}}}%
    \fancyhead[RO]{\smallcaps{\newlinetospace{\thetitle}}\quad\thepage}}
  {\fancyhead[RE,RO]{\smallcaps{\newlinetospace{\thetitle}}\quad\thepage}}


% The `plain' page style is used on chapter opening pages.
% In Tufte's /Beautiful Evidence/ he never puts page numbers at the
% bottom of pages -- the folios are unexpressed.
\fancypagestyle{plain}{
  \fancyhf{} % clear header and footer fields
  % Uncomment the following five lines of code if you want the opening page
  % of the chapter to express the folio in the lower outside corner.
  %\ifthenelse{\boolean{@tufte@twoside}}
  %  {\fancyfoot[LE,RO]{\thepage}}
  %  {\fancyfoot[RE,RO]{\thepage}}
}

% The `empty' page style suppresses all headers and footers.
% It's used on title pages and `intentionally blank' pages.
\fancypagestyle{empty}{
  \fancyhf{} % clear header and footer fields
}




%%
% Set raggedright and paragraph indentation for document

\AtBeginDocument{\@tufte@justification\setlength\parindent{1em}}


%%
% Prints the list of class options and their states.

\newcommand{\typeoutbool}[2]{%
  \ifthenelse{\boolean{#2}}
    {\typeout{\space\space#1: true}}
    {\typeout{\space\space#1: false}}
}

\newcommand{\typeoutstr}[2]{%
  \typeout{\space\space#1: #2}
}

\newcommand{\PrintTufteSettings}{%
  \typeout{-------------------- Tufte-LaTeX settings ----------}
  \typeout{Class: \@tufte@pkgname}
  \typeout{}
  \typeout{Class options:}
  \typeoutbool{a4paper}{@tufte@afourpaper}
  \typeoutbool{load fonts}{@tufte@loadfonts}
  \typeoutbool{fully-justified}{@tufte@justified}
  \typeoutbool{letterspacing}{@tufte@letterspace}
  \typeoutbool{sans-serif sidenotes}{@tufte@sfsidenotes}
  \typeoutbool{symmetric margins}{@tufte@symmetric}
  \typeoutbool{titlepage}{@tufte@titlepage}
  \typeoutbool{twoside}{@tufte@twoside}
  \typeoutbool{debug}{@tufte@debug}
  \typeout{}
  \typeout{Internal variables:}
  \typeoutbool{[twoside]}{@twoside}
  \typeoutbool{pdflatex}{@tufte@pdf}
  \typeoutbool{xelatex}{@tufte@xetex}
  \typeout{----------------------------------------------------}
}



%%
% Color
\RequirePackage{xcolor}

%%
% Produces a full title page

\newcommand{\maketitlepage}[0]{%
  \cleardoublepage
  {
  \sffamily
  \begin{fullwidth}
  \par\noindent\fontsize{18}{18}\selectfont\textcolor{darkgray}{\allcaps{\thanklessauthor}}
  \end{fullwidth}

  \vspace{11.5pc}
  \begin{fullwidth}
  \par\noindent\fontsize{36}{36}\selectfont\textcolor{darkgray}{\allcaps{\thanklesstitle}}
  \end{fullwidth}

  \vfill
  \begin{fullwidth}
  \par\noindent\fontsize{14}{14}\selectfont\allcaps{\thanklesspublisher}
  \end{fullwidth}
  }
  \thispagestyle{empty}
  \clearpage
}

%%
% Title block

\renewcommand{\maketitle}{%
  \newpage
  \global\@topnum\z@% prevent floats from being placed at the top of the page
  \setlength{\parindent}{0pt}
  \setlength{\parskip}{4pt}
  \ifthenelse{\boolean{@tufte@sfsidenotes}}
    {\begingroup
      % FIXME fails with \thanks
      \sffamily
      \par{\Large\allcaps{\@title}}
      \ifthenelse{\equal{\@author}{}}{}{\par{\large\allcaps{\@author}}}
      \ifthenelse{\equal{\@date}{}}{}{\par{\large\allcaps{\@date}}}
    \endgroup}
    {\begingroup
      \par{\Large\textit{\@title}}
      \ifthenelse{\equal{\@author}{}}{}{\par{\large\textit{\@author}}}
      \ifthenelse{\equal{\@date}{}}{}{\par{\large\textit{\@date}}}
    \endgroup}
  \par
  \thispagestyle{plain}% suppress the running head
}
<commit_msg>* We now check for the ``changepage'' package first and use it if it's   available.  Otherwise we call back on the ``chngpage'' package. * Changed \usepackage{setspace} to \RequirePackage{setspace} * Added an optional vertical offset argument to \marginnote * Fonts: We no longer use the deprecated ``palatino'' package -- only the   ``mathpazo'' package. * Fonts: We now automatically load the ``helvet'' package for a sans serif   font, if it's available. * Bug: Fixed a bug in the \maketitle command wherein it would globally reset   the \parindent and \parskip values.<commit_after>\NeedsTeXFormat{LaTeX2e}[1994/06/01]

\ProvidesPackage{tufte-common}[2008/11/16 v3.0.0 Common code for the Tufte-LaTeX styles]

%%
% We use the `xifthen' package to handle our package option switches
\RequirePackage{xifthen}


%%
% `debug' option -- provides more information in the .log file for use in
% troubleshooting problems
\newboolean{@tufte@debug}
\DeclareOption{debug}{\setboolean{@tufte@debug}{true}}

%%
% `nofonts' option -- doesn't load any fonts
% `fonts' option -- tries to load fonts
\newboolean{@tufte@loadfonts}\setboolean{@tufte@loadfonts}{true}
\DeclareOption{fonts}{\setboolean{@tufte@loadfonts}{true}}
\DeclareOption{nofonts}{\setboolean{@tufte@loadfonts}{false}}

%%
% `nols' option -- doesn't configure letterspacing
% `ls' option -- configures letterspacing
\newboolean{@tufte@letterspace}\setboolean{@tufte@letterspace}{true}
\DeclareOption{ls}{\setboolean{@tufte@letterspace}{true}}
\DeclareOption{nols}{\setboolean{@tufte@letterspace}{false}}

%%
% `book' and `handout' options
\newcommand{\@tufte@class}{article}% the base LaTeX class (defaults to the article/handout style)
\newcommand{\@tufte@pkgname}{tufte-handout}% the name of the package (defaults to tufte-handout)
\DeclareOption{book}{%
  \renewcommand{\@tufte@class}{book}
  \renewcommand{\@tufte@pkgname}{tufte-book}
  \setboolean{@tufte@titlepage}{true}
}
\DeclareOption{handout}{%
  \renewcommand{\@tufte@class}{article}
  \renewcommand{\@tufte@pkgname}{tufte-handout}
  \setboolean{@tufte@titlepage}{false}
}
\DeclareOption{article}{% `article' is just an alias for `handout'
  \renewcommand{\@tufte@class}{article}
  \renewcommand{\@tufte@pkgname}{tufte-handout}
  \setboolean{@tufte@titlepage}{false}
}

%%
% `titlepage' option -- creates a full title page with \maketitle

\newboolean{@tufte@titlepage}
\DeclareOption{titlepage}{\setboolean{@tufte@titlepage}{true}}
\DeclareOption{notitlepage}{\setboolean{@tufte@titlepage}{false}}

%%
% `a4paper' option

\newboolean{@tufte@afourpaper}
\DeclareOption{a4paper}{\setboolean{@tufte@afourpaper}{true}}

%%
% `sfsidenotes' option -- typesets sidenotes in sans serif typeface

\newboolean{@tufte@sfsidenotes}
\DeclareOption{sfsidenotes}{\setboolean{@tufte@sfsidenotes}{true}}

%%
% `symmetric' option -- puts marginpar space to the outside edge of the page
%   Note: this option forces the twoside option (see the .cls files)

\newboolean{@tufte@symmetric}
\DeclareOption{symmetric}{
  \setboolean{@tufte@symmetric}{true}
  \PackageInfo{\@tufte@pkgname}{The `symmetric' option implies `twoside'}
  %\ExecuteOptions{twoside}
}

%%
% `twoside' option -- alternates running heads

\newboolean{@tufte@twoside}
\DeclareOption{twoside}{
  \setboolean{@tufte@twoside}{true}
}

%%
% `notoc' option -- suppresses the Tufte-style table of contents

\newboolean{@tufte@toc}
\setboolean{@tufte@toc}{true}
\DeclareOption{notoc}{\setboolean{@tufte@toc}{false}}
\DeclareOption{toc}{\setboolean{@tufte@toc}{true}}

%%
% `justified' option -- uses fully justified text (flush left and flush
% right) instead of ragged right.

\newboolean{@tufte@justified}
\DeclareOption{justified}{\setboolean{@tufte@justified}{true}}


% FIXME: should probably specify options not supported like Mittelbach's aipproc.cls

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{\@tufte@class}}
\ProcessOptions

%%
% Detect whether we're in two-side mode or not.  (Used to set up running
% heads later.)

\ifthenelse{\boolean{@twoside}}
  {\setboolean{@tufte@twoside}{true}}
  {}



%%
% Detect if we're using pdfLaTeX

\newboolean{@tufte@pdf}
\IfFileExists{ifpdf.sty}{%
  \RequirePackage{ifpdf}
  \ifthenelse{\boolean{pdf}}
    {\setboolean{@tufte@pdf}{true}}
    {\setboolean{@tufte@pdf}{false}}
}{% assume we're not using pdfTex?
  \setboolean{@tufte@pdf}{false}
}

%%
% Detect if we're using XeLaTeX

\newboolean{@tufte@xetex}
\IfFileExists{ifxetex.sty}{%
  \RequirePackage{ifxetex}
  \ifthenelse{\boolean{xetex}}
    {\setboolean{@tufte@xetex}{true}}
    {\setboolean{@tufte@xetex}{false}}
}{% not using xelatex
  \setboolean{@tufte@xetex}{false}
}

%%
% Load the `hyperref' package.  We will set more options later.
% TODO Set nice defaults for hyperref options
\ifthenelse{\boolean{@tufte@xetex}}
  {\RequirePackage[hyperfootnotes=false,xetex]{hyperref}}
  {\RequirePackage[hyperfootnotes=false]{hyperref}}

\hypersetup{%
  pdfborder = {0 0 0},
}

%%
% Set page layout geometry

\RequirePackage[letterpaper,includemp,width=6.5in,marginparsep=0.375in,marginparwidth=2in]{geometry}

\ifthenelse{\boolean{@tufte@afourpaper}}
  {\geometry{a4paper,includemp,width=170mm,marginparsep=10mm,marginparwidth=50mm}}
  {}

\ifthenelse{\boolean{@tufte@symmetric}}
  {}
  {\geometry{asymmetric}}% forces internal LaTeX `twoside'


%%
% Separation marginpars by a line's worth of space.

\setlength\marginparpush{\baselineskip}

%%
% Font for margin items

\ifthenelse{\boolean{@tufte@sfsidenotes}}
  {\newcommand{\@tufte@marginfont}{\normalfont\scriptsize\sffamily}}
  {\newcommand{\@tufte@marginfont}{\normalfont\scriptsize}}

%%
% \RaggedRight allows hyphenation

\RequirePackage{ragged2e}
\setlength{\RaggedRightRightskip}{\z@ plus 0.08\hsize}

%%
% Set the justification baesed on the `justified' class option

\newcommand{\@tufte@justification}{%
  \ifthenelse{\boolean{@tufte@justified}}
    {\justifying}
    {\RaggedRight}
}

%%
% Turn off section numbering

\setcounter{secnumdepth}{-1}

%%
% Tighten up space between displays (e.g., a figure or table) and make symmetric

\setlength\abovedisplayskip{6pt plus 2pt minus 4pt}
\setlength\belowdisplayskip{6pt plus 2pt minus 4pt}

%%
% To implement full-width display environments

\newboolean{@tufte@changepage}
\IfFileExists{changepage.sty}{%
  \RequirePackage[strict]{changepage}
  \setboolean{@tufte@changepage}{true}
}{%
  \RequirePackage[strict]{chngpage}
  \setboolean{@tufte@changepage}{false}
}



% Compute length used for full-width displays

\newlength{\@tufte@overhang}
\setlength{\@tufte@overhang}{\marginparwidth}
\addtolength{\@tufte@overhang}{\marginparsep}

%%
% Modified \title, \author, and \date commands.  These store the
% (footnote-less) values in \thetitle, \theauthor, and \thedate, respectively.

\newcommand{\thetitle}{}%     plain-text-only title
\newcommand{\theauthor}{}%    plain-text-only author
\newcommand{\thepublisher}{}% plain-text-only publisher

\newcommand{\thanklesstitle}{}%     full title text minus \thanks{}
\newcommand{\thanklessauthor}{}%    full author text minus \thanks{}
\newcommand{\thanklesspublisher}{}% full publisher minus \thanks{}

\newcommand{\@publisher}{}% full publisher with \thanks{}
\newcommand{\thedate}{\today}

% TODO Fix it so that \thanks is not spaced out (with `soul') and can be
% used in \maketitle when the `sfsidenotes' option is provided.
\renewcommand{\thanks}[1]{\NoCaseChange{\footnote{#1}}}

\renewcommand{\title}[2][]{%
  \gdef\@title{#2}%
  \begingroup%
    % TODO store contents of \thanks command
    \renewcommand{\thanks}[1]{}% swallow \thanks contents
    \protected@xdef\thanklesstitle{#2}%
  \endgroup%
  \ifthenelse{\isempty{#1}}%
    {\renewcommand{\thetitle}{\thanklesstitle}}% use thankless title
    {\renewcommand{\thetitle}{#1}}% use provided plain-text title
  \hypersetup{pdftitle={\thetitle}}% set the PDF metadata title
}

\def\@author{}% default author is empty (suppresses LaTeX's ``no author'' warning)
\renewcommand*{\author}[2][]{%
  \gdef\@author{#2}%
  \begingroup%
    % TODO store contents of \thanks command
    \renewcommand{\thanks}[1]{}% swallow \thanks contents
    \protected@xdef\thanklessauthor{#2}%
  \endgroup%
  \ifthenelse{\isempty{#1}}
    {\renewcommand{\theauthor}{\thanklessauthor}}% use thankless author
    {\renewcommand{\theauthor}{#1}}% use provided plain-text author
  \hypersetup{pdfauthor={\theauthor}}% set the PDF metadata author
}

\renewcommand*{\date}[1]{%
  \gdef\@date{#1}%
  \begingroup%
    % TODO store contents of \thanks command
    \renewcommand{\thanks}[1]{}% swallow \thanks contents
    \protected@xdef\thedate{#1}%
  \endgroup%
}

%%
% Provides a \publisher command to set the publisher

\newcommand{\publisher}[2][]{%
  \gdef\@publisher{#2}%
  \begingroup%
    \renewcommand{\thanks}[1]{}% swallow \thanks contents
    \protected@xdef\thanklesspublisher{#2}%
  \endgroup%
  \ifthenelse{\isempty{#1}}
    {\renewcommand{\thepublisher}{\thanklesspublisher}}% use thankless publisher
    {\renewcommand{\thepublisher}{#1}}% use provided plain-text publisher
}

% TODO: Test \hypersetup{pdfauthor,pdftitle} with DVI and XeTeX

%%
% Require paralist package for tighter lists

\RequirePackage{paralist}

% Add rightmargin to compactenum

\def\@compactenum@{%
  \expandafter\list\csname label\@enumctr\endcsname{%
    \usecounter{\@enumctr}%
    \rightmargin=2em% added this
    \parsep\plparsep
    \itemsep\plitemsep
    \topsep\pltopsep
    \partopsep\plpartopsep
    \def\makelabel##1{\hss\llap{##1}}}}

%%
% Improved letterspacing of small caps and all-caps text.
%
% First, try to use the `microtype' package, if it's available. 
% Failing that, try to use the `soul' package, if it's available.
% Failing that, well, I give up.

\RequirePackage{textcase} % provides \MakeTextUppercase and \MakeTextLowercase
\def\allcapsspacing{\relax}
\def\smallcapsspacing{\relax}
\newcommand{\allcaps}[1]{\MakeTextUppercase{\allcapsspacing{#1}}}
\newcommand{\smallcaps}[1]{\MakeTextLowercase{\textsc{#1}}}

\newcommand{\@tufte@loadsoul}{%
  \IfFileExists{soul.sty}{%
    \RequirePackage{soul}
    \sodef\allcapsspacing{\upshape}{0.15em}{0.65em}{0.6em}
    \sodef\smallcapsspacing{\scshape}{0.075em}{0.5em}{0.6em}
  }{
    \PackageWarningNoLine{\@tufte@pkgname}{Couldn't locate `soul' package.}
  }% soul not installed... giving up.
}

% If we're using pdfLaTeX v1.40+, use the letterspace package. 
% If we're using pdfLaTex < v1.40, use the soul package.
% If we're using XeLaTeX, use XeLaTeX letterspacing options.
% Otherwise fall back on the soul package.

\ifthenelse{\boolean{@tufte@pdf}}
  {\PackageInfo{\@tufte@pkgname}{ifpdf = true}}
  {\PackageInfo{\@tufte@pkgname}{ifpdf = false}}

\ifthenelse{\boolean{@tufte@xetex}}
  {\PackageInfo{\@tufte@pkgname}{ifxetex = true}}
  {\PackageInfo{\@tufte@pkgname}{ifxetex = false}}

% Check pdfLaTeX version
\def\@tufte@pdftexversion{0}
\ifx\normalpdftexversion\@undefined \else
  \let\pdftexversion \normalpdftexversion
  \let\pdftexrevision\normalpdftexrevision
  \let\pdfoutput     \normalpdfoutput
\fi
\ifx\pdftexversion\@undefined \else
  \ifx\pdftexversion\relax \else
    \def\@tufte@pdftexversion{6}
    \ifnum\pdftexversion < 140
      \def\@tufte@pdftexversion{5}
    \fi
  \fi
\fi

\ifthenelse{\boolean{@tufte@letterspace}}
  {%
  \ifnum\@tufte@pdftexversion<6
    % pdfLaTeX version is too old or not using pdfLaTeX
    \ifthenelse{\boolean{@tufte@xetex}}
      {% TODO use xetex letterspacing
      \PackageInfo{\@tufte@pkgname}{XeTeX detected. Reverting to `soul' package for letterspacing.}
      \@tufte@loadsoul}
      {% use `soul' package for letterspacing
      \PackageInfo{\@tufte@pkgname}{Old version of pdfTeX detected.  Reverting to `soul' package for letterspacing.}
      \@tufte@loadsoul}
  \else
    \IfFileExists{letterspace.sty}{% 
      \PackageInfo{\@tufte@pkgname}{Modern version of pdfTeX detected.  Using `letterspace' package.}
      \RequirePackage{letterspace}
      % Set up letterspacing (using microtype package) -- requires pdfTeX v1.40+
      \renewcommand{\allcapsspacing}[1]{\textls[200]{##1}}
      \renewcommand{\smallcapsspacing}[1]{\textls[50]{##1}}
      \renewcommand{\allcaps}[1]{\textls[200]{\MakeTextUppercase{##1}}}
      \renewcommand{\smallcaps}[1]{\textsc{\MakeTextLowercase{##1}}}
    }{% microtype failed, check for soul
      \PackageInfo{\@tufte@pkgname}{Modern version of pdfTeX detected, but `letterspace' package not installed.  Reverting to  `soul' package for letterspacing.}
      \@tufte@loadsoul
    }
  \fi}
  {}


\DeclareTextFontCommand{\textsmallcaps}{\scshape}
\renewcommand{\textsc}[1]{\textsmallcaps{\smallcapsspacing{#1}}}


%%
% An environment for paragraph-style section

\providecommand\newthought[1]{%
   \addvspace{1.0\baselineskip plus 0.5ex minus 0.2ex}%
   \noindent\textsc{#1}%
}

%%
% Used for doublespacing, and other linespacing

\RequirePackage{setspace}


%%
% Citations should go in the margin as sidenotes

\RequirePackage{natbib}
\RequirePackage{bibentry}	% allows bibitems to be typeset outside thebibliography environment
\nobibliography*		% pre-loads the bibliography keys
\providecommand{\doi}[1]{\textsc{doi:} #1} % pre-defining this so it may be used before the \bibliography command it issued

%%
% Normal \cite behavior
\newcommand{\@tufte@normal@cite}[1]{%
  \@for\@temp@bibkey:=#1\do{%
    \sidenote{\bibentry{\@temp@bibkey}.}%
  }%
}

%%
% Macros for holding the list of cite keys until after the \sidenote

\gdef\@tufte@citations{}% list of cite keys
\newcommand\@tufte@add@citation[1]{\relax% adds a new bibkey to the list of cite keys
  \ifx\@tufte@citations\@empty\else
    \g@addto@macro\@tufte@citations{,}% separate by commas
  \fi
  \g@addto@macro\@tufte@citations{#1}
}

\newcommand{\@tufte@print@citations}{% puts each citation in its own margin note
  \@for\@temp@bibkey:=\@tufte@citations\do{%
    \marginpar{\@tufte@marginfont\@tufte@justification\bibentry{\@temp@bibkey}.}%
  }
}

%%
% \cite behavior when executed within a sidenote

\newcommand{\@tufte@sidenote@citations}{}% contains list of \cites in sidenote
\newcommand{\@tufte@infootnote@cite}[1]{%
  \@tufte@add@citation{#1}
}

%%
% Set the default \cite style.  This is set and reset by the \sidenote command.

\let\cite\@tufte@normal@cite

%%
% Transform existing \footnotes into \sidenotes
% Sidenote: ``Where God meant footnotes to go.'' ---Tufte

\RequirePackage{optparams}% for our new sidenote commands -- provides multiple optional arguments for commands

\providecommand*{\footnotelayout}{\@tufte@marginfont\@tufte@justification}
\renewcommand{\footnotelayout}{\@tufte@marginfont\@tufte@justification}

% Override footmisc's definition to set the sidenote marks (numbers) inside the
% sidenote's text block.
\long\def\@makefntext#1{\@textsuperscript{\@tufte@marginfont\tiny\@thefnmark}\,\footnotelayout#1}

% Set the in-text footnote mark in the same typeface as the body text itself.
\def\@makefnmark{\hbox{\@textsuperscript{\normalfont\scriptsize\@thefnmark}}}

\providecommand*{\multiplefootnotemarker}{3sp}
\providecommand*{\multfootsep}{,}

\renewcommand*\@footnotemark{%
  \leavevmode%
  \ifhmode%
    \edef\@x@sf{\the\spacefactor}%
    \@tufte@check@multiple@sidenotes%
    \nobreak%
  \fi%
  \@makefnmark%
  \ifhmode\spacefactor\@x@sf\fi%
  \relax%
}

\newcommand{\@tufte@check@multiple@sidenotes}{%
  \ifdim\lastkern=\multiplefootnotemarker\relax
    \edef\@x@sf{\the\spacefactor}%
    \unkern
    \textsuperscript{\multfootsep}%
    \spacefactor\@x@sf\relax
  \fi
}

\renewcommand\@footnotetext[2][0pt]{%
  \marginpar{%
    \vspace*{#1}%
    \def\baselinestretch {\setspace@singlespace}%
    \reset@font\footnotesize%
    \protected@edef\@currentlabel{%
       \csname p@footnote\endcsname\@thefnmark%
    }%
    \color@begingroup%
       \@makefntext{%
         \ignorespaces#2%
       }%
    \color@endgroup%
  }%
}%

%
% Define \sidenote command.  Can handle \cite.

\newcommand{\@tufte@sidenote@vertical@offset}{0pt}

% #1 = footnote num, #2 = vertical offset, #3 = footnote text
\def\@tufte@sidenote[#1][#2]#3{%
  \let\cite\@tufte@infootnote@cite%   use the in-sidenote \cite command
  \gdef\@tufte@citations{}%           clear out any old citations
  \ifthenelse{\NOT\isempty{#2}}{\renewcommand{\@tufte@sidenote@vertical@offset}{#2}}{}%
  \ifthenelse{\isempty{#1}}{%
%   % no footnote number
    \stepcounter\@mpfn%
    \protected@xdef\@thefnmark{\thempfn}%
    \@footnotemark\@footnotetext[\@tufte@sidenote@vertical@offset]{#3}%
  }{%
%   % footnote number
    \begingroup%
      \csname c@\@mpfn\endcsname #1\relax%
      \unrestored@protected@xdef\@thefnmark{\thempfn}%
    \endgroup%
    \@footnotemark\@footnotetext[\@tufte@sidenote@vertical@offset]{#3}%
  }%
  \@tufte@print@citations%            print any citations
  \let\cite\@tufte@normal@cite%       go back to using normal in-text \cite command
  \unskip\ignorespaces%               remove extra white space
  \kern-\multiplefootnotemarker%      remove \kern left behind by sidenote
  \kern\multiplefootnotemarker\relax% add new \kern here to replace the one we yanked
}

\newcommand*{\sidenote}{\optparams{\@tufte@sidenote}{[][0pt]}}
\renewcommand*{\footnote}{\optparams{\@tufte@sidenote}{[][0pt]}}

%%
% Sidenote without the footnote mark

\providecommand\marginnote[2][0pt]{%
  \let\cite\@tufte@infootnote@cite%   use the in-sidenote \cite command
  \gdef\@tufte@citations{}%           clear out any old citations
  \marginpar{\vspace*{#1}\@tufte@marginfont\@tufte@justification #2}%
  \@tufte@print@citations%            print any citations
  \let\cite\@tufte@normal@cite%       go back to using normal in-text \cite command
}


% TODO: Combine sequences of citations so that 2,3,4,6 becomes 2--4,6
%       but be careful of hyperref interaction

%%
% The placeins package provides the \FloatBarrier command.  This forces
% LaTeX to place all of the floats before proceeding.  We'll use this to
% keep the float (figure and table) numbers in sequence.
\RequirePackage{placeins}

%%
% Margin figure environment

\newsavebox{\@tufte@marginfigbox}
\newenvironment{marginfigure}[1]
    [-1.2ex]
    {\FloatBarrier% process all floats before this point so the figure numbers stay in order.
    \begin{lrbox}{\@tufte@marginfigbox}
    \begin{minipage}{\marginparwidth}
        \@tufte@marginfont
        \def\@captype{figure}
        \vspace*{#1}
        \@tufte@justification
    }    
    {\end{minipage}%
     \end{lrbox}%
     \marginpar{\usebox{\@tufte@marginfigbox}}}


%%
% Margin table environment
\newsavebox{\@tufte@margintablebox}
\newenvironment{margintable}[1]
   [-1.2ex]
   {\FloatBarrier% process all floats before this point so the figure numbers stay in order.
   \begin{lrbox}{\@tufte@margintablebox}
   \begin{minipage}{\marginparwidth}
       \@tufte@marginfont
       \def\@captype{table}
       \vspace*{#1}
       \@tufte@justification
   }
   {\end{minipage}%
    \end{lrbox}%
    \marginpar{\usebox{\@tufte@margintablebox}}}


%%
% Full-width figure

\renewenvironment{figure*}[1]%
  [htbp]%
  {\@float{figure}[#1]%
   \ifthenelse{\boolean{@tufte@symmetric}}
     {\ifthenelse{\boolean{@tufte@changepage}}{\begin{adjustwidth*}{}{-\@tufte@overhang}}{\begin{adjustwidth}[]{}{-\@tufte@overhang}}}
     {\begin{adjustwidth}{}{-\@tufte@overhang}}
   \begin{minipage}{\linewidth}}%
  {\end{minipage}%
   \end{adjustwidth}%
   \end@float}

%%
% Full-width table

\renewenvironment{table*}[1]
  [htbp]%
  {\@float{table}[#1]%
   \ifthenelse{\boolean{@tufte@symmetric}}
     {\ifthenelse{\boolean{@tufte@changepage}}{\begin{adjustwidth*}{}{-\@tufte@overhang}}{\begin{adjustwidth}[]{}{-\@tufte@overhang}}}
     {\begin{adjustwidth}{}{-\@tufte@overhang}}
   \begin{minipage}{\linewidth}}%
  {\end{minipage}%
   \end{adjustwidth}%
   \end@float}

%%
% Full-page-width area

\newenvironment{fullwidth}
  {\ifthenelse{\boolean{@tufte@symmetric}}
     {\ifthenelse{\boolean{@tufte@changepage}}{\begin{adjustwidth*}{}{-\@tufte@overhang}}{\begin{adjustwidth}[]{}{-\@tufte@overhang}}}
     {\begin{adjustwidth}{}{-\@tufte@overhang}}
  }
  {\end{adjustwidth}}

%%
% Format the captions in a style similar to the sidenotes

\RequirePackage[format=default,font={rm,scriptsize},justification=raggedright,singlelinecheck=false]{caption}

% if the `sfsidenotes' option is specified, set the captions in sf, too.
\ifthenelse{\boolean{@tufte@sfsidenotes}}
  {\captionsetup{font={sf,scriptsize}}}
  {\captionsetup{font={rm,scriptsize}}}

% if the `justified' option is specified, set the captions in flush left
% and flush right
\ifthenelse{\boolean{@tufte@justified}}
  {\captionsetup{justification=justified}}
  {\captionsetup{justification=raggedright}}

%%
% If the Palatino typeface (and its math symbol set) are installed, load
% them unless the `nofonts' class option was provided, or if we're
% compiling with XeLaTeX.

\ifthenelse{\boolean{@tufte@loadfonts}\AND\NOT\boolean{@tufte@xetex}}{%
  \IfFileExists{mathpazo.sty}{%
    \RequirePackage[osf,sc]{mathpazo}
  }{} % if the Palatino typefaces aren't found, do nothing.
  \IfFileExists{helvet.sty}{%
    \RequirePackage[scaled=0.95]{helvet}
  }{}
}{}


%%
% If the Bera Mono typeface is available, use it unless the `nofonts' class
% option was provided, or if we're compiling with XeLaTeX.

\ifthenelse{\boolean{@tufte@loadfonts}\AND\NOT\boolean{@tufte@xetex}}{%
  \IfFileExists{beramono.sty}{%
    \RequirePackage[T1]{fontenc}
    \RequirePackage[scaled=0.85]{beramono}
  }{}
}{}


%%
% Turns newlines into spaces.  Based on code from the `titlesec' package.

\DeclareRobustCommand{\@tufte@newlinetospace}{%
  \@ifstar{\@tufte@newlinetospace@i}{\@tufte@newlinetospace@i}%
}

\def\@tufte@newlinetospace@i{%
  \ifdim\lastskip>\z@\else\space\fi
  \ignorespaces%
}

\DeclareRobustCommand{\newlinetospace}[1]{%
  \let\@tufte@orig@cr\\% save the original meaning of \\
  \def\\{\@tufte@newlinetospace}% turn \\ and \\* into \space
  \let\newline\\% turn \newline into \space
  #1
  \let\\\@tufte@orig@cr% revert to original meaning of \\
}


%%
% Sets up the running heads and folios.

\RequirePackage{fancyhdr}

% Set the default page style to 'fancy'
\pagestyle{fancy}

% Set the header/footer width to be the body text block plus the margin
% note area.
\ifthenelse{\boolean{@tufte@symmetric}}
  {\fancyhfoffset[LE,RO]{\@tufte@overhang}}
  {\fancyhfoffset[RE,RO]{\@tufte@overhang}}

% The running heads/feet don't have rules
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

% The 'fancy' page style is the default style for all pages.
\fancyhf{} % clear header and footer fields
\ifthenelse{\boolean{@tufte@twoside}}
  {\fancyhead[LE]{\thepage\quad\smallcaps{\newlinetospace{\theauthor}}}%
    \fancyhead[RO]{\smallcaps{\newlinetospace{\thetitle}}\quad\thepage}}
  {\fancyhead[RE,RO]{\smallcaps{\newlinetospace{\thetitle}}\quad\thepage}}


% The `plain' page style is used on chapter opening pages.
% In Tufte's /Beautiful Evidence/ he never puts page numbers at the
% bottom of pages -- the folios are unexpressed.
\fancypagestyle{plain}{
  \fancyhf{} % clear header and footer fields
  % Uncomment the following five lines of code if you want the opening page
  % of the chapter to express the folio in the lower outside corner.
  %\ifthenelse{\boolean{@tufte@twoside}}
  %  {\fancyfoot[LE,RO]{\thepage}}
  %  {\fancyfoot[RE,RO]{\thepage}}
}

% The `empty' page style suppresses all headers and footers.
% It's used on title pages and `intentionally blank' pages.
\fancypagestyle{empty}{
  \fancyhf{} % clear header and footer fields
}




%%
% Set raggedright and paragraph indentation for document

\AtBeginDocument{\@tufte@justification\setlength\parindent{1em}}


%%
% Prints the list of class options and their states.

\newcommand{\typeoutbool}[2]{%
  \ifthenelse{\boolean{#2}}
    {\typeout{\space\space#1: true}}
    {\typeout{\space\space#1: false}}
}

\newcommand{\typeoutstr}[2]{%
  \typeout{\space\space#1: #2}
}

\newcommand{\PrintTufteSettings}{%
  \typeout{-------------------- Tufte-LaTeX settings ----------}
  \typeout{Class: \@tufte@pkgname}
  \typeout{}
  \typeout{Class options:}
  \typeoutbool{a4paper}{@tufte@afourpaper}
  \typeoutbool{load fonts}{@tufte@loadfonts}
  \typeoutbool{fully-justified}{@tufte@justified}
  \typeoutbool{letterspacing}{@tufte@letterspace}
  \typeoutbool{sans-serif sidenotes}{@tufte@sfsidenotes}
  \typeoutbool{symmetric margins}{@tufte@symmetric}
  \typeoutbool{titlepage}{@tufte@titlepage}
  \typeoutbool{twoside}{@tufte@twoside}
  \typeoutbool{debug}{@tufte@debug}
  \typeout{}
  \typeout{Internal variables:}
  \typeoutbool{[twoside]}{@twoside}
  \typeoutbool{pdflatex}{@tufte@pdf}
  \typeoutbool{xelatex}{@tufte@xetex}
  \typeout{----------------------------------------------------}
}



%%
% Color
\RequirePackage{xcolor}

%%
% Produces a full title page

\newcommand{\maketitlepage}[0]{%
  \cleardoublepage
  {
  \sffamily
  \begin{fullwidth}
  \par\noindent\fontsize{18}{18}\selectfont\textcolor{darkgray}{\allcaps{\thanklessauthor}}
  \end{fullwidth}

  \vspace{11.5pc}
  \begin{fullwidth}
  \par\noindent\fontsize{36}{36}\selectfont\textcolor{darkgray}{\allcaps{\thanklesstitle}}
  \end{fullwidth}

  \vfill
  \begin{fullwidth}
  \par\noindent\fontsize{14}{14}\selectfont\allcaps{\thanklesspublisher}
  \end{fullwidth}
  }
  \thispagestyle{empty}
  \clearpage
}

%%
% Title block

\renewcommand{\maketitle}{%
  \newpage
  \global\@topnum\z@% prevent floats from being placed at the top of the page
  \begingroup
  \setlength{\parindent}{0pt}
  \setlength{\parskip}{4pt}
  \ifthenelse{\boolean{@tufte@sfsidenotes}}
    {\begingroup
      % FIXME fails with \thanks
      \sffamily
      \par{\Large\allcaps{\@title}}
      \ifthenelse{\equal{\@author}{}}{}{\par{\large\allcaps{\@author}}}
      \ifthenelse{\equal{\@date}{}}{}{\par{\large\allcaps{\@date}}}
    \endgroup}
    {\begingroup
      \par{\Large\textit{\@title}}
      \ifthenelse{\equal{\@author}{}}{}{\par{\large\textit{\@author}}}
      \ifthenelse{\equal{\@date}{}}{}{\par{\large\textit{\@date}}}
    \endgroup}
  \par
  \endgroup
  \thispagestyle{plain}% suppress the running head
}

{'fraction_non_alphanumeric': 0.15904169186647676, 'fraction_numerical': 0.008915855395360633, 'mean_word_length': 4.569705530805172, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5957095', 'n_tokens_mistral': 19020, 'n_tokens_neox': 18002, 'n_words': 3536}