%% File `memhangul-common.sty`
%%
%% (C) Copyright 2013-2015 Kangsoo Kim <karnes at ktug org>
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% 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.3c or later is part of all distributions of LaTeX
%% version 2006/05/20 or later.
%%
%%%
%%% part of kotex-oblivoir
%%%
%%% =====================
%%% 편, 장 섹션 타이틀 재설정
%%% 절의 섹션 타이틀은 수정하지 않음.
%%
\ProvidesPackage{memhangul-common}[2015/10/15]

\let\@CHAPAPP\@chapapp
\def\refreshprepostchapters{%
  \if@hanja
     \def\pre@chapter{第}%
  \else
     \def\pre@chapter{제}%
  \fi
  \let\@chapapp\@CHAPAPP
  \def\post@chapter{\@chapapp}%
}
\refreshprepostchapters
%\renewcommand\@chapapp{장}
\def\prechapternum{\pre@chapter}
\def\postchapternum{\post@chapter}
\newcommand\hchaptertitlehead{\pre@chapter\,\thechapter\,\post@chapter}
\providecommand\partmark[1]{}

\let\pre@part\pre@chapter
\let\post@part\partname
%\renewcommand{\printpartname}{\partnamefont \pre@part}
\renewcommand{\printpartname}{} % disabled.
\renewcommand{\partnamenum}{\space}
\def\prepartnum{\partnamefont \pre@part}
\def\postpartnum{\partnamefont \post@part}
\newcommand\hparttitlehead{\pre@part\partnamenum\thepart\partnamenum\post@part}

%% PART
%\long\def\@part[#1]#2{%
%  \M@gettitle{#1}%
%  \ifnum \c@secnumdepth >-2\relax
%    \refstepcounter{part}%
%    \addcontentsline{toc}{part}%
%      {\protect\partnumberline{\hparttitlehead}#1}%
%  \else
%    \addcontentsline{toc}{part}{#1}%
%  \fi
%%  \markboth{}{}%
%  \ifx#1\@empty\partmark{#2}\else\partmark{#1}\fi
%  {\centering
%   \interlinepenalty \@M
%   \normalfont
%   \ifnum \c@secnumdepth >-2\relax
%     \prepartnum \partnamenum \printpartnum \partnamenum \postpartnum
%     \midpartskip
%   \fi
%   \printparttitle{#2}\par}%
%  \@endpart}
\long\def\@part[#1]#2{%
%  \ifx#1\@empty\PrerenderUnicode{#2}\else\PrerenderUnicode{#1}\fi
  \M@gettitle{#1}%
  \def\f@rtoc{#1}%
  \@nameuse{part@f@rtoc@before@write@hook}%
  \phantomsection
  \mempreaddparttotochook
  \ifnum \c@secnumdepth >-2\relax
    \refstepcounter{part}%
    \addcontentsline{toc}{part}%
%      {\protect\partnumberline{\thepart}#1}%
      {\protect\partnumberline{\hparttitlehead}#1}%
    \mempartinfo{\thepart}{#1}{#2}%
  \else
    \addcontentsline{toc}{part}{#1}%
    \mempartinfo{}{#1}{#2}%
  \fi
  \mempostaddparttotochook
  \partmark{#1}%
  {\centering
   \interlinepenalty \@M
   \normalfont
   \ifnum \c@secnumdepth >-2\relax
%     \printpartname \partnamenum \printpartnum
%     \midpartskip
     \prepartnum \partnamenum \printpartnum \partnamenum \postpartnum
     \midpartskip
   \fi
   \printparttitle{#2}\par}%
  \@endpart}

\def\@spart#1{%
  \M@gettitle{#1}%
  \phantomsection
  {\centering
   \interlinepenalty \@M
   \normalfont
   \printparttitle{#1}\par}%
  \@endpart}

%\def\@endpart{\afterpartskip
%  \if@twoside
%    \if@openright
%      \null
%      \thispagestyle{empty}%
%      \newpage
%    \fi
%  \fi
%  \if@tempswa
%    \twocolumn
%  \fi} 

%% CHAPTER
%%%%%%%%%%%
\renewcommand{\@m@mchapter}[1][\@empty]{%
  \def\ch@pt@c{#1}% capture first optional arg
  \@dblarg{\@chapter}}
%%\def\m@m@empty{\@empty} 

%%%%\def\@chapter[#1]#2{%
%%%%  \ifx\ch@pt@c\m@m@empty % no optional args
%%%%    \def\f@rtoc{#1}%
%%%%    \def\f@rhdr{#1}%
%%%%  \else                  % at least one opt arg
%%%%    \def\f@rtoc{\ch@pt@c}%
%%%%    \nametest{#1}{#2}%
%%%%    \ifsamename          % one opt arg
%%%%      \def\f@rhdr{\ch@pt@c}%
%%%%    \else                % two opt args
%%%%      \def\f@rhdr{#1}%
%%%%    \fi
%%%%  \fi 
%%%%  \ifnum \c@secnumdepth >\m@ne
%%%%    \if@mainmatter
%%%%      \refstepcounter{chapter}%
%%%%    \fi
%%%%  \fi
%%%%  \chaptermark{\f@rhdr}%
%%%%  \ifartopt
%%%%    \@makechapterhead{#2}%
%%%%    \@afterheading
%%%%  \else
%%%%    \insertchapterspace
%%%%    \if@twocolumn
%%%%      \@topnewpage[\@makechapterhead{#2}]%
%%%%    \else
%%%%      \@makechapterhead{#2}%
%%%%    \fi
%%%%    \@afterheading
%%%%  \fi
%%%%  \ifnum \c@secnumdepth >\m@ne
%%%%    \if@mainmatter
%%%%      \addcontentsline{toc}{chapter}{%
%%%%%        \protect\chapternumberline{\thechapter}\f@rtoc}%
%%%%        \protect\chapternumberline{\hchaptertitlehead}\f@rtoc}%
%%%%    \else
%%%%      \addcontentsline{toc}{chapter}{\f@rtoc}%
%%%%    \fi
%%%%  \else
%%%%    \addcontentsline{toc}{chapter}{\f@rtoc}%
%%%%  \fi
%%%%  \ifheadnameref\M@gettitle{\f@rhdr}\else\M@gettitle{\f@rtoc}\fi
%%%%} 
%%%%
%%%%\def\@makechapterhead#1{%
%%%%  \chapterheadstart%  \vspace*{50\p@}%
%%%%  {\parindent \z@ \raggedright \normalfont
%%%%   \ifnum \c@secnumdepth >\m@ne
%%%%     \if@mainmatter
%%%%       \memucsinterwordchapterskiphook
%%%%%       \printchaptername \chapternamenum \printchapternum \chapternamenum \postchapternum
%%%%       \prechapternum \chapternamenum \printchapternum \chapternamenum \postchapternum
%%%%       \afterchapternum % \par\nobreak \vskip 20\p@
%%%%     \else
%%%%       \printchapternonum
%%%%     \fi
%%%%   \else
%%%%     \printchapternonum
%%%%   \fi
%%%%   \interlinepenalty\@M
%%%%   \printchaptertitle{#1} % \Huge \bfseries #1
%%%%    \afterchaptertitle % \par\nobreak \vskip 40\p@
%%%%  }}
%%%%
%%%%\def\@makeschapterhead#1{%
%%%%  \chapterheadstart
%%%%  {\parindent \z@ \raggedright \normalfont \memucsinterwordchapterskiphook
%%%%   \printchapternonum
%%%%   \interlinepenalty\@M
%%%%   \printchaptertitle{#1}
%%%%   \afterchaptertitle
%%%%  }
%%%%}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% CHAPTER
%% memhangul : opt args : different from memoir 1.618
%% \chapter[toc][hdr]{title}
%% \chapter[toc]{hdr/title}
%% \chapter{toc/hdr/title}
\def\@chapter[#1]#2{%
  \m@mpn@new@chaptrue%
  \m@mpn@new@schapfalse%
  \def\f@rbdy{#2}%
%  \ifx\ch@pt@c\@empty % no optional args
%    \def\f@rtoc{#2}%
%    \def\f@rhdr{#2}%
%    \PrerenderUnicode{#2}%
%  \else                  % at least one opt arg
%    \let\f@rtoc\ch@pt@c
%    \ifx\@empty#1\@empty
%      \let\f@rhdr\ch@pt@c
%      \PrerenderUnicode{#1#2}%
%    \else
%      \def\f@rhdr{#1}%
%      \PrerenderUnicode{#1}%
%    \fi
%  \fi
  \ifx\ch@pt@c\m@m@empty % no optional args
    \def\f@rtoc{#1}%
    \def\f@rhdr{#1}%
%    \PrerenderUnicode{#1}%
  \else                  % at least one opt arg
    \def\f@rtoc{\ch@pt@c}%
    \nametest{#1}{#2}%
    \ifsamename          % one opt arg
      \def\f@rhdr{\ch@pt@c}%
%      \PrerenderUnicode{#1#2}%
    \else                % two opt args
      \def\f@rhdr{#1}%
%      \PrerenderUnicode{#1}%
    \fi
  \fi 
  \m@m@Andfalse
  \ifnum \c@secnumdepth >\m@ne
    \if@mainmatter
      \m@m@Andtrue
    \fi
  \fi
  \ifm@m@And
   \ifanappendix
    \refstepcounter{APPchapter}%
   \else
    \refstepcounter{chapter}%
   \fi
  \fi
  \chaptermark{\f@rhdr}
  \ifartopt
    \@makechapterhead{#2}%
    \@afterheading
  \else
    \insertchapterspace
    \if@twocolumn
      \@topnewpage[\@makechapterhead{#2}]%
    \else
      \@makechapterhead{#2}%
    \fi
    \@afterheading
  \fi
  \ifm@m@And
    \ifanappendix
      \addcontentsline{toc}{appendix}{%
%       \protect\chapternumberline{\thechapter}\f@rtoc}%
       \protect\chapternumberline{\hchaptertitlehead}\f@rtoc}%
%       \memappchapinfo{\thechapter}{\f@rtoc}{\f@rhdr}{#2}%
       \memappchapinfo{\thechapter}{\f@rtoc}{\f@rhdr}{#2}%
    \else
      \addcontentsline{toc}{chapter}{%
%        \protect\chapternumberline{\thechapter}\f@rtoc}%
        \protect\chapternumberline{\hchaptertitlehead}\f@rtoc}%
%      \memchapinfo{\thechapter}{\f@rtoc}{\f@rhdr}{#2}%
       \memchapinfo{\hchaptertitlehead}{\f@rtoc}{\f@rhdr}{#2}%
    \fi
  \else
    \addcontentsline{toc}{chapter}{\f@rtoc}%
    \ifanappendix
      \memappchapinfo{}{\f@rtoc}{\f@rhdr}{#2}%
    \else
      \memchapinfo{\hchaptertitlehead}{\f@rtoc}{\f@rhdr}{#2}%
    \fi
  \fi
  \mempostaddchaptertotochook%
  \ifheadnameref\M@gettitle{\f@rhdr}\else\M@gettitle{\f@rtoc}\fi
%%% for chapter
  \ifanappendix
     \protected@edef\@currentlabel{\thechapter}%
  \fi
  \memendofchapterhook%
}

\def\@makechapterhead#1{%
  \chapterheadstart%  \vspace*{50\p@}%
  {\parindent \z@ \raggedright \normalfont
   \ifm@m@And
   	 \memucsinterwordchapterskiphook
%     \printchaptername \chapternamenum \printchapternum
%     \afterchapternum % \par\nobreak \vskip 20\p@
      \prechapternum \chapternamenum \printchapternum \chapternamenum \postchapternum
      \afterchapternum % \par\nobreak \vskip 20\p@
   \else
     \printchapternonum
   \fi
   \interlinepenalty\@M
   \printchaptertitle{#1} % \Huge \bfseries #1
   \afterchaptertitle % \par\nobreak \vskip 40\p@
  }}

\renewcommand{\@m@mschapter}[2][\@empty]{%
  \@schapter{#2}%
  \ifx \@empty#1
    \def\f@rhdr{#2}%
%    \PrerenderUnicode{#2}%
  \else   % opt arg
    \def\f@rhdr{#1}%
%    \PrerenderUnicode{#1}%
    \setcounter{secnumdepth}{-10}%
    \chaptermark{#1}%
    \setcounter{secnumdepth}{\value{maxsecnumdepth}}%
  \fi
  \ifanappendix
    \memappchapstarinfo{\f@rhdr}{#2}%
  \else
    \memchapstarinfo{\f@rhdr}{#2}%
  \fi}

\def\@makeschapterhead#1{%
  \chapterheadstart
%  {\parindent \z@ \raggedright \normalfont
  {\parindent \z@ \raggedright \normalfont \memucsinterwordchapterskiphook
   \printchapternonum
   \interlinepenalty\@M
   \printchaptertitle{#1}
   \afterchaptertitle
  }
}

%%%%%%%%%%%%%

\renewcommand{\@chs@def@ult}{%
 \def\chapterheadstart{\vspace*{\beforechapskip}}
%  \def\printchaptername{\chapnamefont \@chapapp}
% \def\printchaptername{\chapnamefont \pre@chapter}
 \def\printchaptername{} % disabled.
%  \def\chapternamenum{\space}
 \def\chapternamenum{\,}
 \def\printchapternum{\chapnumfont \thechapter}
 \def\prechapternum{\chapnamefont \pre@chapter}
 \def\postchapternum{\chapnamefont \post@chapter}
 \def\afterchapternum{\par\nobreak\vskip \midchapskip}
 \def\printchapternonum{}
 \def\printchaptertitle##1{\chaptitlefont ##1}
 \def\postchaptertitle{\post@chapter}
 \def\afterchaptertitle{\par\nobreak\vskip \afterchapskip}
}

%% l@chapter, l@part

\setlength{\cftpartnumwidth}{4.5em}
\setlength{\cftchapternumwidth}{4.0em}
%% for compatibility with mempatch 3.12
\def\@chapapp@head{}% 

%% SECTION

\renewcommand{\section}{%
  \sechook\memucsinterwordhook%
  \@startsection{section}{1}%  level 1
      {\secindent}%            heading indent
      {\beforesecskip}%        skip before the heading
      {\aftersecskip}%         skip after the heading
      {\normalfont\secheadstyle}} % font 

%% PAGESTYLES
%% headings, ruled, Ruled, companioin을 수정함

%% pagestyle headings
\if@twoside
  \makepagestyle{headings}
    \makepsmarks{headings}{%
      \let\@mkboth\markboth
      \def\chaptermark##1{%
        \markboth{\MakeUppercase{%
          \ifnum \c@secnumdepth >\m@ne
            \if@mainmatter
              \hchaptertitlehead \enskip\ %
            \fi
          \fi
          ##1}}{}}%
      \def\tocmark{\markboth{\MakeUppercase{\contentsname}}{}}%
      \def\lofmark{\markboth{\MakeUppercase{\listfigurename}}{}}%
      \def\lotmark{\markboth{\MakeUppercase{\listtablename}}{}}%
      \def\bibmark{\markboth{\MakeUppercase{\bibname}}{}}%
      \def\indexmark{\markboth{\MakeUppercase{\indexname}}{}}%
      \def\sectionmark##1{%
        \markright{\MakeUppercase{%
          \ifnum \c@secnumdepth > \z@
            \thesection \enskip\ %
          \fi
          ##1}}}%
    }
    \makeevenhead{headings}{\thepage}{}{\normalfont\slshape\leftmark}
    \makeoddhead{headings}{\normalfont\slshape\rightmark}{}{\thepage}
\else
  \makepagestyle{headings}
    \makepsmarks{headings}{%
      \let\@mkboth\markboth
      \def\chaptermark##1{%
        \markright{\MakeUppercase{%
          \ifnum \c@secnumdepth >\m@ne
            \if@mainmatter
              \hchaptertitlehead \enskip\ %
            \fi
          \fi
          ##1}}}%
      \def\tocmark{\markright{\MakeUppercase{\contentsname}}}%
      \def\lofmark{\markright{\MakeUppercase{\listfigurename}}}%
      \def\lotmark{\markright{\MakeUppercase{\listtablename}}}%
      \def\bibmark{\markright{\MakeUppercase{\bibname}}}%
      \def\indexmark{\markright{\MakeUppercase{\indexname}}}%
    }
    \makeoddhead{headings}{\normalfont\slshape\rightmark}{}{\thepage}
\fi 

%% Pagestyle ruled

\makepagestyle{ruled}
\makeevenfoot{ruled}{\thepage}{}{}
\makeoddfoot{ruled}{}{}{\thepage}
\makeheadrule{ruled}{\textwidth}{\normalrulethickness}
\renewcommand{\@ruledmarks}{%
  \let\@mkboth\markboth
  \def\chaptermark##1{%
    \markboth{%
      \ifnum \c@secnumdepth >\m@ne
        \if@mainmatter
          \hchaptertitlehead \enskip\ %
        \fi
      \fi
      ##1}{}}
  \def\sectionmark##1{\markright{##1}}
  \def\tocmark{\markboth{\contentsname}{}}
  \def\lofmark{\markboth{\listfigurename}{}}
  \def\lotmark{\markboth{\listtablename}{}}
  \def\bibmark{\markboth{\bibname}{}}
  \def\indexmark{\markboth{\indexname}{}}
}
\makepsmarks{ruled}{\@ruledmarks}
\makeevenhead{ruled}{\normalfont\scshape\leftmark}{}{}
\makeoddhead{ruled}{}{}{\normalfont\rightmark}  

%% Pagestyle Ruled

\makepagestyle{Ruled}
\makerunningwidth{Ruled}{1.1\textwidth}
\makeheadposition{Ruled}{flushright}{flushleft}{flushright}{flushleft}
\makeevenfoot{Ruled}{\thepage}{}{}
\makeoddfoot{Ruled}{}{}{\thepage}
\makeheadrule{Ruled}{1.1\textwidth}{\normalrulethickness}
\makepsmarks{Ruled}{\@ruledmarks}
\makeevenhead{Ruled}{\normalfont\scshape\leftmark}{}{}
\makeoddhead{Ruled}{}{}{\normalfont\rightmark}

%% Pagestyle companion

%\makepagestyle{companion}
%\setlength{\headwidth}{\textwidth}
%  \addtolength{\headwidth}{\marginparsep}
%  \addtolength{\headwidth}{\marginparwidth}
%\makerunningwidth{companion}{\headwidth}
%\makeheadrule{companion}{\headwidth}{\normalrulethickness}
%\makeheadposition{companion}{flushright}{flushleft}{}{}
%\makepsmarks{companion}{%
%  \let\@mkboth\markboth
%  \def\chaptermark##1{\markboth{##1}{##1}}    % left mark & right marks
%  \def\sectionmark##1{\markright{%
%    \ifnum \c@secnumdepth>\z@
%      \thesection. \ %
%    \fi
%    ##1}}
%  \def\tocmark{\markboth{\contentsname}{\contentsname}}
%  \def\lofmark{\markboth{\listfigurename}{\listfigurename}}
%  \def\lotmark{\markboth{\listtablename}{\listtablename}}
%  \def\bibmark{\markboth{\bibname}{\bibname}}
%  \def\indexmark{\markboth{\indexname}{\indexname}}
%}
%\makeevenhead{companion}{\normalfont\bfseries\thepage}{}%
%                        {\normalfont\bfseries\leftmark}
%\makeoddhead{companion}{\normalfont\bfseries\rightmark}{}%
%                       {\normalfont\bfseries\thepage} 

%% 한글 pagestyle hangul
\makepagestyle{hangul}
\newdimen\pghgheadwidth\let\pghgheadwidth=\textwidth
\makerunningwidth{hangul}{\pghgheadwidth}
%\makeheadrule{hangul}{\pghgheadwidth}{0pt}
\makeheadposition{hangul}{flushleft}{flushright}{flushleft}{flushright}
\def\@hgpsmarks{%
      \let\@mkboth\markboth
      \def\chaptermark##1{%
        \markboth{\scshape
          \ifnum \c@secnumdepth >\m@ne
            \if@mainmatter
              \hchaptertitlehead \enskip\ %
            \fi
          \fi
          ##1}{}}%
      \def\tocmark{\markboth{\scshape\contentsname}{}}%
      \def\lofmark{\markboth{\scshape\listfigurename}{}}%
      \def\lotmark{\markboth{\scshape\listtablename}{}}%
      \def\bibmark{\markboth{\scshape\bibname}{}}%
      \def\indexmark{\markboth{\scshape\indexname}{}}%
      \def\sectionmark##1{%
        \markright{\scshape
%% disabled printing \thesection.
%          \ifnum \c@secnumdepth >\z@
%            \thesection \enskip\ %
%          \fi
          ##1}}%
    }
\makepsmarks{hangul}{\@hgpsmarks}
\makeoddhead{hangul}{}{}{\normalfont\small\rightmark~\makebox[2em][r]{\normalfont\normalsize\sffamily\thepage}}
\makeevenhead{hangul}{\makebox[2em][l]{\normalfont\normalsize\sffamily\thepage}~\normalfont\small\leftmark}{}{}

%%% CHAPTERSTYLE

\makechapterstyle{default}{%
  \renewcommand{\post@chapter}{\chaptername}%
  \renewcommand{\chapnamefont}{\normalfont\huge\bfseries}
  \renewcommand{\chapnumfont}{\normalfont\huge\bfseries}
  \renewcommand{\chaptitlefont}{\normalfont\Huge\bfseries}
  \setlength{\beforechapskip}{50pt}
  \setlength{\midchapskip}{20pt}
  \setlength{\afterchapskip}{40pt}
}
\chapterstyle{default}

%% for Appendix
\makechapterstyle{appendixdefault}{%
  \renewcommand{\chapnamefont}{\normalfont\huge\bfseries}
  \renewcommand{\chapnumfont}{\normalfont\huge\bfseries}
  \renewcommand{\chaptitlefont}{\normalfont\Huge\bfseries}
%  \renewcommand{\printchaptername}{\chapnumfont\@chapapp}
  \renewcommand{\prechapternum}{\chapnumfont\@chapapp}
  \renewcommand{\postchapternum}{}
  \renewcommand{\chapternamenum}{\enskip}
  \setlength{\beforechapskip}{50pt}
  \setlength{\midchapskip}{20pt}
  \setlength{\afterchapskip}{40pt}
}

\makechapterstyle{section}{%
%  \renewcommand{\printchaptername}{}
  \renewcommand{\chapternamenum}{\enskip}
  \renewcommand{\prechapternum}{}
  \renewcommand{\postchapternum}{}
  \renewcommand{\chapnumfont}{\normalfont\Huge\bfseries}
  \renewcommand{\printchapternum}{\chapnumfont \thechapter\space}
  \renewcommand{\afterchapternum}{}
}

%% for appendix
\makechapterstyle{appendixsection}{%
%  \renewcommand{\printchaptername}{\chapnumfont\@chapapp}
  \renewcommand{\chapternamenum}{\enskip}
  \renewcommand{\prechapternum}{\chapnumfont\@chapapp}
  \renewcommand{\postchapternum}{}
  \renewcommand{\chapnumfont}{\normalfont\Huge\bfseries}
  \renewcommand{\printchapternum}{\chapnumfont \thechapter\space}
  \renewcommand{\afterchapternum}{}
}

\makechapterstyle{article}{%
  \renewcommand{\chapterheadstart}{\vspace{\beforechapskip}}
  \setlength{\beforechapskip}{3.5ex \@plus 1ex \@minus .2ex}
  \setlength{\afterchapskip}{2.3ex \@plus .2ex}
%  \renewcommand{\printchaptername}{}
  \renewcommand{\prechapternum}{}
  \renewcommand{\chapternamenum}{}
  \renewcommand{\postchapternum}{}
  \renewcommand{\chaptitlefont}{\normalfont\Large\bfseries}
  \renewcommand{\chapnumfont}{\normalfont\Large\bfseries}
  \renewcommand{\printchapternum}{\chapnumfont \thechapter\quad}
  \renewcommand{\afterchapternum}{}
}

\makechapterstyle{hangnum}{%
  \renewcommand{\chapnumfont}{\chaptitlefont}
  \settowidth{\chapindent}{\chapnumfont 999}
%  \renewcommand{\printchaptername}{}
  \renewcommand{\chapternamenum}{}
  \renewcommand{\prechapternum}{}
  \renewcommand{\postchapternum}{}
  \renewcommand{\printchapternum}{%
    \noindent\llap{\makebox[\chapindent][l]{\chapnumfont \thechapter}}}
  \renewcommand{\afterchapternum}{}
}

%\newlength{\chapindent}

\makechapterstyle{companion}{%
  \chapterstyle{default}
  \renewcommand*{\chapnamefont}{\normalfont\LARGE\scshape}
  \renewcommand*{\printchaptername}{\raggedleft\chapnamefont \@chapapp}
  \renewcommand*{\prechapternum}{\raggedleft\chapnamefont \pre@chapter}
  \renewcommand*{\chapnumfont}{\normalfont\Huge}
  \setlength{\chapindent}{\marginparsep}
  \addtolength{\chapindent}{\marginparwidth}
  \renewcommand*{\printchaptertitle}[1]{%
    \begin{adjustwidth}{}{-\chapindent}
      \raggedleft \chaptitlefont ##1\par\nobreak
    \end{adjustwidth}}} 

%% for appendix.
\makechapterstyle{appendixcompanion}{%
  \renewcommand{\chapnamefont}{\normalfont\LARGE\scshape}
  \renewcommand{\prechapternum}{\raggedleft\chapnamefont \@chapapp}
%  \renewcommand{\printchaptername}{\raggedleft\chapnamefont \pre@chapter}
  \renewcommand{\chapnumfont}{\normalfont\Huge}
  \renewcommand{\postchapternum}{}
  \setlength{\chapindent}{\marginparsep}
  \addtolength{\chapindent}{\marginparwidth}
  \renewcommand{\printchaptertitle}[1]{%
    \begin{adjustwidth}{}{-\chapindent}
      \raggedleft \chaptitlefont ##1\par\nobreak
    \end{adjustwidth}}
}

\makechapterstyle{demo}{
%  \renewcommand{\printchaptername}{\centering}
  \renewcommand{\prechapternum}{\centering}
  \renewcommand{\printchapternum}{\chapnumfont \numtoName{\c@chapter}}
  \renewcommand{\postchapternum}{}
  \renewcommand{\chaptitlefont}{\normalfont\Huge\sffamily}
  \renewcommand{\printchaptertitle}[1]{%
    \hrule\vskip\onelineskip \raggedleft \chaptitlefont ##1}
  \renewcommand{\afterchaptertitle}{\vskip\onelineskip \hrule\vskip \afterchapskip}
}

\makechapterstyle{demovar}{
%  \renewcommand{\printchaptername}{\centering}
  \renewcommand{\prechapternum}{\centering}
  \renewcommand{\printchapternum}{\chapnumfont \hNum{chapter}}
  \renewcommand{\postchapternum}{\post@chapter}
  \renewcommand{\chaptitlefont}{\normalfont\Huge\sffamily}
  \renewcommand{\printchaptertitle}[1]{%
    \hrule\vskip\onelineskip \raggedleft \chaptitlefont ##1}
  \renewcommand{\afterchaptertitle}{\vskip\onelineskip \hrule\vskip \afterchapskip}
}

%% for appendix
\makechapterstyle{appendixdemo}{
%  \renewcommand{\printchaptername}{\centering\chapnumfont\@chapapp}
  \renewcommand{\prechapternum}{\centering\chapnumfont\@chapapp}
  \renewcommand{\printchapternum}{\chapnumfont \@Alph\c@chapter}
  \renewcommand{\postchapternum}{\ }
  \renewcommand{\chaptitlefont}{\normalfont\Huge\sffamily}
  \renewcommand{\printchaptertitle}[1]{%
    \hrule\vskip\onelineskip \raggedleft \chaptitlefont ##1}
  \renewcommand{\afterchaptertitle}{\vskip\onelineskip \hrule\vskip \afterchapskip}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%% appendix %%%%%%%%%%%%%%%%%%%%%%%%%

\def\set@appendix@chapsec{%
   %%% SECTION in APPENDIX
   \renewcommand{\section}{%
   \sechook\memucsinterwordhook%
   \@startsection{section}{1}%  level 1
      {\secindent}%            heading indent
      {\beforesecskip}%        skip before the heading
      {\aftersecskip}%         skip after the heading
      {\normalfont\secheadstyle}} % font 
 \def\M@sect##1##2##3##4##5##6[##7][##8]##9{%
  \ifheadnameref\M@gettitle{##8}\else\M@gettitle{##7}\fi
  \ifnum ##2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{APP##1}%
    \protected@edef\@svsec{\@seccntformat{##1}\relax}%
  \fi
  \@tempskipa ##5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      ##6{%
      \@hangfrom{\hskip ##3\relax\@svsec}%
        \interlinepenalty \@M ##9\@@par}%
    \endgroup
    \csname ##1mark\endcsname{##8}%
    \addcontentsline{toc}{##1}{%
      \ifnum ##2>\c@secnumdepth \else
        \protect\numberline{\noexpand\protect\csname the##1\endcsname}%
      \fi
      ##7}%
  \else
    \def\@svsechd{%
      ##6{\hskip ##3\relax
     \@svsec ##9}%
     \csname ##1mark\endcsname{##8}%
     \addcontentsline{toc}{##1}{%
       \ifnum ##2>\c@secnumdepth \else
        \protect\numberline{\csname the##1\endcsname}%
       \fi
       ##7}}%
  \fi
  \@xsect{##5}}%
}

\def\restorechapsec{%
   %%% SECTION in APPENDIX
   \renewcommand{\section}{%
   \sechook\memucsinterwordhook%
   \@startsection{section}{1}%  level 1
      {\secindent}%            heading indent
      {\beforesecskip}%        skip before the heading
      {\aftersecskip}%         skip after the heading
      {\normalfont\secheadstyle}} % font 
 \def\M@sect##1##2##3##4##5##6[##7][##8]##9{%
  \ifheadnameref\M@gettitle{##8}\else\M@gettitle{##7}\fi
  \ifnum ##2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{##1}%
    \protected@edef\@svsec{\@seccntformat{##1}\relax}%
  \fi
  \@tempskipa ##5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      ##6{%
      \@hangfrom{\hskip ##3\relax\@svsec}%
        \interlinepenalty \@M ##9\@@par}%
    \endgroup
    \csname ##1mark\endcsname{##8}%
    \addcontentsline{toc}{##1}{%
      \ifnum ##2>\c@secnumdepth \else
        \protect\numberline{\noexpand\protect\csname the##1\endcsname}%
      \fi
      ##7}%
  \else
    \def\@svsechd{%
      ##6{\hskip ##3\relax
     \@svsec ##9}%
     \csname ##1mark\endcsname{##8}%
     \addcontentsline{toc}{##1}{%
       \ifnum ##2>\c@secnumdepth \else
        \protect\numberline{\csname the##1\endcsname}%
       \fi
       ##7}}%
  \fi
  \@xsect{##5}}
}

\renewcommand{\appendix}{\par
%  \setcounter{chapter}{0}%
%  \setcounter{section}{0}%
%  \gdef\@chapapp{\appendixname}%
%  \gdef\thechapter{\@Alph\c@chapter}%
  \set@appendix@chapter
  \anappendixtrue
  \chapterstyle{appendixdefault}%
}

\newcounter{APPchapter}\setcounter{APPchapter}{0}%
\newcounter{APPsection}[APPchapter]\setcounter{APPsection}{0}%
\newcounter{APPsubsection}[APPsection]\setcounter{APPsubsection}{0}%

\def\set@appendix@chapter{%
  \gdef\@chapapp{\appendixname}%
  \gdef\thechapter{\Alph{APPchapter}}%
  \gdef\thesection{\thechapter.\arabic{APPsection}}% 
%  \counterwithout{figure}{chapter}\counterwithout{table}{chapter}%
  \counterwithin{figure}{APPchapter}\counterwithin{table}{APPchapter}%
  \counterwithin{equation}{APPchapter}%
  \renewcommand\thefigure{\thechapter.\arabic{figure}}%
  \renewcommand\thetable{\thechapter.\arabic{table}}%
  \renewcommand\theequation{\thechapter.\arabic{equation}}%
  \set@appendix@chapsec
  \renewcommand\hchaptertitlehead{\appendixname\:\thechapter}%
}

%%% here, I will renewcommand chapterstyle,
%%% to recover chapterstyle after appendices.
\renewcommand{\chapterstyle}[1]{%
  \@nameuse{chs@#1}%
  \ifanappendix\else\gdef\CURR@chpstyle{#1}\fi
}

\def\restore@appendices@chapter{%
  \refreshprepostchapters
  \gdef\thechapter{\arabic{chapter}}%
  \gdef\thesection{\thechapter.\arabic{section}}%
  \def\hchaptertitlehead{\pre@chapter\:\thechapter}%
  \counterwithin{figure}{chapter}\counterwithin{table}{chapter}%
  \restorechapsec
  \@ifundefined{CURR@chapstyle}{\chapterstyle{default}}%
     {\expandafter\expandafter\chapterstyle{\CURR@chapstyle}}%
}

\renewcommand{\@resets@pp}{%
  \par
  \@ppsavesec
%  \setcounter{section}{0}%
%  \setcounter{chapter}{0}%
%  \renewcommand\@chapapp{\appendixname}%
%  \renewcommand\thechapter{\@Alph\c@chapter}%
  \set@appendix@chapter
  \chapterstyle{appendixdefault}
  \restoreapp
}

\renewenvironment{appendices}%
  {\@resets@pp\anappendixtrue}%
  {\@ppsaveapp\@pprestoresec\restore@appendices@chapter\anappendixfalse}

\renewenvironment{subappendices}{%
  \@resets@ppsub
  \def\addappheadtotoc{\phantomsection\addcontentsline{toc}{section}{\appendixtocname}} % <- mempatch 2.3
  \ifnamesubappendix
    \def\sectionname{\protect\subappendixname}%
    \def\@seccntformat##1{\@ifundefined{##1name}{}{\csname ##1name\endcsname\ }%
        \csname the##1\endcsname\quad}
  \fi
  }{} 

%%% References
\renewcommand\Cref[1]{%
   \pre@chapter\,\ref{#1}\,\chapterrefname%
}
\renewcommand\Pref[1]{%
   \pre@part\,\ref{#1}\,\partrefname%
}
\renewcommand\pref[1]{%
   \pageref{#1}~\pagerefname
}
\renewcommand\tref[1]{%
   \tablerefname~\ref{#1}%
}
\renewcommand\fref[1]{%
   \figurerefname~\ref{#1}%
}

%% arabic, roman... etc.
%%\def\HArabic#1{\arabic{#1}}
\def\HAlph#1{\Alph{#1}}
\def\Halph#1{\alph{#1}}
\def\HROMAN#1{\hRoman{#1}}
\def\Hroman#1{\hroman{#1}}
%%

%% phantomchapter = phantomsection
\def\phantomchapter{%
 \Hy@GlobalStepCount\Hy@linkcounter
 \xdef\@currentHref{chapter*.\the\Hy@linkcounter}%
 \Hy@raisedlink{\hyper@anchorstart{\@currentHref}\hyper@anchorend}%
}

%%% index
\renewcommand{\@wrindexm@m}[1]{\@@wrindexhyp#1||\\} 


%%% 한글 문장부호 관련 명령
%%% \hfontfamilynameprefix is deprecated. (ver1.5.0)
\def\hfontfamilynameprefix{ut} % for compatibility only

\def\cnm@char@open{〈}
\def\cnm@char@close{〉}
\def\ccnm@char@open{《}
\def\ccnm@char@close{》}
\def\snm@char@open{「}
\def\snm@char@close{」}
\def\bnm@char@open{『}
\def\bnm@char@close{』}

\def\memx@make@bnmcmd#1#2{%
	\leavevmode
	\unhbox0\memx@bnmcmd@prekern\csname #1@char@open\endcsname
	#2\csname #1@char@close\endcsname\memx@bnmcmd@postkern
}

\protected\def\cnm{%
	\@ifnextchar*\cnm@star\cnm@nostar
}

\def\cnm@star*#1{% 
	\def\memx@bnmcmd@prekern{\kern-.35em}%
	\def\memx@bnmcmd@postkern{\kern-.4em}%
	\memx@make@bnmcmd{cnm}{#1}%
}

\def\cnm@nostar#1{%
    \let\memx@bnmcmd@prekern\relax
    \let\memx@bnmcmd@postkern\relax
	\memx@make@bnmcmd{cnm}{#1}%
}

\protected\def\ccnm{%
	\@ifnextchar*\ccnm@star\ccnm@nostar
}

\def\ccnm@star*#1{%
	\def\memx@bnmcmd@prekern{\kern-.35em}%
	\def\memx@bnmcmd@postkern{\kern-.4em}%
	\memx@make@bnmcmd{ccnm}{#1}%
}

\def\ccnm@nostar#1{%
    \let\memx@bnmcmd@prekern\relax
    \let\memx@bnmcmd@postkern\relax
	\memx@make@bnmcmd{ccnm}{#1}%
}


\protected\def\snm{%
	\@ifnextchar*\snm@star\snm@nostar
}

\def\snm@star*#1{%
	\def\memx@bnmcmd@prekern{\kern-.35em}%
	\def\memx@bnmcmd@postkern{\kern-.4em}%
	\memx@make@bnmcmd{snm}{#1}%
}

\def\snm@nostar#1{% 
    \let\memx@bnmcmd@prekern\relax
    \let\memx@bnmcmd@postkern\relax
	\memx@make@bnmcmd{snm}{#1}%
}


\protected\def\bnm{%
	\@ifnextchar*\bnm@star\bnm@nostar
}

\def\bnm@star*#1{% 
	\def\memx@bnmcmd@prekern{\kern-.35em}%
	\def\memx@bnmcmd@postkern{\kern-.4em}%
	\memx@make@bnmcmd{bnm}{#1}%
}

\def\bnm@nostar#1{%
    \let\memx@bnmcmd@prekern\relax
    \let\memx@bnmcmd@postkern\relax
	\memx@make@bnmcmd{bnm}{#1}%
}

\AtBeginDocument{
  \@ifpackageloaded{hyperref}{%
    \pdfstringdefDisableCommands{%
         \def\cnm#1{\ifx#1*\expandafter\@@cnm\else <#1>\fi}
	  \def\@@cnm#1{<#1>}
	  \def\snm#1{\ifx#1*\expandafter\@@cnm\else <#1>\fi}
	  \def\ccnm#1{\ifx#1*\expandafter\@@cnm\else <#1>\fi}
	  \def\bnm#1{\ifx#1*\expandafter\@@cnm\else <#1>\fi}
	  \def\oblivoirdblquote#1{\ifx#1*\expandafter\@@obquote\else "#1"\fi}
	  \def\@@obquote#1{"#1"}
	  \def\oblivoirquote#1{\ifx#1*\expandafter\@@obsquote\else '#1'\fi}
	  \def\@@obsquote#1{'#1'}
	  \def\cntrdots{…}
    }%
  }{}
} 

%%%% 따옴표
\def\oblivoirdblquote@char@open{“}
\def\oblivoirdblquote@char@close{”}
\def\oblivoirquote@char@open{‘}
\def\oblivoirquote@char@close{’}

\protected\def\oblivoirdblquote{%
	\@ifnextchar*\oblivoirdblquote@star\oblivoirdblquote@nostar
}

\def\oblivoirdblquote@star*#1{% 
	\def\memx@bnmcmd@prekern{\kern-.35em}%
	\def\memx@bnmcmd@postkern{\kern-.4em}%
	\memx@make@bnmcmd{oblivoirdblquote}{#1}%
}

\def\oblivoirdblquote@nostar#1{%
    \let\memx@bnmcmd@prekern\relax
    \let\memx@bnmcmd@postkern\relax
	\memx@make@bnmcmd{oblivoirdblquote}{#1}%
}

\protected\def\oblivoirquote{%
	\@ifnextchar*\oblivoirquote@star\oblivoirquote@nostar
}

\def\oblivoirquote@star*#1{% 
	\def\memx@bnmcmd@prekern{\kern-.35em}%
	\def\memx@bnmcmd@postkern{\kern-.4em}%
	\memx@make@bnmcmd{oblivoirquote}{#1}%
}

\def\oblivoirquote@nostar#1{%
    \let\memx@bnmcmd@prekern\relax
    \let\memx@bnmcmd@postkern\relax
	\memx@make@bnmcmd{oblivoirquote}{#1}%
}

%%%% cntrdots

\ifx\cntrdot\undefined
\DeclareRobustCommand{\cntrdot}{%
 	\@ifnextchar*\@cntrd@t\@@cntrd@t
}
\else
\renewcommand{\cntrdot}{%
 	\@ifnextchar*\@cntrd@t\@@cntrd@t
}
\fi

\ifx\oblivoirallowbreak\undefined
\let\oblivoirallowbreak\allowbreak
\fi

\def\@cntrd@t*{%
	\leavevmode\kern.2em\@@cntrd@t\hskip.2em\oblivoirallowbreak
}
\def\@@cntrd@t{%
    \leavevmode\nobreak\raise.02ex\hbox{·}\oblivoirallowbreak
}

\DeclareRobustCommand{\cntrdots}{%
    \leavevmode…%\oblivoirallowbreak
}

\DeclareRobustCommand{\cntrdotss}{%
	\leavevmode ……%
}

%% Punctuation Explanation Rule.
%% usage: \explpunc.Some_running_texts.\ % last space must not be succeeded by ^M.
\protected\def\explpunc.#1.\ {\leavevmode\,\XBrule#1\XErule\,}
\protected\def\expldash{\leavevmode\,\XBrule\,}
%
\newsavebox\togetheightoffont\sbox\togetheightoffont{!}
\newlength\htoffnt\setlength\htoffnt{\ht\togetheightoffont}
\addtolength\htoffnt{\dp\togetheightoffont}
\def\XBrule{\raise.45\htoffnt\hbox{\rule{1.25em}{.25pt}}}
\def\XErule{\raise.45\htoffnt\hbox{\hskip1pt\rule{1.25em}{.25pt}}}

%% Misc def.
%\def\PageName{페이지}
%\def\AltPageName{쪽}

%% \titleref
%%    -- 2009/02/11. revert to original definition.
\let\M@TitleReference\@firstoftwo 
\let\M@M@TitleReference\@firstoftwo
\def\@mem@theTR{\let\M@M@TitleReference\@firstoftwo\theTitleReference}

%% vertical distance of math display
\if@mathdisp
\addtodef{\normalsize}{}{\abovedisplayskip 6\p@ \@plus2\p@ \@minus5\p@
   \abovedisplayshortskip \z@ \@plus3\p@
   \belowdisplayshortskip 3\p@ \@plus3\p@ \@minus3\p@
   \belowdisplayskip \abovedisplayskip}
\fi

%% arabic frontmatter numbering
\if@arabicfrontmatter
\renewcommand{\@memfront}{%
  \@smemfront\pagenumbering{arabic}}
\fi

%% default pagestyle
\if@defaulthangulpagestyle
 \pagestyle{hangul}
\fi

%% 할주.
%%  still experimental
%%  \hbox로 처리하기 때문에 행끝에서 할주 행나눔은 이루어지지 않는다.
%%  글꼴이 \tiny로 고정. \footnotesize를 쓰면 행간이 흐트러짐.
%%  TODO: 현재 설정은 10pt 문서에서 1pt정도가 행간에 추가됨.
%%  \lineskiplimit를 이용해서 행간을 고정시켜둘 것인지는 아직
%%  결정하지 못하였음.
\newskip\divnoteskip
\def\divnotedelimopen{\hskip.1em$\big($}
\def\divnotedelimclose{$\big)$\hskip.1em}
\divnoteskip = .18em plus .02em minus .02em
\def\divnotestyle{%
 \spaceskip\divnoteskip\normalfont\tiny}
\protected\def\divnote#1{%
  \settowidth{\@tempdima}{\divnotestyle #1}%
  \ifvmode\leavevmode\fi\divnotedelimopen
  \raise.2em\hbox{\parbox{.525\@tempdima}{\divnotestyle\singlespace #1}}\divnotedelimclose
}

%% 각주 설정을 위한 추가 매크로
\def\SetFnmark#1#2{%
   \expandafter\def\csname @makefnmark\endcsname{\bgroup #1\@thefnmark#2\egroup}%
}

%% chapter의 첫 단락 들여쓰기.
\newcommand*\chapterindentfirst{%
  \addtodef{\@afterheading}{\@afterindenttrue}{}
}

%% memhangul-patch
\InputIfFileExists{memhangul-patch.sty}{}{}

%%% fig, tab caption
\def\obCaptionFont#1{\def\@figtabcaptfont@{#1}}
\providecommand\@figtabcaptfont@{\normalfont}
\if@figtabcapt@
\AtBeginDocument{
\@ifpackageloaded{caption}{%  requested by Progress
	\DeclareCaptionLabelFormat{xob-cnm-parens}{\cnm{#1~#2}}
	\captionsetup{labelformat=xob-cnm-parens,labelsep=space}
}%
{%
	\precaption{{\@figtabcaptfont@\cnm@char@open}}
	\captiondelim{{\@figtabcaptfont@\cnm@char@close}\quad}
	\renewcommand\cfttablepresnum{\cnm@char@open\tablename\space}
	\renewcommand\cfttableaftersnum{\cnm@char@close}
	\setlength\cfttablenumwidth{3.8em}
	\renewcommand\cftfigurepresnum{\cnm@char@open\figurename\space}
	\renewcommand\cftfigureaftersnum{\cnm@char@close}
	\setlength\cftfigurenumwidth{4.5em}
}
}
\fi

%%% LuaTeX, XeTeX logos
\RequirePackage{xparse}
\ExplSyntaxOn
\tl_if_exist:NF \XeTeX
{
	\RequirePackage{hologo}
	\ProvideDocumentCommand \XeTeX { } { \hologo{XeTeX} }
	\ProvideDocumentCommand \XeLaTeX { } { \hologo{XeLaTeX} }
}
\tl_if_exist:NF \LuaTeX
{
	\RequirePackage{hologo}
	\ProvideDocumentCommand \LuaTeX { } { \hologo{LuaTeX} }
	\ProvideDocumentCommand \LuaLaTeX { } { \hologo{LuaLaTeX} }
}
\ExplSyntaxOff

\endinput

%%% 2015/10/15: \@part, \@chapter renewal.
%%% 2015/08/07: default chapter style, postchapternum (hoze & gromov)
