% --------------- identification ----------------
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{PNS}[2003/02/14]
% --------------- initial code ---------------
\RequirePackage{ifthen,calc,xspace,verbatim,array}

\newboolean{PNSMath}
\setboolean{PNSMath}{false}
\newboolean{PNSStyle}
\setboolean{PNSStyle}{true}
\newboolean{PNSApple}
\setboolean{PNSApple}{true}
\newboolean{PNSGraphics}
\setboolean{PNSGraphics}{true}
\newcommand{\theTitle}{set theTitle}
\newcommand{\theDate}{\@date}

% --------------- declaration of options ---------------
\DeclareOption{math}{\setboolean{PNSMath}{true}}
\DeclareOption{nostyle}{\setboolean{PNSStyle}{false}}
\DeclareOption{noapple}{\setboolean{PNSApple}{false}}
\DeclareOption{headers}{%
	\AtEndOfPackage{\RequirePackage{fancyhdr}
		\renewcommand{\footrulewidth}{0.4pt}
		\fancyfoot[LE,RO]{\theTitle}
		\fancyfoot[RE,LO]{\theDate}
%		\pagestyle{fancy} if this is in, then you can't fix the 
		% header width; you have to set fancy within the document
		\thispagestyle{plain}
		\newcommand{\theCourse}{}
		\newcommand{\theTerm}{}
		\newcommand{\theNumber}{1}

		\newcommand{\SetHeaders}{%
			\fancyhead{} % clear all fields
			\fancyhead[LO,RE]{\textit{\theTitle}}%
			\fancyhead[RO,LE]{\textit{\theDate}}%
			\fancyfoot[LO,RE]{\textit{\theCourse}}%
			\fancyfoot[RO,LE]{\textit{\theTerm}}%
			}
	}}
\DeclareOption{problems}{\AtEndOfPackage{\RequirePackage{PNSProblem}}}
\DeclareOption{nographics}{\setboolean{PNSGraphics}{false}}
\DeclareOption{code}{%
	\AtEndOfPackage{\RequirePackage{moreverb}}}
% --------------- execution of options ---------------
\ProcessOptions
% --------------- package loading ---------------
\ifthenelse{\boolean{PNSMath}}{\RequirePackage{amsmath,amssymb,array}}{}
\ifthenelse{\boolean{PNSGraphics}}{\RequirePackage{epsfig}}{}
\ifthenelse{\boolean{PNSApple}}{\RequirePackage[applemac]{inputenc}}{}

% --------------- main code ---------------


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                   %
%   Redefine basic formatting commands to use sans-serif titles     %
%   in section headings, description list labels, and provide the   %
%   \umph{} command for emphasis.                                   %
%                                                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ifthenelse{\boolean{PNSStyle}}{
	\RequirePackage{indentfirst}
	\makeatletter
	\renewcommand{\section}{\@startsection
		{section}%						% the name
		{1}%							% the level
		{0mm}%							% the indent
		{-\baselineskip}%				% the beforeskip
		{0.5\baselineskip}%				% the afterskip
		{\sffamily\bfseries\large}}%	% the style

	\renewcommand{\subsection}{\@startsection
		{subsection}%					% the name
		{2}%							% the level
		{0mm}%							% the indent
		{-\baselineskip}%				% the beforeskip
		{0.5\baselineskip}%				% the afterskip
		{\sffamily\bfseries}}%			% the style

	\renewcommand{\subsubsection}{\@startsection
		{subsubsection}%				% the name
		{3}%							% the level
		{0mm}%							% the indent
		{-\baselineskip}%				% the beforeskip
		{0.5\baselineskip}%				% the afterskip
		{\small\slshape\sffamily}}%		% the style
	\makeatother

	\renewcommand{\thesection}{\arabic{section}.}
	\renewcommand{\thesubsection}{\thesection\arabic{subsection}}
	\renewcommand{\descriptionlabel}[1]%
		{\hspace{\labelsep}\umph{#1}}
	}{}

\providecommand{\umph}[1]{\textbf{\textsf{#1}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                            %
%   Page layout support. I often want to specify the width   %
%   and height of the text area. This macro automatically    %
%   recalculates the appropriate margins.                    %
%                                                            %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\SetPrintArea}[2]{% width, height
	\setlength{\textwidth}{#1} %
	\setlength{\textheight}{#2} %
	\setlength{\oddsidemargin}{(\paperwidth-#1-2in)/2} %
	\setlength{\evensidemargin}{\oddsidemargin}
	\setlength{\topmargin}{(\paperheight-2in-#2-\footskip-%
		\headheight-\headsep)/2}
%	\setlength{\headwidth}{\textwidth}
}

\SetPrintArea{6in}{8.5in}	% the default page size I use

\newcommand{\spacing}[1]{%		macro to set the line spacing
%								to double space, for example
	\renewcommand{\baselinestretch}{#1}\small\normalsize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                              %
%   Following are some shortcuts to save typing. I prefer      %
%   to use the \np macro instead of \newpage, so that I can    %
%   redefine to a newline when I want to save paper.           %
%   \ig saves typing when including an Encapsulated            %
%   PostScript graphic file:                                   %
%   \ig[.67]{blah} would include the file `blah.epsf' scaled   %
%   to 67%                                                     %
%                                                              %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\bc}{\begin{center}}
\newcommand{\ec}{\end{center}}
\newcommand{\np}{\newpage}
\newcommand{\noi}{\noindent}
\newcommand{\ds}{\displaystyle}
\newcommand{\ig}[2][1]{\includegraphics[scale=#1]{#2.epsf}}

%%%%%%%%%%%%%%%%%%%
%                 %
%   Mathematics   %
%                 %
%%%%%%%%%%%%%%%%%%%

\newcommand{\PNSSymbol}[1]{\ensuremath{#1}\xspace}	% generic symbol

\ifthenelse{\boolean{PNSMath}}{
\DeclareMathOperator{\sech}{sech}
\DeclareMathOperator{\csch}{csch}


\DeclareMathOperator{\erf}{erf}
\DeclareMathOperator{\erfc}{erfc}
}{}

\newcommand{\ii}{\ensuremath{i}}		% imaginary unit
\newcommand{\DD}{\ensuremath{d}}		% differential d without leading space
\newcommand{\dd}{\ensuremath{\,\DD}}	% differential d
\newcommand{\ddd}{\PNSSymbol{\delta}} 	% inexact differential
%\newcommand{\deriv}[2]{\ensuremath{\frac{\DD #1}{\DD #2}}}
\newcommand{\deriv}[3][]{\ensuremath{%
	\ifthenelse{\equal{#1}{}}{\frac{\DD #2}{\DD #3}}
	{\frac{\DD^{#1} #2}{\DD #3^{#1}}}}}
\newcommand{\pd}[3][]{\ensuremath{%
	\ifthenelse{\equal{#1}{}}{\frac{\partial #2}{\partial #3}}
	{\partial #2 / \partial #3}}}
\newcommand{\pdc}[3]{\ensuremath{\left(\frac{\partial #1}{\partial #2}}\right)_{#3}}
\newcommand{\pdn}[3][2]{\ensuremath{%
	\frac{\partial^{#1} #2}{\partial #3^{#1}}}}


\newcommand{\gives}{\ensuremath{\quad\text{\ding{223}}\quad}}
\newcommand{\half}{\mbox{\ensuremath{\scriptstyle{\frac{1}{2}}}}\xspace}

\newcommand{\RHS}{\PNSSymbol{\text{RHS}}}
\newcommand{\LHS}{\PNSSymbol{\text{LHS}}}

\newcommand{\Order}[1]{\ensuremath{\mathcal{O}(#1)}}	% big O notation


\newcommand{\infint}{\ensuremath{\int_{-\infty}^{\infty}}}
\newcommand{\sinfint}{\ensuremath{\int_{0}^{\infty}}}
\newcommand{\REAL}{\Re}
\newcommand{\IMAG}{\Im}
\newcommand{\tensorprod}{\otimes}


%%%%%%%%%%%%%%%
%             %
%   Physics   %
%             %
%%%%%%%%%%%%%%%

\newcommand{\val}[2]{\ensuremath{#1~\mathrm{#2}}\xspace}	% number plus unit

\newcommand{\dg}[1]{\ensuremath{#1^{\circ}}}
\newcommand{\degC}[1]{\ensuremath{#1^{\circ}\mathrm{C}}}
\newcommand{\degF}[1]{\ensuremath{#1^{\circ}\mathrm{F}}}

\renewcommand{\vector}[1]{\ensuremath{\vec{\mathbf{#1}}}}
\newcommand{\gvector}[1]{\ensuremath{\vec{\boldsymbol{#1}}}}

\newcommand{\mper}{}	% a period in an equation
\newcommand{\mcom}{}	% a comma in an equation

% Quantum mechanics

\newcommand{\ket}[1]{\PNSSymbol{|#1\rangle}}
\newcommand{\bra}[1]{\PNSSymbol{\langle#1|}}

% Statistical mechanics

\newcommand{\NA}{\PNSSymbol{N_{\rm A}}}			% Avogadro's number
\newcommand{\nos}{\PNSSymbol{\Omega}}			% number of states
\newcommand{\kb}{\PNSSymbol{k_{\rm B}}} 		% Boltzmann's constant
\newcommand{\kT}{\PNSSymbol{\kb T}}				% kT
\newcommand{\hw}{\PNSSymbol{\hbar\omega}}		% hbar omega

\newcommand{\msolar}{\PNSSymbol{M_{\odot}}}		% solar mass
\newcommand{\rsolar}{\PNSSymbol{R_{\odot}}}		% solar radius

% Mechanics

\newcommand{\ux}{\PNSSymbol{\mathbf{\hat{x}}}}	% unit vectors
\newcommand{\uy}{\PNSSymbol{\mathbf{\hat{y}}}}
\newcommand{\uz}{\PNSSymbol{\mathbf{\hat{z}}}}
\newcommand{\ut}{\PNSSymbol{\boldsymbol{\hat{\theta}}}}
\newcommand{\ur}{\PNSSymbol{\mathbf{\hat{r}}}}

\newcommand{\tdd}{\PNSSymbol{\ddot{\theta}}}
\newcommand{\qd}{\PNSSymbol{\dot{\theta}}}
\newcommand{\qdd}{\PNSSymbol{\ddot{\theta}}}
\newcommand{\rd}{\PNSSymbol{\dot{r}}}
\newcommand{\rdd}{\PNSSymbol{\ddot{r}}}
\newcommand{\xd}{\PNSSymbol{\dot{x}}}
\newcommand{\xdd}{\PNSSymbol{\ddot{x}}}
\newcommand{\yd}{\PNSSymbol{\dot{y}}}
\newcommand{\ydd}{\PNSSymbol{\ddot{y}}}
\newcommand{\zd}{\PNSSymbol{\dot{z}}}
\newcommand{\zdd}{\PNSSymbol{\ddot{z}}}

\newcommand{\into}{\ensuremath{\hat{\otimes}}}	% direction symbols
\newcommand{\outof}{\ensuremath{\hat{\odot}}}
\newcommand{\so}{\ensuremath{\qquad\longrightarrow\qquad}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                            %
%   Typography definitions   %
%                            %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\eref}[2][]{%
	\ifthenelse{\equal{#1}{}}%
	{Eq.~(\ref{eq:#2})}%
	{Equation~(\ref{eq:#2})}\xspace}

\newcommand{\fref}[2][]{%
  \ifthenelse{\equal{#1}{}}%
    {Fig.~\ref{fig:#2}}%
	{Figure~\ref{fig:#2}}}%   \ensuremath{\mathrm{Fig.}~(\ref{#1})}}
\newcommand{\pgref}[1]{p.~\pageref{#1}\xspace}
\newcommand{\sref}[1]{Sec.~\ref{sec:#1}\xspace}
\newcommand{\tref}[1]{Table~\ref{tbl:#1}\xspace}
\newcommand{\cref}[1]{Chapter~\ref{chap:#1}\xspace}
\newcommand{\aref}[1]{Appendix~\ref{ap:#1}\xspace}
\newcommand{\prref}[1]{Problem~\ref{prob:#1}\xspace}

\newcommand{\fnref}[1]{footnote~\ref{fn:#1}}
	
\newcommand{\eg}{\textit{e.g.}\xspace}
\newcommand{\ie}{\textit{i.e.}\xspace}

\newenvironment{Quotation}[1][xx]{%
	\sbox{\tempbox}{#1}\begin{quotation}\slshape}%
	{\ifthenelse{\lengthtest{\wd\tempbox < 0.2in}}{}{%
	\par\medskip\raggedleft{\upshape\usebox{\tempbox}}}\end{quotation}}


\newcommand{\logo}{\ig[.33]{logo}}

\newcommand{\masttitle}[2]{%
\parbox{1.5in}{\bc\logo \\ \small\textsl{\theCourse}\ec}%
\parbox{4in}{%
\begin{center}{\Large\textbf{\textsf{#1}} \\[.1in]\normalsize #2}%
\end{center}}
\\[.15in]
}

\makeatletter
\newcommand{\mymaketitle}[1][]{{\Large\sffamily\bfseries
	\ifthenelse{\equal{#1}{}}{\bc \@title \par \normalsize\normalfont \@date \ec}
	{
	\begin{flushleft}
		\@title\par\normalsize\normalfont\@date\par
	\end{flushleft}
	}}
}
\makeatother

\newcommand{\mytitle}[1]{\begin{center}{\LARGE \textbf{\textsf{#1}}}\end{center}}

%%%%%%%%%%%%%%%%
%              %
%   Graphics   %
%              %
%%%%%%%%%%%%%%%%

\newlength{\rptmplen}
\newcommand{\rightpic}[3][2in]{\medskip\noi%
	\setlength{\rptmplen}{\linewidth-#1}%
	\parbox{\rptmplen}{#2}%
	\parbox{#1}
	{\hspace{.2in} #3 }
	\medskip}
