%% common/preamble.tex — shared preamble for all three formal volumes
%% SOURCE: docs/formal/CLAUDE.md (authoring conventions)
%% Do NOT include this file directly. Each volume's main.tex does:
%%   \input{../common/preamble}

\documentclass[11pt,a4paper,twoside]{book}

%% --- Packages ---
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{booktabs}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{hyperref}
\usepackage{xcolor}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{natbib}

\geometry{a4paper, top=25mm, bottom=25mm, inner=30mm, outer=20mm}

%% --- Theorem environments ---
\newtheorem{theorem}{Theorem}[chapter]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{definition}{Definition}[chapter]
\newtheorem{claim}{Claim}[chapter]

%% --- Code listings ---
\lstset{
  basicstyle=\ttfamily\small,
  breaklines=true,
  frame=single,
  numbers=left,
  numberstyle=\tiny,
  xleftmargin=2em
}

%% Custom language definitions (not built into listings)
\lstdefinelanguage{Forth}{
  morekeywords={IF,ELSE,THEN,DO,LOOP,BEGIN,UNTIL,WHILE,REPEAT,
                DUP,DROP,SWAP,ROT,OVER,NIP,TUCK,
                AND,OR,XOR,INVERT,LSHIFT,RSHIFT,
                VARIABLE,CONSTANT,CREATE,DOES>,
                EMIT,KEY,TYPE,CR,SPACE,ACCEPT,
                INCLUDE,LOAD,THRU,FLUSH,
                BYE,ABORT,S\",.\(,
                WORD-ENTROPY,PHYSICS-FREEZE,PHYSICS-THAW,
                ACL-GRANT,ACL-REVOKE,ACL-PIN,ACL-TTL,
                EXEC,CAPSULE-LOAD},
  morecomment=[l]{\ },
  morecomment=[l]{(},
  morestring=[b]",
  sensitive=false
}
\lstalias{forth}{Forth}

\lstdefinelanguage{R}{
  morekeywords={function,if,else,for,while,return,library,require,
                TRUE,FALSE,NULL,NA,Inf,NaN,
                data.frame,list,vector,matrix,array,
                read.csv,write.csv,ggplot,aov,lm,summary},
  morecomment=[l]{\#},
  morestring=[b]",
  morestring=[b]',
  sensitive=true
}

\lstdefinelanguage{Isabelle}{
  morekeywords={theory,begin,end,theorem,lemma,proof,qed,by,apply,
                definition,fun,datatype,type_synonym,
                fixes,assumes,shows,where,let,have,thus,hence,
                moreover,ultimately,from,with,simp,auto,blast,
                induction,case,next,sledgehammer},
  morecomment=[l]{--},
  morecomment=[s]{(*}{*)},
  morestring=[b]",
  sensitive=true
}
\lstalias{isabelle}{Isabelle}

%% --- Notation ---
% Q48.16 fixed-point type (usable in both text and math mode)
\newcommand{\Qtype}{\ensuremath{\mathbb{Q}_{48.16}}}
% Thermodynamic proxy label (usable in both text and math mode)
\newcommand{\heat}{\ensuremath{\mathit{heat}}}
% Feedback loop reference (\loop is a LaTeX primitive — use \loopref)
\newcommand{\loopref}[1]{\textsf{Loop~\##1}}

%% --- Hyperref setup ---
\hypersetup{
  colorlinks=true,
  linkcolor=blue!60!black,
  citecolor=green!50!black,
  urlcolor=blue!60!black
}
