%%
%% file: pgreenbook.sty
%% (c) 2013-2022, Progress
%% (c) 2022,2024, Nova de Hi
%%
%% This package was written by Progress as a working
%% example for KNU workshop 2013.
%%
%% Nova de Hi slightly modified and tidied up the original version.
%%
\ProvidesPackage{pgreenbook}[2024/06/16 an example book v0.4]

\RequirePackage{l3keys2e}

\ExplSyntaxOn
\keys_define:nn { pgreenbook }
{
	exthanja	.bool_set:N = \opt_exthanja_bool,
	notrims		.bool_set:N = \opt_notrim_bool,
}

\keys_set:nn { pgreenbook }
{
	exthanja = false,
	notrims = false
}

\ProcessKeysOptions { pgreenbook }
\ExplSyntaxOff

\usepackage[stock]{fapapersize}
\usefastocksize{210mm,297mm}
\setheadfoot{\headheight}{12mm}
\usefapapersize{174mm,251mm,27mm,*,35.5mm,*}

\ExplSyntaxOn
\bool_if:NF \opt_notrim_bool
{
	\quarkmarks
}
\ExplSyntaxOff

\RequirePackage{amsmath,amsthm}

\@ifclassloaded{oblivoir}{}{%
	\RequirePackage[hangul]{kotex}
}

\RequirePackage{graphicx}
\graphicspath{{figure/}}  

\RequirePackage{tabularray}
%%% 2022-04-10
%%% We gave up the tabu package. Instead,
%%% the tabularray will be used for tabular drawing.
% tabu-fix
% to make "spread 0pt" work
% -----------------------------
%\RequirePackage{etoolbox}
%\patchcmd
%	\tabu@startpboxmeasure
%	{\bgroup\begin{varwidth}}%
%	{\bgroup
%	 \iftabu@spread\color@begingroup\fi\begin{varwidth}}%
%	{}{}
%\def\@tabarray{\m@th\def\tabu@currentgrouptype
%    {\currentgrouptype}\@ifnextchar[\@array{\@array[c]}}
%%
%%%% \pdfelapsedtime bug 2019-12-15
%\patchcmd
%	\tabu@message@etime
%	{\the\pdfelapsedtime}%
%	{\pdfelapsedtime}%
%	{}{}
%%
%% -----------------------------

%% 폰트 설정
\setmainfont{STIX Two Text}
\setsansfont{Source Sans Pro}[Scale=MatchUppercase,BoldFont={* Semibold}]
\setkomainfont[KoPubWorldBatang ](Light)(Medium)
\setkosansfont[KoPubWorldDotum ](Light)(Medium)
\ExplSyntaxOn
\ifXeTeX
\bool_if:NT \opt_exthanja_bool
{
	\newfontfamily\fallbackhanjafont{Noto Serif KR}[Scale=.92]
}
\xetexkofontregime[puncts=prevfont, colons=prevfont, cjksymbols=hangul]{latin}
\fi
\ExplSyntaxOff

\usepackage{unicode-math}
\setmathfont{latinmodern-math.otf}
\setmathhangulfont{KoPubWorldBatang Light}

%% 줄간격
\@ifclassloaded{oblivoir}{\SetHangulspace{1.4}{1.12}}{\linespread{1.4}}
%% 멀티라인 수식의 줄간격도 덩달아 넓어지는 것을 방지
%\everydisplay{\setstretch{1.2}}
\RequirePackage[mathleading=1.2]{ob-mathleading}

%% 색깔 설정
\@ifpackageloaded{xcolor}{}{
	\RequirePackage[dvipsnames,svgnames,x11names]{xcolor}
}
% Pantone Fashion Color: Spring 2014
\definecolor{PlacidBlue}{cmyk}{.47,.17,.02,.0}
\definecolor{VioletTulip}{cmyk}{.44,.39,.0,.0}
\definecolor{Hemlock}{cmyk}{.39,.04,.35,.0}
\definecolor{Paloma}{cmyk}{.35,.24,.27,.0}
\definecolor{Sand}{cmyk}{.20,.27,.48,.0}
\definecolor{Freesia}{cmyk}{.0,.14,.100,.0}
\definecolor{Cayenne}{cmyk}{.06,.74,.56,.0}
\definecolor{CelosiaOrange}{cmyk}{.0,.63,.80,.0}
\definecolor{RadiantOrchid}{cmyk}{.32,.65,.0,.0}
\definecolor{DazzlingBlue}{cmyk}{.92,.57,.0,.0}
\definecolor{PurpleHaze}{cmyk}{.56,.51,.15,.0}
\definecolor{Comfrey}{cmyk}{.74,.28,.63,.10}
\definecolor{MagentaPurple}{cmyk}{.51,.94,.24,.24}

\colorlet{MainColorOne}{Comfrey}
\colorlet{MainColorTwo}{PurpleHaze}

\RequirePackage{tikz}
\RequirePackage{tikzpagenodes}
%\RequirePackage{memtikzpagenodes}
%\RequirePackage{oblivoir-misc}
\RequirePackage{calc}

% 일반적인 페이지 스타일
\pagestyle{hangul}

%% 조금 더 멋을 부리자. KNUworkshop

%% \copypagestyle{KNUworkshop}{companion}
\makepagestyle{KNUworkshop}
\makeheadrule{KNUworkshop}{0pt}{0pt}
%% 쪽 번호 자릿수. 세 자리 숫자가 들어갈 폭을 가지도록 함. 
\newlength\pagenumwidth
\settowidth{\pagenumwidth}{999}
%% 쪽 번호 들어가는 상자
\tikzset{pagefooter/.style={
anchor=base,font=\sffamily\bfseries\small,
text=white,fill=MainColorOne!60,text centered,
text depth=20mm,text width=\pagenumwidth}}

\makeoddhead{KNUworkshop}{}{}{%
    \sffamily\footnotesize%
    \begin{tikzpicture}[
    	xshift=-.75\baselineskip,
    	yshift=.25\baselineskip,
		overlay,
		fill=MainColorOne!50,
		draw=MainColorOne!50
	]
    \fill circle(3pt);
    \draw[semithick](0,0) -- (-\textwidth-2\spinemargin,0);
    \end{tikzpicture} 
    \rightmark
}
\makeevenhead{KNUworkshop}{%
    \sffamily\footnotesize\leftmark\,\, 
    \begin{tikzpicture}[
    	xshift=.5\baselineskip,
		yshift=.25\baselineskip,
		overlay,
		fill=MainColorOne!50,
		draw=MainColorOne!50
	]
	\fill (0,0) circle (3pt); 
	\draw[semithick](\textwidth+2\spinemargin,0) -- (0,0);
	\end{tikzpicture}
}{}{}
\makeoddfoot{KNUworkshop}{}{}{\tikz[baseline]\node[pagefooter]{\thepage};}
\makeevenfoot{KNUworkshop}{\tikz[baseline]\node[pagefooter]{\thepage};}{}{}

\copypagestyle{chapter}{KNUworkshop}
\makeoddhead{chapter}{}{}{}
\makeevenhead{chapter}{}{}{}

%% 페이지 스타일의 원리를 보여주는 
\makepagestyle{PageStylePrinciple}
%% 쪽 번호 들어가는 상자
\tikzset{pageheader/.style={
    anchor=base,
    font=\sffamily\bfseries\small,
    text=white,
    fill=MainColorTwo!60,
    text centered,
    text height=20mm,
    text width=\pagenumwidth,}
}

\makeoddhead{PageStylePrinciple}{}{\rightmark}{\tikz[baseline,overlay,]\node[pageheader]{\thepage};}
\makeevenhead{PageStylePrinciple}{\tikz[baseline,overlay,]\node[pageheader]{\thepage};}{\leftmark}{}
\makeoddfoot{PageStylePrinciple}{}{\textsf{\thetitle}}{}
\makeevenfoot{PageStylePrinciple}{}{\textsf{\theauthor}}{}

%% psmarks
\makepsmarks{KNUworkshop}{%
	\createmark{chapter}{left}{shownumber}{\pre@chapter}{\post@chapter\enskip}
	\createmark{section}{right}{shownumber}{}{\enskip}
}
\makepsmarks{PageStylePrinciple}{%
	\createmark{chapter}{left}{nonumber}{}{}
	\createmark{section}{right}{nonumber}{}{}
}

% 챕터 스타일
%\RequirePackage{ob-chapstyles}

% 장제목 스타일
\makechapterstyle{KNUchapter}{%
	\setlength{\afterchapskip}{40pt}
	\renewcommand*{\chapterheadstart}{\vspace*{-3\onelineskip}}
	\renewcommand*{\afterchapternum}{\par\nobreak\vskip 25pt}
	\renewcommand*{\chapnamefont}{\normalfont\LARGE\flushright}
	\renewcommand*{\chapnumfont}{\normalfont\HUGE\color{MainColorOne}}
	\renewcommand*{\chaptitlefont}{\color{MainColorOne}\sffamily\HUGE\bfseries\flushright}
	\renewcommand*{\prechapternum}{\chapnamefont\MakeUppercase{Chapter}}
	\renewcommand*{\postchapternum}{%
		\makebox[0pt][l]{%
		\hspace{1em}{\color{MainColorOne!50}\rule{\midchapskip+\spinemargin}{\beforechapskip}}
	}}
	\renewcommand*{\chapternamenum}{}
	\setlength{\beforechapskip}{18mm}%  \numberheight
	\setlength{\midchapskip}{\paperwidth}% \barlength
	\addtolength{\midchapskip}{-\textwidth}
	\addtolength{\midchapskip}{-\spinemargin}
	\renewcommand*{\printchapternum}{%
		\hspace{1em}\resizebox{!}{\beforechapskip}{\chapnumfont \thechapter}}
	\makeoddfoot{plain}{}{}{\normalfont\normalsize\sffamily\thepage}
}

% 섹션/ 서브섹션 바꾸기
\setsechook{%
    \setsecnumformat{\thesection\quad}
    \setsecheadstyle{\sffamily\bfseries\Large\color{MainColorOne}}
    \setsubsecheadstyle{\sffamily\bfseries\color{MainColorTwo}}
    \hangsecnum
}

% 정리류 
% \newtheorem{thm}{정리}[chapter]
% \newtheorem{cor}[thm]{보조정리}
% \renewcommand{\proofname}{증명}

\newcounter{thm} % 새 카운터를 만들고
\setcounter{thm}{0} % 0으로 초기화

\newenvironment{thm}[1][\empty]{% 옵션 변수 하나를 받을 예정
\refstepcounter{thm} % 이게 있어야 번호가 순차적으로 늘어나며 \label과 \ref 가능
\begin{framed}
\ifx#1\empty
\noindent{\bfseries\sffamily정리 \thechapter.\thethm}\quad
\else
\noindent{\bfseries\sffamily정리 \thechapter.\thethm\enskip ({\small#1})}\quad % 정리 [ ] 안에 있는 부가 설명을 괄호 ( ) 안에 표시
\fi
}{
\end{framed}
}

\colorlet{shadecolor}{MainColorOne!20}

\NewDocumentEnvironment{lemcor}{mo}{
\refstepcounter{thm}
\begin{shaded}
\IfNoValueTF{#2}
{\noindent{\bfseries\sffamily #1 \thechapter.\thethm}\quad} % 정리의 카운터를 이어서 사용
{\noindent{\bfseries\sffamily #1 \thechapter.\thethm\enskip ({\small#2})}\quad}
}{
\end{shaded}
}

\NewDocumentEnvironment{lem}{o}
{
\IfValueTF{#1}
{\begin{lemcor}[보조정리][#1]}
{\begin{lemcor}{보조정리}}
}{
\end{lemcor}
}

\NewDocumentEnvironment{cor}{o}
{
\IfValueTF{#1}
{\begin{lemcor}[따름정리][#1]}
{\begin{lemcor}{따름정리}}
}{
\end{lemcor}
}

\renewcommand{\proofname}{\color{MainColorOne}{\bfseries\sffamily 증명}}
\renewcommand\qedsymbol{\color{MainColorOne!50}■} % QED 마크 변경
 

\renewenvironment{proof}[1][\empty]{%
\par%
  \pushQED{\qed} % \qedhere 쓰기 위한 장치 ①. from amsthm.sty
\begin{description}
\ifx#1\empty
\item[\bfseries\sffamily\color{MainColorOne}증명]
\else
\item[\bfseries\sffamily\color{MainColorOne}증명({\small#1})] \quad
\fi
}{\popQED  % \qedhere 쓰기 위한 장치 ②
\end{description}%
\@endpefalse%
\par%
}


%차례 만져주기
%본문에서 subsubsection까지 번호가 붙음.
%\setsecnumdepth{subsection}
\maxsecnumdepth{subsection}

%toc에서 section까지 번호가 붙음.
\maxtocdepth{subsection}
%\settocdepth{subsection}

%차례에서 들여쓰기 따위 자잘한 설정
\renewcommand{\cftchapterfont}{\large\bfseries\color{MainColorTwo}}
%\renewcommand{\cftchapterleader}{/~}
%\renewcommand{\cftchapterafterpnum}{\cftparfillskip}
\setlength{\cftchapternumwidth}{4.0em}
%\cftsetindents{chapter}{0em}{3.5em}
%\cftsetindents{section}{3.5em}{2.5em}

\renewcommand{\cftsectionpresnum}{\raggedright} % note the double `l'
\setlength{\cftsectionnumwidth}{2.3em}
\setlength{\cftsectionindent}{4.1em}
% \renewcommand{\cftsectionnumwidth}{2.5em}

\setlength{\cftsubsectionindent}{6.5em}
\setlength{\cftsubsectionnumwidth}{3.0em}


% 찾아보기 (Index}
%\RequirePackage{makeidx}

%\DeclareRobustCommand{\myem}[2][\empty]{%
%\ifx#1\empty
%#2\index{#2}%
%\else
%#2\index{#2}(#1)\index{#1}%
%\fi}
\NewDocumentCommand{\myem}{om}{%
    \IfNoValueTF{#1}%
    {#2\index{#2}}%
    {#2\index{#2}(#1)\index{#1}}}

\makeindex

\RequirePackage{multicol} % 3단 조판하려고

\renewenvironment{theindex}{%
  \renewcommand\indexspace{\vskip\onelineskip}
    \@restonecoltrue
    \setlength{\columnseprule}{\indexrule}%
    \setlength{\columnsep}{\indexcolsep}%
  \chapter*{\indexname}
  \markright{\indexname}
  \preindexhook
  \setlength{\columnsep}{1.8em}%
  \begin{multicols}{3}%
  \linespread{1.2}\small
  \small
  \spaceskip=.275em plus .1em minus .3pt
  \flushcolumns
  \indexmark
    \phantomsection
    \addcontentsline{toc}{chapter}{\indexname}%
    \parindent\z@
  \parskip\z@ \@plus .3\p@\relax
  \let\item\@idxitem}%
 {\end{multicols}
}

\renewcommand{\@idxitem}{\par\raggedright\hangindent 20\p@}

% 캡션 제목과 내용 스타일
\captiondelim{\quad}
\captionnamefont{\small\sffamily}
\captiontitlefont{\small\normalfont}

\endinput
