%%fakesection Load packages

\usepackage{lmodern}
\usepackage[pdfusetitle]{hyperref}
\ExplSyntaxOn
\sys_if_engine_luatex:T {
	\usepackage{luatex85}
}
\sys_if_engine_pdftex:T {
	\usepackage[T1]{fontenc}
}
\ExplSyntaxOff

% These are evan.sty
\usepackage{amsmath,amssymb,amsthm}
\usepackage{mathrsfs}
\usepackage[usenames,svgnames,dvipsnames]{xcolor}
\usepackage{textcomp}
\usepackage{enumerate}
\usepackage[textsize=scriptsize,shadow]{todonotes}
\usepackage{mathtools}
\usepackage{microtype}
\usepackage[normalem]{ulem}
\usepackage{stmaryrd}
\usepackage{wasysym}
\usepackage{multirow}
\usepackage{prerex}
\usepackage[nameinlink]{cleveref}
\usepackage{derivative}

%%fakesection evan.sty macros
%Small commands
%% Napkin commands
\newcommand{\prototype}[1]{
	\emph{{\color{red} Prototypical example for this section:} #1} \par\medskip
}
\newenvironment{moral}{%
	\begin{tcolorbox}[boxrule=0.4pt,colframe=green!70!black,sharp corners,
		standard jigsaw,opacityback=0,left=10pt,right=10pt,top=3pt,bottom=3pt,
		before skip=10pt,after skip=20pt]%
	\bfseries\color{green!50!black}}%
	{\end{tcolorbox}}

%%fakesection Links (hyperref loaded earlier implicitly)
\hypersetup{
	linkcolor={red!50!black},
	citecolor={green!50!black},
	urlcolor={blue!80!black},
	pdfkeywords={napkin,math},
	pdfsubject={web.evanchen.cc},
	colorlinks,
}

%%fakesection Commutative diagrams
\usepackage{tikz-cd}
\usetikzlibrary{arrows,arrows.meta}
% make a larger hook
% https://tex.stackexchange.com/questions/514451/how-to-define-a-new-hooked-arrow
\makeatletter
\pgfdeclarearrow{
	name=xGlyph,
	cache=false,
	bending mode=none,
	parameters={\tikzcd@glyph@len,\tikzcd@glyph@shorten},
	setup code={%
		\pgfarrowssettipend{\tikzcd@glyph@len\advance\pgf@x by\tikzcd@glyph@shorten}},
	defaults={
		glyph axis=axis_height,
		glyph length=+1.55ex,
		glyph shorten=+-0.1ex},
	drawing code={%
		\pgfpathrectangle{\pgfpoint{+0pt}{+-1.5ex}}{\pgfpoint{+\tikzcd@glyph@len}{+3ex}}%
		\pgfusepathqclip%
		\pgftransformxshift{+\tikzcd@glyph@len}%
		\pgftransformyshift{+-\tikzcd@glyph@axis}%
		\pgftext[right,base]{\tikzcd@glyph}}}
\makeatother
\tikzcdset{
	arrow style=tikz,
	diagrams={>={Latex}},
	tikzcd left hook/.tip={xGlyph[glyph math command=supset, swap, glyph axis = 5.7pt]},
	tikzcd right hook/.tip={xGlyph[glyph math command=supset, glyph axis = 5.7pt]},
	surjective head arrow /.tip = {tikzcd to[sep=-1.5pt]tikzcd to},
	surjective head/.style={
		-surjective head arrow
	}
}

%%fakesection Page layout
\usepackage[headsepline]{scrlayer-scrpage}
\renewcommand{\headfont}{}
\addtolength{\textheight}{3.14cm}
\setlength{\footskip}{0.5in}
\setlength{\headsep}{10pt}

\def\shortdate{\leavevmode\hbox{\the\year-\twodigits\month-\twodigits\day}}
\def\twodigits#1{\ifnum#1<10 0\fi\the#1}
\automark[chapter]{chapter}

\rohead{\footnotesize\thepage}
\rehead{\footnotesize \textbf{\sffamily Napkin}, by \emph{Evan Chen} (\napkinversion)}
\lehead{\footnotesize\thepage}
\lohead{\footnotesize \leftmark}
\chead{}
\rofoot{}
\refoot{}
\lefoot{}
\lofoot{}
%\cfoot{\pagemark}

%%fakesection Fancy section and chapter heads
\renewcommand*{\sectionformat}{\color{purple}\S\thesection\autodot\enskip}
\renewcommand*{\subsectionformat}{\color{purple}\S\thesubsection\autodot\enskip}
\newcommand{\problemhead}{A few harder problems to think about}
\renewcommand{\thesubsection}{\thesection.\roman{subsection}}

\addtokomafont{chapterprefix}{\raggedleft}
\RedeclareSectionCommand[beforeskip=0.5em]{chapter}
\renewcommand*{\chapterformat}{%
\mbox{\scalebox{1.5}{\chapappifchapterprefix{\nobreakspace}}%
\scalebox{2.718}{\color{purple}\thechapter\autodot}\enskip}}

\addtokomafont{partprefix}{\rmfamily}
\renewcommand*{\partformat}{\color{purple}\scalebox{2.5}{\thepart}}

%%fakesection Theorems
\usepackage{tcolorbox}
\tcbuselibrary{breakable,skins,hooks}

% patch tcolorbox to support continuation of a paragraph
% after box
% from https://tex.stackexchange.com/questions/568782/
\makeatletter
\tcbset{
	after app={%
		\ifx\tcb@drawcolorbox\tcb@drawcolorbox@breakable
		\else
		% add only when not breakabel
		\@endparenv
		\fi
	}
}

% for breakable
\appto\tcb@use@after@lastbox{\@endparenv\@doendpe}
\makeatother
% END patch tcolorbox for continuation of paragraph


\usepackage{thmtools}

\theoremstyle{definition}
\declaretheoremstyle[
	headfont=\sffamily\bfseries\color{MidnightBlue},
	headpunct={\\[3pt]},
	postheadspace={0pt},
]{thmtheorem}


\declaretheoremstyle[
	headfont=\bfseries\color{RawSienna},
	headpunct={\\[3pt]},
	postheadspace={0pt},
]{thmexample}

\tcbset{
	theorem box/.style={
		enhanced,
		arc=9pt,
		outer arc=10pt,
		colframe=blue,
		colback=TealBlue!5,
		boxrule=1pt,
		before skip=12pt,
		after skip=14pt,
		left=10pt,
		right=10pt
	},
	remark box/.style={
		boxrule=0pt,
		frame hidden,
		sharp corners,
		enhanced,
		borderline west={2pt}{0pt}{ForestGreen},
		before skip=8pt,
		colback=ForestGreen!5,
		after skip=12pt,
		breakable,
		left=10pt,
		right=10pt
	},
	example box/.style={
		enhanced,
		sharp corners,
		arc=9pt,
		outer arc=10pt,
		colframe=RawSienna,
		colback=Salmon!5,
		boxrule=0.5pt,
		before skip=12pt,
		after skip=14pt,
		breakable,
		top=6pt,
		bottom=8pt,
		breakable,
		left=10pt,
		right=10pt
	},
	ques box/.style={
		boxrule=0pt,
		frame hidden,
		enhanced,
		sharp corners,
		before skip=8pt,
		after skip=12pt,
		borderline west={3pt}{0pt}{black},
		colback=RedViolet!5!gray!5,
		breakable,
		left=10pt,
		right=10pt
	}
}

\declaretheorem[style=thmtheorem,name=Theorem,numberwithin=section]{theorem}
\tcolorboxenvironment{theorem}{theorem box}
\declaretheorem[style=thmtheorem,name=Lemma,sibling=theorem]{lemma}
\tcolorboxenvironment{lemma}{theorem box}
\declaretheorem[style=thmtheorem,name=Proposition,sibling=theorem]{proposition}
\tcolorboxenvironment{proposition}{theorem box}
\declaretheorem[style=thmtheorem,name=Corollary,sibling=theorem]{corollary}
\tcolorboxenvironment{corollary}{theorem box}
\declaretheorem[style=thmexample,name=Example,sibling=theorem]{example}
\tcolorboxenvironment{example}{example box}

\declaretheoremstyle[
	headfont=\bfseries\sffamily\color{ForestGreen!70!black},
	bodyfont=\normalfont,
	headpunct={ --- },
]{thmremark}
\declaretheoremstyle[
	headfont=\bfseries\sffamily\color{ForestGreen!70!black},
	bodyfont=\normalfont,
	headpunct={},
]{thmremark*}

\declaretheoremstyle[
	headfont=\bfseries,
	bodyfont=\normalfont\small
]{thmques}

\declaretheorem[name=Question,sibling=theorem,style=thmques]{ques}
\tcolorboxenvironment{ques}{ques box}
\declaretheorem[name=Exercise,sibling=theorem,style=thmques]{exercise}
\tcolorboxenvironment{exercise}{ques box}
\declaretheorem[name=Remark,sibling=theorem,style=thmremark]{remark}
\tcolorboxenvironment{remark}{remark box}
\declaretheorem[name=Remark,sibling=theorem,style=thmremark*]{remark*}
\tcolorboxenvironment{remark*}{remark box}
\declaretheorem[name=Step,style=thmremark]{step} % only used in Lebesgue int
\tcolorboxenvironment{step}{remark box}

\theoremstyle{definition}
\newtheorem{claim}[theorem]{Claim}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{fact}[theorem]{Fact}
\newtheorem{abuse}[theorem]{Abuse of Notation}

\newtheorem{problem}{Problem}[chapter]
\renewcommand{\theproblem}{\thechapter\Alph{problem}}
\newtheorem{sproblem}[problem]{Problem}
\newtheorem{dproblem}[problem]{Problem}
\renewcommand{\thesproblem}{\theproblem$^{\star}$}
\renewcommand{\thedproblem}{\theproblem$^{\dagger}$}
\newcommand{\listhack}{$\empty$\vspace{-2em}}

%%fakesection Answers
\usepackage{answers}
\Newassociation{hint}{answeritem}{tex/backmatter/all-hints}
\Newassociation{sol}{answeritem}{tex/backmatter/all-solns}
\renewcommand{\solutionextension}{out}
\renewenvironment{answeritem}[1]{\item[\bfseries #1.]}{}

%%fakesection Table of contents
% First add ToC to ToC
\makeatletter
\usepackage{etoolbox}
\pretocmd{\tableofcontents}{%
	\if@openright\cleardoublepage\else\clearpage\fi
	\pdfbookmark[0]{\contentsname}{toc}%
}{}{}%
\makeatother
\setcounter{tocdepth}{1}
\RedeclareSectionCommand[tocnumwidth=4.2em]{part}
\RedeclareSectionCommand[tocpagenumberwidth=2.2em,tocnumwidth=4.2em]{chapter}
\RedeclareSectionCommand[tocpagenumberwidth=2.2em,tocnumwidth=2.8em]{section}
% adjust tocpagenumberwidth manually for large page number: https://tex.stackexchange.com/a/502168

%%fakesection Asymptote definitions
\usepackage{patch-asy}
\numberwithin{asy}{chapter}
\renewcommand{\theasy}{\thechapter\Alph{asy}}
\begin{asydef}
	import graph;

	size(6cm);
	usepackage("amsmath");
	usepackage("amssymb");
	defaultpen(fontsize(11pt));
	settings.tex = "latex";
	settings.outformat = "pdf";

	// from cse5
	pair origin = (0,0);
	pair MP(Label L, pair A, pair B=plain.S, pen p=defaultpen) {
		Label M=L;
		M.s="\("+L.s+"\)";
		label(M,A,B,p);
		return A;
	}
	pair Drawing(Label L="", pair A, pair B=plain.S, pen p=defaultpen) {
		dot(MP(L, A, B, p), p);
		return A;
	}
	path Drawing(path p, pen q=defaultpen, arrowbar ar=None) {
		draw(p,q,ar);
		return p;
	}
	path CP(pair P, pair A) { return circle(P, abs(A-P)); }
	path CR(pair P, real r) { return circle(P, r); }
	pair IP(path p, path q) { return intersectionpoint(p,q); }
	pair OP(path p, path q) { return intersectionpoints(p,q)[1]; }
	picture CC() {
		picture p=rotate(0)*currentpicture;
		currentpicture.erase();
		return p;
	}

	// Napkin extras

	void bigbox(string L) {
		filldraw((-4,3)--(4,3)--(4,-3)--(-4,-3)--cycle,
			lightblue+opacity(0.2), black);
		label(L, (4,3), dir(-45));
	}
	void bigblob(string L) {
		filldraw((-4,3)..(-5,1)..(-4.7,0)..(-4.1,-2)..(-4,-3)
			..(-1,-3.3)..(1,-3.2)..(4,-3)
			..(4.3,-1)..(4.1,0.5)..(4,3)
			..(2,3.3)..(0,3.1)..(-3,2.9)..cycle,
			lightblue+opacity(0.2), black);
		label(L, (4,3), dir(5));
	}

	real[] default_ticks = {-4,-3,-2,-1,0,1,2,3,4};
	void cplane(real theta=-45, real[] T = default_ticks, real xmin = -4.5, real xmax = 4.5, real S = 3) {
		graph.xaxis("Re", xmin, xmax, graph.Ticks(format="%", Ticks=T, Size=S), Arrows);
		graph.yaxis("Im", xmin, xmax, graph.Ticks(format="%", Ticks=T, Size=S), Arrows);
		Drawing("0", (0,0), dir(theta));
	}

	pair opendot(pair P, pen p = defaultpen) {
		dot(P, p, UnFill);
		dot(P, p, Draw);
		return P;
	}
\end{asydef}
\def\asydir{asy}

%%fakesection Bibliography
\usepackage[backend=biber,backref=true,style=alphabetic]{biblatex}
\DeclareLabelalphaTemplate{
	\labelelement{
		\field[final]{shorthand}
		\field{label}
		\field[strwidth=2,strside=left]{labelname}
	}
	\labelelement{
		\field[strwidth=2,strside=right]{year}
	}
}
\DeclareFieldFormat{labelalpha}{\textbf{\scriptsize #1}}
\addbibresource{references.bib}
\addbibresource{images.bib}
%% stylistic biblatex choices
\DefineBibliographyStrings{english}{%
	backrefpage  = {cited p.}, % for single page number
	backrefpages = {cited pp.} % for multiple page numbers
}
\DeclareFieldFormat{journaltitle}{\mkbibemph{#1},} % italic journal title with comma
\DeclareFieldFormat[inbook,thesis]{title}{\mkbibemph{#1}\addperiod} % italic title with period
\DeclareFieldFormat[article]{title}{#1} % title of journal article is printed as normal text
\DeclareFieldFormat[article]{volume}{\textbf{#1}\addcolon\space}
\renewcommand{\mkbibnamegiven}[1]{\textsc{#1}}
\renewcommand{\mkbibnamefamily}[1]{\textsc{#1}}
\renewcommand{\mkbibnameprefix}[1]{\textsc{#1}}
\renewcommand{\mkbibnamesuffix}[1]{\textsc{#1}}
\renewcommand{\finentrypunct}{}

%%fakesection Mini ToC
\usepackage[tight]{minitoc}
\mtcsetfont{parttoc}{chapter}{\sffamily\bfseries}
\mtcsetfont{parttoc}{section}{\footnotesize\rmfamily\upshape\mdseries}
\mtcsetfont{parttoc}{subsection}{\footnotesize\rmfamily\upshape\mdseries}
%\mtcsetdepth{parttoc}{1}
\setcounter{parttocdepth}{1}
\renewcommand*{\partheadstartvskip}{\vspace*{20em}}
\renewcommand*{\partheadendvskip}{}
%\noptcrule
\renewcommand\beforeparttoc{\noindent{\bfseries \Large Part \thepart: Contents}}
%\hspace{\fill}\rule{0.95\linewidth}{2pt}\hspace{\fill}
\doparttoc[n]

%%fakesection Misc haxx
\pdfstringdefDisableCommands{\def\Spec{\text{Spec }}\def\sigma{σ}}
