% Author: Andreas Tsouchlos
%
% Collection of useful commands and definitions
%
% ||====================================================================||
% ||                               WARNING                              ||
% ||====================================================================||
% ||                                                                    ||
% || The following packages have to be included before using this file: ||
% ||      amsmath                                                       ||
% ||      pgfplots                                                      ||
% ||      caption                                                       ||
% ||      listings                                                      ||
% ||====================================================================||


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Math Symbols %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%\DeclareMathOperator*{\argmax}{arg\,max}
%\DeclareMathOperator*{\argmin}{arg\,min}

\DeclareMathOperator*{\argmin}{\arg\!\min}
\DeclareMathOperator*{\argmax}{\arg\!\max}
\DeclareMathOperator\sign{sign}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Graphics & Plotting %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%
% Filters for Pgfplots
% Source: https://tex.stackexchange.com/a/58563 (taken from source and modified)
%

\pgfplotsset{
    discard if/.style 2 args={
        x filter/.append code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
                \def\pgfmathresult{inf}
            \fi
        }
    },
    discard if not/.style 2 args={
        x filter/.append code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
            \else
                \def\pgfmathresult{inf}
            \fi
        }
    },
    discard if gt/.style 2 args={
        x filter/.append code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifdim\tempa pt > \tempb pt
                \def\pgfmathresult{inf}
            \fi
        }
    }
}

%
% Colormaps
%

\pgfplotsset{
    /pgfplots/colormap/mako/.style={
        colormap={mako}{
            rgb=(0.18195582, 0.11955283, 0.23136943)
            rgb=(0.25307401, 0.23772973, 0.48316271)
            rgb=(0.21607792, 0.39736958, 0.61948028)
            rgb=(0.20344718, 0.56074869, 0.65649508)
            rgb=(0.25187832, 0.71827158, 0.67872193)
            rgb=(0.54578602, 0.8544913, 0.69848331)
        }
    },
    /pgfplots/colormap/rocket/.style={
        colormap={rocket}{
            rgb=(0.20973515, 0.09747934, 0.24238489)
            rgb=(0.43860848, 0.12177004, 0.34119475)
            rgb=(0.67824099, 0.09192342, 0.3504148)
            rgb=(0.8833417, 0.19830556, 0.26014181)
            rgb=(0.95381595, 0.46373781, 0.31769923)
            rgb=(0.96516917, 0.70776351, 0.5606593)
        }
    },
    /pgfplots/colormap/cividis/.style={
        colormap={cividis}{
            rgb=(0.130669, 0.231458, 0.43284)
            rgb=(0.298421, 0.332247, 0.423973)
            rgb=(0.42512, 0.431334, 0.447692)
            rgb=(0.555393, 0.537807, 0.471147)
            rgb=(0.695985, 0.648334, 0.440072)
            rgb=(0.849223, 0.771947, 0.359729)
        }
    },
    colormap/cividis,
    colormap/mako,
    colormap/rocket,
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Misc %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%
% Make titles of bibliography entries links
% Source: https://tex.stackexchange.com/a/48409
%

\newbibmacro{string+doiurlisbn}[1]{%
  \iffieldundef{doi}{%
    \iffieldundef{url}{%
      \iffieldundef{isbn}{%
        \iffieldundef{issn}{%
          #1%
        }{%
          \href{http://books.google.com/books?vid=ISSN\thefield{issn}}{#1}%
        }%
      }{%
        \href{http://books.google.com/books?vid=ISBN\thefield{isbn}}{#1}%
      }%
    }{%
      \href{\thefield{url}}{#1}%
    }%
  }{%
    \href{http://dx.doi.org/\thefield{doi}}{#1}%
  }%
}

\DeclareFieldFormat{title}{%
    \usebibmacro{string+doiurlisbn}{\mkbibemph{#1}}
}
\DeclareFieldFormat[article,inbook,incollection,inproceedings,
                    patent,thesis,unpublished]{title}{
    \usebibmacro{string+doiurlisbn}{\mkbibquote{#1\isdot}}
}
\DeclareFieldFormat[suppbook,suppcollection,suppperiodical]{title}{
    \usebibmacro{string+doiurlisbn}{#1}
}

%
% Generic algorithm environment allowing for pseudocode and mathematical expressions
% Source: https://tex.stackexchange.com/a/218450 (taken from source and modified)
%

% defines a new caption label as Algorithm x.y
\DeclareCaptionLabelFormat{algocaption}{Algorithm \thenalg} 

\lstnewenvironment{genericAlgorithm}[1][] 
{
    \captionsetup{labelformat=algocaption,labelsep=colon}
    \lstset{
        mathescape=true,
        escapeinside=||,
        frame=tB,
        numbers=left,
        numberstyle=\tiny,
        basicstyle=\normalfont,
        columns=fullflexible,
        keywordstyle=\color{black}\bfseries,
        % For some reason the first and last entries seem to be ingored
        keywords={a, for, while, end, do, nd, or, return, if, b}
        numbers=left,
        xleftmargin=.04\textwidth,
        #1 % This is to add custom settings
    }
}
{}

%
% Allows for pausing and resuming the numbering in an lstlisting environment
% Taken from https://tex.stackexchange.com/q/215697
%

\let\origthelstnumber\thelstnumber
\makeatletter
\newcommand*\Suppressnumber{%
  \lst@AddToHook{OnNewLine}{%
    \let\thelstnumber\relax%
     \advance\c@lstnumber-\@ne\relax%
    }%
}
\newcommand*\Reactivatenumber{%
  \lst@AddToHook{OnNewLine}{%
   \let\thelstnumber\origthelstnumber%
   \advance\c@lstnumber\@ne\relax}%
}
\makeatother


% For a way to add custom endings to acronyms see https://tex.stackexchange.com/a/268225

