%=================================================
%% 2 percentage signs represent comments for code;
% 1 percentage sign represents commented out code

%% LaTeX-style CV with biblatex package usage
%% Paul Le Tran
%% Last updated: 24 February, 2024.
%=================================================
%% Packages used
\usepackage{paralist}
\usepackage{ragged2e}
\usepackage{datetime}
\usepackage[hyphens]{url}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{enumitem}
\usepackage[margin = 1in]{geometry}
\usepackage{titlesec}
\usepackage{array}

%% Define ragged right columns within tables
\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}

%% Colour of headings
\def\headcolor{\color[rgb]{0, 0, 0}}
\RaggedRight
\sloppy

%% Header and footer of first page
\fancypagestyle{firststyle}{
  \fancyhf{}
  \cfoot{\thepage \ of {\hypersetup{linkcolor = black}\pageref{LastPage}}}
  \renewcommand{\headrulewidth}{0pt}
}

%% Header and footer of every page after first page
\pagestyle{fancy}
\lhead{\textcolor[gray]{0.4}{\textbf{\@name \ (CV, he/him/his)}}}
\rhead{\textcolor[gray]{0.4}{\thepage \ of {\hypersetup{linkcolor = black}\pageref{LastPage}}}}
\cfoot{}
\def\headrule{{\color[gray]{0.4}\hrule\@height\headrulewidth\@width\headwidth \vskip-\headrulewidth}}

%% Date format
\newdateformat{dmy}{\monthname \ \THEYEAR}
\dmy

%% Header box
\def\name#1{\def\@name{#1}}
\def\info#1{\def\@info{#1}}
\newcommand{\shadebox}[3][.9]{\fcolorbox[gray]{0}{#1}{\parbox{#2}{#3}}}
\def\maketitle{
  \thispagestyle{plain}
  \shadebox[0.9]{6.1in}{
    \headcolor\hfil
    \hbox to 15cm{
    \begin{tabular}{P{6.9cm}}
      \LARGE\textbf{\@name}\\
      \normalsize \textbf{(he/him/his)}\\\\
      \normalsize \textbf{Curriculum Vitae (CV)}\\
      \normalsize Last updated: \today.\\
    \end{tabular}
    \hfill\hbox{\fontsize{10}{12}
    
    \begin{tabular}{@{}rP{7.4cm}@{}}
      \@info
    \end{tabular}}}\hfil
  }
  \vspace*{0.2cm}
}

%% Section headings
\titlelabel{}
\titlespacing{\section}{0pt}{2ex}{1ex}
\titleformat*{\section}{\headcolor\large\scshape}
\titlespacing{\subsection}{0pt}{1ex}{0.5ex}

%% Miscellaneous dimensions
\setlength{\parskip}{-0.5ex}
\setlength{\parindent}{0em}
\setlength{\headheight}{15pt}
\setlength{\tabcolsep}{0.15cm}
\clubpenalty = 10000
\widowpenalty = 10000
\setlist{itemsep = 1pt}
\setdescription{labelwidth = 1.2cm, leftmargin = 1.5cm, labelindent = 1.5cm, font = \rm}

%% Bibliography formatting
\usepackage[
  sorting = ydnt,
  citestyle = authoryear,
  bibstyle = authoryear-comp,
  defernumbers = true,
  maxnames = 20,
  giveninits = false,
  bibencoding = utf8,
  terseinits = true,
  uniquename = init,
  dashed = false,
  doi = true,
  isbn = false,
  natbib = true,
  backend = biber,
  date = year
]{biblatex}
\DeclareFieldFormat{url}{\url{#1}}
\DeclareFieldFormat[article]{pages}{#1}
\DeclareFieldFormat[inproceedings]{pages}{\lowercase{pp.}#1}
\DeclareFieldFormat[incollection]{pages}{\lowercase{pp.}#1}
\DeclareFieldFormat[article]{volume}{\mkbibbold{#1}}
\DeclareFieldFormat[article]{number}{\mkbibparens{#1}}
\DeclareFieldFormat[article]{title}{\MakeCapital{#1}}
\DeclareFieldFormat[article]{url}{}
\DeclareFieldFormat[inproceedings]{title}{#1}
\DeclareFieldFormat{shorthandwidth}{#1}
\DeclareFieldFormat{extradate}{}

%% No dot before number of articles
\usepackage{xpatch}
\xpatchbibmacro{volume+number+eid}{\setunit*{\adddot}}{}{}{}

%% Remove In: for an article.
\renewbibmacro{in:}{%
  \ifentrytype{article}{}{
    \printtext{\bibstring{in}\intitlepunct}
  }
}
%\makeatletter
%\DeclareDelimFormat[cbx@textcite]{nameyeardelim}{\addspace}
%\makeatother

%% Bibliography categories
\def\makebibcategory#1#2{
  \DeclareBibliographyCategory{#1}
  \defbibheading{#1}{\section*{#2}}
}
\makebibcategory{books}{Books}
\makebibcategory{papers}{Refereed research papers}
\makebibcategory{chapters}{Book chapters}
\makebibcategory{conferences}{Papers in conference proceedings}
\makebibcategory{techreports}{Unpublished working papers}
\makebibcategory{bookreviews}{Book reviews}
\makebibcategory{editorials}{Editorials}
\makebibcategory{phd}{PhD thesis}
\makebibcategory{subpapers}{Submitted papers}
\makebibcategory{wps}{Working papers}
\makebibcategory{wips}{Works in progress}
\makebibcategory{fin}{Finished papers}
\setlength{\bibitemsep}{2.3pt}
\setlength{\bibhang}{.9cm}
\renewcommand*{\bibitem}{
  \addtocounter{papers}{1}
  \item \mbox{}\hskip-0.9cm\hbox to 0.7cm{\hfill\arabic{papers}.~\,}
}
\defbibenvironment{bibliography}
{\list{}
  {
    \setlength{\leftmargin}{\bibhang}%
    \setlength{\itemsep}{\bibitemsep}%
    \setlength{\parsep}{\bibparsep}
  }
}
{\endlist}
{\bibitem}

%% Creating publications environment
\newenvironment{publications}{
  \label{papersstart}
  \vspace*{0cm}
  \titlespacing{\section}{0pt}{1.5ex}{1ex}\itemsep = 0.00cm
}{
  \label{papersend}
  \addtocounter{sumpapers}{-1}
  \refstepcounter{sumpapers}
  \label{sumpapers}
}
\def\printbib#1{\printbibliography[category = #1, heading = #1]\lastref{sumpapers}}

%% Counters for keeping track of papers
%% Sumpapers will naturally count everything contained inside of the publications environment (because you ideally want only publications counted). For conveninecen, the following lines of code modify the count of sumpapers of the publications environment if you want to count other materials, such as manuscripts submitted for publication (because you have no publications yet :') )
%% If you're counting the number of publications, subtract the number of .bib items THAT COME BEFORE the publications environemtn (x) from sumpapers (0 - x = -x)
%% If you're counting the number of publications, keep sumpapers argument to equal zero since these papers go under the ``Working Papers'' category, which is first
%% This does not need to be done for .bib items after the publications environment
\newcounter{papers}\setcounter{papers}{0}
\newcounter{sumpapers}\setcounter{sumpapers}{0}
\def\lastref#1{\addtocounter{#1}{\value{papers}}\setcounter{papers}{0}}

%% Add all papers in the bib file.
\nocite{*}

%% Code for sorting separate bibliography environment by paper title and year rather than author name if desired
\DeclareSortingScheme{title_year}{
  \sort{
    \field{title}
  }
  \sort{
    \field{year}
  }
}