
% baabaas environment for definition boxes
\usepackage[top=20mm,bottom=20mm,right=20mm,left=20mm]{geometry}
\usepackage{pgffor}
\usepackage{setspace}
\usepackage{mathtools}
\usepackage{enumerate}
\usepackage{tabularx}
\usepackage{multicol}
\usepackage{background}
\usepackage{cancel}
\usepackage{fontspec}
\usepackage[dvipsnames]{pstricks}
\usepackage{pst-eucl,pst-text,pst-tree,pst-tools,pstricks-add}
\usepackage{calc,pifont,fontawesome}
\usepackage{wasysym}
\usepackage{tikz}
\usepackage{hyperref}
\usepackage{amssymb}
\usetikzlibrary{decorations.text, shadows, arrows.meta}

\marksnotpoints
\pointsinrightmargin
\boxedpoints
\setlength{\rightpointsmargin}{1cm}

% header and footer stuff
\cfoot{\thepage}
\thispagestyle{headandfoot}
\firstpageheadrule

\newcommand{\randomword}[1]{%
  \pgfmathsetmacro{\angle}{random(-45,45)}% Generate a random angle between -45 and 45
  \rotatebox{\angle}{#1}%
}

\setcounter{tocdepth}{1}
\setlength\parindent{0pt}

\definecolor{Blue}{rgb}{0.18, 0.19, 0.58}
\newlength{\margedef}
\newlength{\marge}
\setlength{\marge}{0.75cm}
\newcounter{definition}

\newsavebox{\defbox}
\newcommand{\defboxtext}{\raisebox{-0.5ex}{\Large \faBook} \textsf{Definition \thedefinition}}
\newlength{\deftextlength}
\setlength{\deftextlength}{\widthof{\textbf{\defboxtext}}+\widthof{\textsf{\textbf{8}}}+0.66cm+1cm}
\newlength{\defboxheight}
\newenvironment{boxdef}{
   %\par\noindent                        % No indentation
   \begin{flushleft}
   \refstepcounter{definition}             % Counter for this environment
   \begin{lrbox}{\defbox}           % Name of box to contain text
   \begin{minipage}{0.95\linewidth-0.5cm}
}
{
  \end{minipage}
  \end{lrbox}%
  \setlength{\defboxheight}{\totalheightof{\usebox{\defbox}}+20.5pt}
  \rput[tl](0,0){   % Choose colours of framebox
    \psframebox[opacity=0.8,fillstyle=solid,fillcolor=blue!10,framearc=0,framesep=10pt,linecolor=blue,linestyle=none]{%
      \usebox{\defbox}
    }
  }
  \rput[l](1cm,0){      % Patch white line
    \psline[linewidth=2.1\pslinewidth,linecolor=white](-0.25em,0)(\deftextlength,0)%
  }%
  \rput[l](1cm,0.5ex){  % Place text
    %\textcolor{Blue}{\parbox[m]{0.66cm}{\includegraphics[width=0.66cm]{./include/books8_Blue.eps}} \textbf{\defboxtext}}%
    \color{Blue} \textbf{\defboxtext}
  }
  \psline[linewidth=3pt,linecolor=Blue](0.22,0)(0.22,-\defboxheight)
  \par
  \setlength{\margedef}{\ht\defbox+\dp\defbox+\marge}
  \vspace{\margedef}
  \end{flushleft}
}

\newsavebox{\lawbox}
\newcommand{\lawboxtext}{\raisebox{-0.5ex}{\Large \faGavel} \textsf{Laws/Results}}
\newlength{\lawtextlength}
\setlength{\lawtextlength}{\widthof{\textbf{\lawboxtext}+0.5em}}
\newlength{\lawboxheight}
\newenvironment{boxlaw}{
   %\par\noindent                        % No indentation
   \begin{flushleft}
   %\refstepcounter{theorem}             % Counter for this environment
   \begin{lrbox}{\lawbox}           % Name of box to contain text
   \begin{minipage}{0.95\linewidth-0.5cm}
}
{
  \end{minipage}
  \end{lrbox}%
  \setlength{\lawboxheight}{\totalheightof{\usebox{\lawbox}}+20.5pt}
  \rput[tl](0,0){   % Choose colours of framebox
    \psframebox[opacity=0.8,fillstyle=solid,fillcolor=yellow!30,framearc=0,framesep=10pt,linecolor=red,linestyle=none]{%
      \usebox{\lawbox}
    }
  }
  \rput[l](1cm,0){      % Patch white line
    \psline[linewidth=2.1\pslinewidth,linecolor=white](-0.25em,0)(\lawtextlength,0)%
  }%
  \rput[l](1cm,0.5ex){  % Place text
    % \textcolor{red}{\parbox[m]{0.66cm}{\includegraphics[width=0.66cm]{./include/law_red.eps}} \textbf{\lawboxtext}}%
    \color{red} \textbf{\lawboxtext}
  }
  \psline[linewidth=3pt,linecolor=red](0.22,0)(0.22,-\lawboxheight)
  \par
  \setlength{\margedef}{\ht\lawbox+\dp\lawbox+\marge}
  \vspace{\margedef}
  \end{flushleft}
}


% theorembox

\usepackage[tikz]{mdframed}
\usepackage{xcolor,comment}
% now sparing PEN eds printers
%\newmdenv[backgroundcolor=exampleboxcolor]{examplebox}
\colorlet{problemboxcolor}{yellow!20}
\colorlet{exampleboxcolor}{green!30}
\colorlet{exerciseboxcolor}{red!20}
\colorlet{theoremboxcolor}{gray!20}
\colorlet{deriveboxcolor}{yellow!20}
\newmdenv[backgroundcolor=deriveboxcolor]{derivebox}
\newmdenv[backgroundcolor=exampleboxcolor]{examplebox}
\newmdenv[backgroundcolor=exerciseboxcolor]{exercisebox}
\newmdenv[backgroundcolor=problemboxcolor]{problembox}
\newmdenv[backgroundcolor=theoremboxcolor,linewidth=0pt]{theorembox}
%\newmdenv[linecolor=exampleboxcolor,linewidth=10pt]{examplebox}
%\newmdenv[linecolor=exerciseboxcolor,linewidth=10pt]{exercisebox}
%\newmdenv[linecolor=problemboxcolor,linewidth=10pt]{problembox}


%takes in number and spits out that many dotted lines
\newcommand{\mdots}[1]{%
  \ifnum#1>0
    \begin{doublespace}
    \begin{center}
      \foreach \i in {1,...,#1}{%
        \dotfill\\
      }
    \end{center}
    \end{doublespace}
  \fi
  \vspace{-0.5cm}
}

% algebraic expansion demonstration

\newcounter{source}
\newcommand\source[1]{%
    \tikz[remember picture,baseline,inner sep=0pt] {%
        \node [name=source-\thesource,anchor=base]{$#1$};
    }%
    \setcounter{target}{0}
    \stepcounter{source}
}

\newcounter{target}
\newcommand\target[1]{%
    \tikz[remember picture,baseline,inner xsep=0pt] {%
        \node [name=target-\thetarget,anchor=base]{$#1$};
    }%
    \setcounter{source}{0}
    \stepcounter{target}%
}
\newcommand\drawarrows{
    \tikz[remember picture, overlay, bend left=45, -latex] {
    \foreach \j [evaluate=\j as \m using int(\j)] in {1,...,\thesource}{
        \foreach \i [evaluate=\i as \n using int(\i-1)] in {1,...,\thetarget} {
            \draw [red](source-0.north) to (target-\n.north) ;
          }

    }
}
     \tikz[remember picture, overlay, bend left=-45, -latex] {
        \foreach \j [evaluate=\j as \m using int(\j)] in {1,...,\thesource}{
            \foreach \i [evaluate=\i as \n using int(\i-1)] in {1,...,\thetarget} {
                \draw [blue](source-1.south) to (target-\n.south)  ;
          \pgfmathsetmacro{\ii}{\i+2)};
        }
    }
}}

\newcommand\drawarrowssingle{
    \tikz[remember picture, overlay, bend left=45, -latex] {
    \foreach \j [evaluate=\j as \m using int(\j)] in {1,...,\thesource}{
        \foreach \i [evaluate=\i as \n using int(\i-1)] in {1,...,\thetarget} {
            \draw [red](source-0.north) to (target-\n.north) ;
              \node [red] at ([xshift=-5mm]target-\n.north) [above=2mm] {\i};
          }

    }
}
}

\newcommand\drawarrowsodd{
    \tikz[remember picture, overlay, bend left=45, -latex] {
    \foreach \j [evaluate=\j as \m using int(\j)] in {1,...,\thesource}{
        \foreach \i [evaluate=\i as \n using int(\i-1)] in {1,...,\thetarget} {
            \ifodd\i
            \draw [red](source-0.north) to (target-\n.north) ;
            \fi
          }

    }
}
     \tikz[remember picture, overlay, bend left=-45, -latex] {
        \foreach \j [evaluate=\j as \m using int(\j)] in {1,...,\thesource}{
            \foreach \i [evaluate=\i as \n using int(\i-1)] in {1,...,\thetarget} {
                \ifodd\i
                \else
                \draw [blue](source-1.south) to (target-\n.south)  ;
                \fi
          \pgfmathsetmacro{\ii}{\i+2)};
        }
    }
}}

% construct right-angled triangle
\newcommand{\olddrawTriangle}[7]{% #1, #2, #3 are the side lengths; #4, #5, #6 are the labels
    % Determine which side is the hypotenuse based on side lengths
    \pgfmathsetmacro{\hypotenuse}{max(#1,#2,#3)}

    % Assign legs of the triangle
    \ifdim\hypotenuse pt=#1pt
        \pgfmathsetmacro{\legOne}{#2}
        \pgfmathsetmacro{\legTwo}{#3}
        \def\labelHypotenuse{#4}
        \def\labelLegOne{#5}
        \def\labelLegTwo{#6}
    \else
        \ifdim\hypotenuse pt=#2pt
            \pgfmathsetmacro{\legOne}{#1}
            \pgfmathsetmacro{\legTwo}{#3}
            \def\labelHypotenuse{#5}
            \def\labelLegOne{#4}
            \def\labelLegTwo{#6}
        \else
            \pgfmathsetmacro{\legOne}{#1}
            \pgfmathsetmacro{\legTwo}{#2}
            \def\labelHypotenuse{#6}
            \def\labelLegOne{#4}
            \def\labelLegTwo{#5}
        \fi
    \fi

    \begin{tikzpicture}[scale=#7]
        \draw (0,0) -- (\legOne,0) -- (0,\legTwo) -- cycle;

        % Right-angle mark
        \draw (0.3,0) -- (0.3,0.3) -- (0,0.3);

        % Label the sides
        \path (0,0) -- (\legOne,0) node[midway, below] {\labelLegOne};
        \path (0,0) -- (0,\legTwo) node[midway, left] {\labelLegTwo};
        \path (\legOne,0) -- (0,\legTwo) node[midway, above right] {\labelHypotenuse};
    \end{tikzpicture}
}


\newcommand{\drawTriangle}[8]{
    \begin{center}
    \begin{tikzpicture}[scale=#8]
        \ifstrequal{#1}{bottom left}{
            \draw (0,0) -- (#3,0) -- (0,#4) -- cycle;
            \draw (0.3,0) -- (0.3,0.3) -- (0,0.3);
            \path (0,0) -- (#3,0) node[midway, below] {#5};
            \path (0,0) -- (0,#4) node[midway, left] {#6};
            \path (0,#4) -- (#3,0) node[midway, above right] {#7};
        }{}
        \ifstrequal{#1}{bottom right}{
            \draw (0,0) -- (-#3,0) -- (0,#4) -- cycle;
            \draw (-0.3,0) -- (-0.3,0.3) -- (0,0.3);
            \path (0,0) -- (-#3,0) node[midway, below] {#5};
            \path (0,0) -- (0,#4) node[midway, right] {#6};
            \path (0,#4) -- (-#3,0) node[midway, above left] {#7};
        }{}
        \ifstrequal{#1}{top left}{
            \draw (0,0) -- (#3,0) -- (0,-#4) -- cycle;
            \draw (0.3,0) -- (0.3,-0.3) -- (0,-0.3);
            \path (0,0) -- (#3,0) node[midway, above] {#5};
            \path (0,0) -- (0,-#4) node[midway, left] {#6};
            \path (0,-#4) -- (#3,0) node[midway, below right] {#7};
        }{}
        \ifstrequal{#1}{top right}{
            \draw (0,0) -- (-#3,0) -- (0,-#4) -- cycle;
            \draw (-0.3,0) -- (-0.3,-0.3) -- (0,-0.3);
            \path (0,0) -- (-#3,0) node[midway, above] {#5};
            \path (0,0) -- (0,-#4) node[midway, right] {#6};
            \path (0,-#4) -- (-#3,0) node[midway, below left] {#7};
        }{}
    \end{tikzpicture}
    \end{center}
}

%enumerates
\newcounter{saveenumerate}
\makeatletter
\def\enumerate{%
  \xdef\@last@enumerate@arg{none}%
  \ifnum \@enumdepth >3 \@toodeep\else
      \advance\@enumdepth \@ne
      \edef\@enumctr{enum\romannumeral\the\@enumdepth}\fi
  \@ifnextchar[{\@@enum@}{\@enum@}}
\def\@@enum@[#1]{%
  \xdef\@last@enumerate@arg{#1}%
  \@enLab{}\let\@enThe\@enQmark
  \@enloop#1\@enum@
  \ifx\@enThe\@enQmark\@warning{The counter will not be printed.%
   ^^J\space\@spaces\@spaces\@spaces The label is: \the\@enLab}\fi
  \expandafter\edef\csname label\@enumctr\endcsname{\the\@enLab}%
  \expandafter\let\csname the\@enumctr\endcsname\@enThe
  \csname c@\@enumctr\endcsname7
  \expandafter\settowidth
            \csname leftmargin\romannumeral\@enumdepth\endcsname
            {\the\@enLab\hspace{\labelsep}}%
  \@enum@}
\newcommand{\enumtext}[1]{%
\setcounter{saveenumerate}{\value{enum\romannumeral\the\@enumdepth}}
\end{enumerate}
#1
\def\@none{none}
\ifx\@last@enumerate@arg\@none%
\edef\temp{\noexpand\begin{enumerate}}%
\else
\edef\temp{\noexpand\begin{enumerate}[\@last@enumerate@arg]}%
\fi
\temp%
\setcounter{enum\romannumeral\the\@enumdepth}{\value{saveenumerate}}%
}
\makeatother

%fuzzyfont

\newcommand{\fuzzyfont}[1]{%
\begin{tikzpicture}
    \foreach \x in {-0.5pt,0.5pt}{
        \foreach \y in {-0.5pt,0.5pt}{
            \node at (\x,\y) [opacity=0.4,black,inner sep=0pt] {#1};
        }
    }
    \node at (0,0) [opacity=1,inner sep=0pt] {#1};
\end{tikzpicture}
}

\newcommand{\Question}[2][]{%
    \ifx&#1&%
     \question #2%
    \else
     \addtocounter{secmarks}{#1}%
     \question[#1] #2%
    \fi%
}
\newcommand{\Part}[2][]{%
    \ifx&#1&%
        \part #2%
    \else
        \addtocounter{secmarks}{#1}%
        \part[#1] #2%
    \fi
}

\newcommand{\ineqLine}[6][1]{%\ineqLine[scale]{Rstart}{Rfinish}{Istart}{Ifinish}{fill}
\begin{center}
    \begin{tikzpicture}[scale=#1]
        \draw[latex-latex] (#2,0) -- (#3,0);
        \foreach \x in {\number\numexpr#2+1,...,\number\numexpr#3-1}
            \draw (\x,0.1) -- (\x,-0.1) node[below] {\x};

        \draw[very thick, blue, -{Stealth[scale=1.5]}] (#4,0.5) -- (#5,0.5);
        \draw (#4,0.1) -- (#4,-0.1) node[below] {#4};
        \ifnum#6=1
        \filldraw[fill=red, draw=blue, very thick] (#4,0.5) circle (2pt);
        \else
        \filldraw[fill=white, draw=blue, very thick] (#4,0.5) circle (2pt);
        \fi
    \end{tikzpicture}
\end{center}
}


