%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Sections 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\usepackage{titlesec}
\makeatletter

%\newcommand{\makememo}{%
%	\pagestyle{memo}%
%	\renewcommand{\thesection}{\@arabic\c@section}%
%}
\newcommand{\makememo}{% 15 July 2015
	\pagestyle{memo}%
	\thispagestyle{startchap}% 
	\renewcommand{\thesection}{\@arabic\c@section}%
}

\def\SectionName{\relax}
%\def\Notes{\pagestyle{notes}\section*{Notes}}
\def\Notes{%
	\section*{Notes}%
%	\mark{{Notes for Chapter \ChapterNumber}}%
	\gdef\SectionName{Notes for \ChapTitle/ \ChapterNumber}
	\normalfalse
}

\@addtoreset{section}{Chapter}
%\@addtoreset{section}
%\def\Sref#1{\ref{Sect.#1}}
%\let\sref=\Sref

\def\sect@star{\relax}

%\def\star@dottedtocline#1#2#3#4#5{%
%  \ifnum #1>\c@tocdepth \else
%    \vskip \z@ \@plus.2\p@
%    {\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
%     \parindent #2\relax\@afterindenttrue
%     \interlinepenalty\@M
%     \leavevmode
%     \@tempdima #3\relax
%     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
%     {\llap{*}#4}\nobreak		% note the star
%     \leaders\hbox{$\m@th
%        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
%        mu$}\hfill
%     \nobreak
%     \hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor #5}%
%     \par}%
%  \fi}

%\renewcommand{\@seccntformat}[1]{\llap{\sect@star\ChapterNumber.\csname the#1\endcsname\qquad}}

% DBP 14 jan 2012  cf. GMS page 26
% With book style the section number includes the chapter number
%%21april2014 \renewcommand{\@seccntformat}[1]{\llap{\sect@star\csname the#1\endcsname\qquad}}
%\newcounter{starsection}[Chapter]
%\@addtoreset{starsection}{Chapter}
%\newcommand{\starsection}{\@startsection
%						 {starsection}%     the name
%						 {1}%	the level
%						 {\z@}%	the indent
%                         {-3.5ex \@plus -1ex \@minus -.2ex}%	the beforeskip
%                         {2.3ex \@plus.2ex}%	the afterskip
%                         {\normalfont\Large\bfseries}}%		the style  
%\renewcommand{\thestarsection}{\theChapter.\@arabic\c@starsection}
%\renewcommand{\thestarsection}{\thechapter.\@arabic\c@starsection}
%\let\starsectionmark=\sectionmark
%\newcommand*\l@starsection{\star@dottedtocline{1}{1.5em}{2.3em}}
%\let\l@starsection=\l@section             
% from book.cls
%\newcommand\section{\@startsection {section}{1}{\z@}%
%                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
%                                   {2.3ex \@plus.2ex}%
%                                   {\normalfont\Large\bfseries}}
%\newcommand*\l@section{\@dottedtocline{1}{1.5em}{2.3em}}
%\let\c@starsection = \c@section
%\let\p@starsection= \p@section
%\let\thestarsection = \thesection
%\def\thestarsection{*\thesection}
%\def\Section[#1]#2{\section{#2}\label{Sect.#1}}
%\def\SectionName{\relax}

%\def\Section[#1]#2{%
%	\gdef\SectionName{#2}%
%	\def\sect@star{\relax}%
%	\section{#2}%
%	\label{\the\tok@Chap::#1}%
%}
% 21 April 2014
%\titlelabel{\llap{\sect@star\thesection\qquad}}
% 13 aug 2014
%\titleformat*{\section}{\llap{\sect@star\thesection\qquad}}
%\titleformat*{\subsection}{\llap{\thesection.\thesubsection\qquad}}
\titlelabel{\llap{\sect@star\thetitle\qquad}}


%\long\def\@writefile#1#2{%
%  \@ifundefined{tf@#1}\relax
%    {\@temptokena{#2}%
%     \immediate\write\csname tf@#1\endcsname{\the\@temptokena}%
%    }%
%}
%\newcommand\section{\@startsection {section}{1}{\z@}%
%                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
%                                   {2.3ex \@plus.2ex}%
%                                   {\normalfont\Large\bfseries}}

\def\Section[#1]#2{%
	\gdef\SectionName{#2}%
	\def\sect@star{\relax}%
	\section{#2}
%	\label{\the\tok@Chap#1}%
%	\label{\the\tok@Chap#1}%
	\label{\the\tok@Chap\the\tok@Sect#1}%
	\if@GLOBAL
		%\edef\tmp@gbl{\string\gslabel{\the\tok@Chap#1}{\@currentlabel}{\thepage}}%
\edef\tmp@gbl{\string\gslabel{\the\tok@Chap\the\tok@Sect#1}{\@currentlabel}{\thepage}}%
		\protected@write\@auxout{}{\string\@writefile{gbl}{\tmp@gbl}}%
	\fi
%	\label{\the\tok@Chap..#1}%  for hyperref
	\check@for@SN@abbrev
}

\newcommand\starsection{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\Large\bfseries}}

%\newcommand{\starsection}[1]{%
%%	\addtocounter{section}{1}%
%%\addcontentsline{toc}{section}{\llap{*}\thesection\hspace{1em}#1}%
%\addcontentsline{toc}{section}{\texorpdfstring{\llap{*}\thesection\hspace{1em}}{*}#1}%
%	{\vspace{1.5em}\par\noindent\bfseries\Large \llap{*\thesection\qquad}#1}\par\vspace{1em}\noindent%
%}

\def\starSection[#1]#2{%
%	\addtocounter{section}{1}%
	\gdef\SectionName{#2}%
	\def\sect@star{*}%
%	\setcounter{starsection}{\value{section}}%
%	\stepcounter{section}%
	\starsection{*#2}%
%	\section{#2}%
%	\section{#2}%
	\label{\the\tok@Chap\the\tok@Sect#1}%
%	\label{#1}%
	\if@GLOBAL
		\edef\tmp@gbl{\string\gslabel{\the\tok@Chap\the\tok@Sect#1}{\thesection}{\thepage}}%
		\protected@write\@auxout{}{\string\@writefile{gbl}{\tmp@gbl}}%
	\fi
%	\label{\the\tok@Chap--#1}%  hyperref doesn't seem to like ::
	\check@for@SN@abbrev
}

\def\check@for@SN@abbrev{\@ifnextchar[{\resetSN}{\relax}}

\def\resetSN[#1]{\def\SectionName{#1}}

\def\gslabel#1#2#3{\expandafter\gdef\csname#1\endcsname{#2}}
\def\gsref#1{\csname#1\endcsname}
%%\let\starSection=\Section
%\def\starSection[#1]#2{%
%	\gdef\SectionName{#2}%
%	\def\sect@star{*}%
%	\starsection{#2}%
%	\section{#2}%
%	\label{\the\tok@Chap::#1}%
%	\check@for@SN@abbrev
%}



\def\Sref{%
	\@ifnextchar[{\@Sref}%	
	{\@Sref[\the\tok@Chap]}%
}
\def\@Sref[#1]#2{\ref{#1::#2}}
%\def\@Sref[#1]#2{\ref{#1--#2}}%  hyperref trouble
%\let\sref=\Sref
\def\sref#1{\ref{\the\tok@Chap\the\tok@Sect#1}}

\def\GSREF#1#2{\csname#1::S:#2\endcsname}

%\def\starSection[#1]#2{\section*{#2}\label{Sect.#1}}

\def\beginPreamble{\bgroup\itshape}
\def\endPreamble{\bigskip\egroup}

%\def\SectionDescribe#1{\par\hangindent\parindent\hangafter1\noindent
%\expandafter SECTION~{\ref{Sect.#1}}
%% 4 april 2014: seems to mess with hyperref
\def\SectionDescribe#1{\par
\hangindent\parindent\hangafter1\noindent
{\scshape Section}~{\ref{\the\tok@Chap\the\tok@Sect#1}}}
%{\scshape Section}~{\ref{#1}}}

%\def\SectionDescribe#1{Section~\sref{#1}}


\def\starSectionDescribe#1{\par
\hangindent\parindent\hangafter1\noindent
\expandafter {\scshape \llap{*}Section}{\ref{\the\tok@Chap\the\tok@Sect#1}}}
%\expandafter {\scshape \llap{*}Section}~{\ref{#1}}}

%\def\PreambleSref#1{\getcref{Sect.#1}\crefsection}
%
%\def\beginPreamble{%
%\bgroup\SDfont\baselineskip=11.5pt\let\sref=\PreambleSref}
%\def\endPreamble{\bigskip\egroup}

\makeatother
