% ReadAnyBook Default Cheat Sheet Template
% A clean, dense 12-page cheat sheet for technical content

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

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

% Encoding and fonts
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

% Page layout
\usepackage{geometry}
\usepackage{multicol}
\usepackage{fancyhdr}

% Mathematics
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{bm}

% Algorithms and code
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{listings}

% Graphics and colors
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{tikz}

% Tables
\usepackage{booktabs}
\usepackage{tabularx}

% Links and references
\usepackage{hyperref}

% Typography
\usepackage{titlesec}
\usepackage{enumitem}
\usepackage{parskip}

%% ============================================================
%% PAGE GEOMETRY
%% ============================================================

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

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

\definecolor{sectioncolor}{RGB}{0,51,102}
\definecolor{subsectioncolor}{RGB}{0,76,153}
\definecolor{linkcolor}{RGB}{0,102,204}
\definecolor{codebackground}{RGB}{248,248,248}
\definecolor{codecomment}{RGB}{106,153,85}
\definecolor{codekeyword}{RGB}{0,0,255}
\definecolor{codestring}{RGB}{163,21,21}
\definecolor{boxbackground}{RGB}{240,248,255}

%% ============================================================
%% SECTION FORMATTING
%% ============================================================

\titleformat{\section}
    {\normalfont\large\bfseries\color{sectioncolor}}
    {\thesection}{0.8em}{}[\titlerule]

\titleformat{\subsection}
    {\normalfont\normalsize\bfseries\color{subsectioncolor}}
    {\thesubsection}{0.6em}{}

\titleformat{\subsubsection}
    {\normalfont\small\bfseries}
    {\thesubsubsection}{0.5em}{}

\titlespacing*{\section}{0pt}{2ex plus 1ex minus .2ex}{1ex plus .2ex}
\titlespacing*{\subsection}{0pt}{1.5ex plus 1ex minus .2ex}{0.5ex plus .2ex}
\titlespacing*{\subsubsection}{0pt}{1ex plus .5ex minus .1ex}{0.3ex plus .1ex}

%% ============================================================
%% LIST FORMATTING
%% ============================================================

\setlist[itemize]{noitemsep, topsep=2pt, leftmargin=1.5em}
\setlist[enumerate]{noitemsep, topsep=2pt, leftmargin=1.8em}
\setlist[description]{noitemsep, topsep=2pt, leftmargin=0pt, labelindent=0pt}

%% ============================================================
%% CODE LISTING STYLE
%% ============================================================

\lstset{
    basicstyle=\ttfamily\footnotesize,
    backgroundcolor=\color{codebackground},
    commentstyle=\color{codecomment}\itshape,
    keywordstyle=\color{codekeyword}\bfseries,
    stringstyle=\color{codestring},
    breaklines=true,
    breakatwhitespace=true,
    frame=single,
    framesep=3pt,
    xleftmargin=5pt,
    xrightmargin=5pt,
    tabsize=2,
    showstringspaces=false,
    captionpos=b,
    numbers=none,
    aboveskip=0.5em,
    belowskip=0.5em,
}

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

\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]
\newtheorem{example}{Example}[section]

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

\theoremstyle{remark}
\newtheorem{remark}{Remark}[section]
\newtheorem{note}{Note}[section]

%% ============================================================
%% CUSTOM COMMANDS
%% ============================================================

% Typography
\newcommand{\important}[1]{\textbf{#1}}
\newcommand{\concept}[1]{\textit{#1}}
\newcommand{\term}[1]{\textsc{#1}}

% Math shortcuts
\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\E}{\mathbb{E}}
\newcommand{\Prob}{\mathbb{P}}
\newcommand{\Var}{\text{Var}}
\newcommand{\Cov}{\text{Cov}}
\newcommand{\argmin}{\operatorname{argmin}}
\newcommand{\argmax}{\operatorname{argmax}}

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

% Derivatives
\newcommand{\dd}[2]{\frac{d #1}{d #2}}
\newcommand{\pd}[2]{\frac{\partial #1}{\partial #2}}

% Custom boxes
\newcommand{\keypoint}[1]{%
    \begin{tcolorbox}[colback=boxbackground,colframe=sectioncolor,boxrule=0.5pt,arc=2pt]
    #1
    \end{tcolorbox}
}

%% ============================================================
%% HEADER/FOOTER
%% ============================================================

\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\small\textcolor{gray}{ {{ title }} }}
\fancyhead[R]{\small\textcolor{gray}{\thepage}}
\renewcommand{\headrulewidth}{0.4pt}
\fancyfoot[C]{}

%% ============================================================
%% HYPERREF SETUP
%% ============================================================

\hypersetup{
    colorlinks=true,
    linkcolor=linkcolor,
    urlcolor=linkcolor,
    citecolor=linkcolor,
    pdftitle={ {{ title }} },
    pdfauthor={ReadAnyBook},
}

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

\begin{document}

% Title
\begin{center}
    {\LARGE\bfseries\color{sectioncolor} {{ title }} }\\[0.5em]
    {% if subtitle %}
    {\large {{ subtitle }} }\\[0.3em]
    {% endif %}
    {% if authors %}
    {\normalsize {{ authors | join(', ') }} }\\[0.3em]
    {% endif %}
    {\small\textcolor{gray}{\today}}
\end{center}

\vspace{0.5em}
\hrule
\vspace{1em}

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

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

%% ------------------------------------------------------------
%% OVERVIEW
%% ------------------------------------------------------------
{% if overview %}
\section*{Overview}
{{ overview }}
\vspace{0.5em}
{% endif %}

%% ------------------------------------------------------------
%% CORE CONCEPTS
%% ------------------------------------------------------------
{% if concepts %}
\section{Core Concepts}

{% for concept in concepts %}
\textbf{ {{ concept.name }} }{% if concept.definition %}: {{ concept.definition }}{% endif %}

{% if concept.intuition %}
\textit{Intuition:} {{ concept.intuition }}
{% endif %}

{% if concept.examples %}
\textit{Examples:} {{ concept.examples | join(', ') }}
{% endif %}

{% if not loop.last %}\vspace{0.3em}{% endif %}
{% endfor %}
{% endif %}

%% ------------------------------------------------------------
%% KEY FORMULAS
%% ------------------------------------------------------------
{% if formulas %}
\section{Key Formulas}

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

{% for formula in group_formulas %}
{% if formula.name %}\textbf{ {{ formula.name }} }{% endif %}
{% if formula.latex %}
\begin{equation*}
{{ formula.latex }}
\end{equation*}
{% endif %}
{% if formula.context %}
\small\textit{ {{ formula.context }} }\normalsize
{% endif %}

{% if formula.derivation %}
\textit{Derivation:} {{ formula.derivation }}
{% endif %}

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

%% ------------------------------------------------------------
%% MODELS & THEORY
%% ------------------------------------------------------------
{% if models %}
\section{Models \& Theory}

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

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

{% endif %}
{% if model.assumptions %}
\textbf{Assumptions:}
\begin{itemize}
{% for assumption in model.assumptions %}
\item {{ assumption }}
{% endfor %}
\end{itemize}
{% endif %}

{% if model.formulation %}
\textbf{Formulation:}
{{ model.formulation }}
{% endif %}

{% if model.properties %}
\textbf{Properties:}
\begin{itemize}
{% for prop in model.properties %}
\item {{ prop }}
{% endfor %}
\end{itemize}
{% endif %}

{% if model.summary %}
{{ model.summary }}
{% endif %}

{% endfor %}
{% endif %}

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

{% for algo in algorithms %}
\subsection{ {{ algo.name }} }

{% if algo.purpose %}
\textbf{Purpose:} {{ algo.purpose }}
{% endif %}

{% if algo.input %}
\textbf{Input:} {{ algo.input }}
{% endif %}

{% if algo.output %}
\textbf{Output:} {{ algo.output }}
{% endif %}

{% if algo.pseudocode %}
\begin{lstlisting}[language=Python, caption={{ algo.name }}]
{{ algo.pseudocode }}
\end{lstlisting}
{% endif %}

{% 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 %}
\textbf{Key Insight:} {{ algo.key_insight }}
{% endif %}

{% endfor %}
{% endif %}

%% ------------------------------------------------------------
%% CONNECTIONS
%% ------------------------------------------------------------
{% if connections %}
\section{Related Topics}

\begin{itemize}
{% for conn in connections %}
\item \textbf{ {{ conn.topic }} }: {{ conn.description }}
{% endfor %}
\end{itemize}
{% endif %}

%% ------------------------------------------------------------
%% QUICK REFERENCE
%% ------------------------------------------------------------
{% if recap %}
\section{Quick Reference}

{{ recap }}
{% endif %}

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

%% ------------------------------------------------------------
%% REFERENCES
%% ------------------------------------------------------------
{% if include_references and references %}
\section*{References}

\begin{enumerate}[label={[\arabic*]}]
{% for ref in references %}
\item {{ ref }}
{% endfor %}
\end{enumerate}
{% endif %}

\end{document}
