% ReadAnyBook Math-Heavy Template
% Optimized for mathematical content with theorem environments

\documentclass[{{ font_size | default(10) }}pt,{{ paper_size | default('a4paper') }}]{amsart}

%% ============================================================
%% PACKAGES
%% ============================================================

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{geometry}
\usepackage{multicol}

% Math packages
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{thmtools}
\usepackage{bm}

\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{enumitem}
\usepackage{cleveref}

%% ============================================================
%% GEOMETRY
%% ============================================================

\geometry{
    {{ paper_size | default('a4paper') }},
    top={{ margins.top | default('2cm') }},
    bottom={{ margins.bottom | default('2cm') }},
    left={{ margins.left | default('2cm') }},
    right={{ margins.right | default('2cm') }}
}

%% ============================================================
%% THEOREM ENVIRONMENTS
%% ============================================================

\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{claim}[theorem]{Claim}

\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}
\newtheorem{problem}[theorem]{Problem}

\theoremstyle{remark}
\newtheorem{remark}[theorem]{Remark}
\newtheorem{note}[theorem]{Note}
\newtheorem{observation}[theorem]{Observation}

%% ============================================================
%% MATH COMMANDS
%% ============================================================

% Number sets
\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\F}{\mathbb{F}}

% Probability and statistics
\newcommand{\E}{\mathbb{E}}
\newcommand{\Prob}{\mathbb{P}}
\newcommand{\Var}{\operatorname{Var}}
\newcommand{\Cov}{\operatorname{Cov}}
\newcommand{\Bias}{\operatorname{Bias}}

% Operators
\newcommand{\argmin}{\operatorname*{argmin}}
\newcommand{\argmax}{\operatorname*{argmax}}
\newcommand{\sgn}{\operatorname{sgn}}
\newcommand{\tr}{\operatorname{tr}}
\newcommand{\diag}{\operatorname{diag}}
\newcommand{\rank}{\operatorname{rank}}
\newcommand{\dom}{\operatorname{dom}}
\newcommand{\range}{\operatorname{range}}
\newcommand{\supp}{\operatorname{supp}}

% Norms and inner products
\newcommand{\norm}[1]{\left\lVert #1 \right\rVert}
\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}
\newcommand{\inner}[2]{\left\langle #1, #2 \right\rangle}
\newcommand{\ceil}[1]{\left\lceil #1 \right\rceil}
\newcommand{\floor}[1]{\left\lfloor #1 \right\rfloor}

% Vectors and matrices
\renewcommand{\vec}[1]{\mathbf{#1}}
\newcommand{\mat}[1]{\mathbf{#1}}

% Calculus
\newcommand{\dd}[2]{\frac{d #1}{d #2}}
\newcommand{\pd}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\grad}{\nabla}
\newcommand{\hess}{\nabla^2}

% Big-O notation
\newcommand{\bigO}[1]{O\left(#1\right)}
\newcommand{\bigTheta}[1]{\Theta\left(#1\right)}
\newcommand{\bigOmega}[1]{\Omega\left(#1\right)}

%% ============================================================
%% COLORS
%% ============================================================

\definecolor{defcolor}{RGB}{0,100,0}
\definecolor{thmcolor}{RGB}{0,0,100}
\definecolor{codecolor}{RGB}{100,0,100}

%% ============================================================
%% DOCUMENT
%% ============================================================

\begin{document}

\title{ {{ title }} }
{% if authors %}
\author{ {{ authors | join(' \\and ') }} }
{% endif %}
\date{\today}

\maketitle

{% if abstract %}
\begin{abstract}
{{ abstract }}
\end{abstract}
{% endif %}

{% if include_toc %}
\tableofcontents
\newpage
{% endif %}

{% if columns > 1 %}
\begin{multicols}{ {{ columns }} }
{% endif %}

%% ------------------------------------------------------------
%% DEFINITIONS
%% ------------------------------------------------------------
{% if concepts %}
\section{Definitions and Concepts}

{% for concept in concepts %}
\begin{definition}[{{ concept.name }}]
{{ concept.definition }}
{% if concept.intuition %}

\textit{Intuition:} {{ concept.intuition }}
{% endif %}
\end{definition}

{% endfor %}
{% endif %}

%% ------------------------------------------------------------
%% THEOREMS AND FORMULAS
%% ------------------------------------------------------------
{% if formulas or theorems %}
\section{Key Results}

{% for group_name, group_formulas in formulas_grouped.items() %}
\subsection{ {{ group_name }} }

{% for formula in group_formulas %}
{% if formula.is_theorem %}
\begin{theorem}[{{ formula.name }}]
{% if formula.latex %}
\begin{equation}
{{ formula.latex }}
\end{equation}
{% endif %}
{% if formula.context %}
{{ formula.context }}
{% endif %}
\end{theorem}

{% if formula.proof %}
\begin{proof}
{{ formula.proof }}
\end{proof}
{% endif %}

{% else %}
{% if formula.name %}\textbf{ {{ formula.name }} }:{% endif %}
{% if formula.latex %}
\begin{equation*}
{{ formula.latex }}
\end{equation*}
{% endif %}
{% if formula.context %}
{{ formula.context }}
{% endif %}

{% endif %}
{% endfor %}
{% endfor %}
{% endif %}

%% ------------------------------------------------------------
%% ALGORITHMS
%% ------------------------------------------------------------
{% if algorithms %}
\section{Algorithms}

{% for algo in algorithms %}
\begin{algorithm}
\caption{ {{ algo.name }} }
\begin{algorithmic}[1]
{% if algo.input %}
\Require {{ algo.input }}
{% endif %}
{% if algo.output %}
\Ensure {{ algo.output }}
{% endif %}
{{ algo.algorithmic_code | default(algo.pseudocode) }}
\end{algorithmic}
\end{algorithm}

{% if algo.time_complexity or algo.space_complexity %}
\textbf{Complexity:}
{% if algo.time_complexity %}Time: ${{ algo.time_complexity }}${% endif %}
{% if algo.space_complexity %}, Space: ${{ algo.space_complexity }}${% endif %}
{% endif %}

{% if algo.key_insight %}
\begin{remark}
{{ algo.key_insight }}
\end{remark}
{% endif %}

{% endfor %}
{% endif %}

%% ------------------------------------------------------------
%% MODELS
%% ------------------------------------------------------------
{% if models %}
\section{Models and Theory}

{% for model in models %}
\subsection{ {{ model.name }} }

{% if model.assumptions %}
\begin{definition}[Model Assumptions]
\begin{enumerate}
{% for assumption in model.assumptions %}
\item {{ assumption }}
{% endfor %}
\end{enumerate}
\end{definition}
{% endif %}

{% if model.objective %}
\textbf{Objective:} {{ model.objective }}
{% endif %}

{% if model.formulation %}
\begin{equation*}
{{ model.formulation }}
\end{equation*}
{% endif %}

{% if model.properties %}
\begin{proposition}[Properties]
\begin{enumerate}
{% for prop in model.properties %}
\item {{ prop }}
{% endfor %}
\end{enumerate}
\end{proposition}
{% endif %}

{% endfor %}
{% endif %}

{% if columns > 1 %}
\end{multicols}
{% endif %}

%% ------------------------------------------------------------
%% REFERENCES
%% ------------------------------------------------------------
{% if include_references and references %}
\bibliographystyle{alpha}
\begin{thebibliography}{99}
{% for ref in references %}
\bibitem{ {{ loop.index }} } {{ ref }}
{% endfor %}
\end{thebibliography}
{% endif %}

\end{document}
