Files
ai-agent-book/book-ar/preamble.tex
T
liqiang b119135836
Build latest book artifacts / build (push) Canceled after 0s
dependency resolution / resolve (3.11) (push) Canceled after 0s
dependency resolution / resolve (3.13) (push) Canceled after 0s
deploy-pages / build (push) Canceled after 0s
deploy-pages / deploy (push) Canceled after 0s
i18n consistency check / check (push) Canceled after 0s
provider adoption tests / test (chapter2/context-compression) (push) Canceled after 0s
provider adoption tests / test (chapter2/prompt-injection) (push) Canceled after 0s
provider adoption tests / test (chapter2/system-hint) (push) Canceled after 0s
provider adoption tests / test (chapter3/log-sanitization) (push) Canceled after 0s
web-search-agent tests / test (push) Canceled after 0s
web-search-agent tests / agentbook (push) Canceled after 0s
ai-agent-book 精选快照(<2MB 代码与文档,来自 github.com/bojieli/ai-agent-book)
2026-08-20 13:12:50 +00:00

317 lines
14 KiB
TeX
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
% ===== Arabic RTL customizations for AI Agents in Depth =====
% Accent color comes from ElegantBook's cyan theme: \structurecolor = RGB(31,186,190)
% ── Text & math fonts: Palatino look, matching the author's papers ──
% The papers' preprint style (arxiv.sty) uses mathpazo (Palatino body +
% matching math) with helvet[scaled=.90] sans. These are the XeLaTeX-native
% equivalents: TeX Gyre Pagella (Palatino) text + TeX Gyre Pagella Math,
% and TeX Gyre Heros (Helvetica clone) sans. Must be set here rather than
% relying on the class: pandoc's template resets the fonts to Latin Modern
% after the class runs, so only preamble-level settings take effect.
% Loaded by filename (like the class's own setup): macOS fontconfig doesn't
% index texmf-dist, so family-name lookup would fail.
\usepackage{unicode-math}
\usepackage{polyglossia}
\setdefaultlanguage{arabic}
\setotherlanguage{english}
% Pandoc does not wrap every structural element in an explicit Arabic
% environment. Keep Arabic coverage in the global body/sans families; forcing
% Latin-only language families here makes unwrapped headings and captions lose
% their Arabic glyphs. The language-specific monospace families are defined
% with the selected true mono font below.
\IfFontExistsTF{Noto Naskh Arabic}{%
\setmainfont[Script=Arabic,Scale=1.08]{Noto Naskh Arabic}%
\newfontfamily\arabicfont[Script=Arabic,Scale=1.08]{Noto Naskh Arabic}%
}{\IfFontExistsTF{Amiri}{%
\setmainfont[Script=Arabic,Scale=1.08]{Amiri}%
\newfontfamily\arabicfont[Script=Arabic,Scale=1.08]{Amiri}%
}{%
\setmainfont[Script=Arabic,Scale=1.08]{Geeza Pro}%
\newfontfamily\arabicfont[Script=Arabic,Scale=1.08]{Geeza Pro}%
}}
\IfFontExistsTF{Noto Sans Arabic}{%
\setsansfont[Script=Arabic,Scale=1.0]{Noto Sans Arabic}%
\newfontfamily\arabicfontsf[Script=Arabic,Scale=1.0]{Noto Sans Arabic}%
}{%
\setsansfont[Script=Arabic,Scale=1.0]{Arial}%
\newfontfamily\arabicfontsf[Script=Arabic,Scale=1.0]{Arial}%
}
\setmathfont{texgyrepagella-math}[Extension = .otf]
\hypersetup{pdflang={ar}}
% ── Unicode glyph fallback (the text/mono fonts lack these) ──────
\usepackage{newunicodechar}
% macOS has Arial Unicode MS / Heiti SC; on Linux those Apple fonts are absent,
% so fall back to broad-coverage libre fonts to keep the build working.
\IfFontExistsTF{Arial Unicode MS}{\newfontfamily\fallbackfont{Arial Unicode MS}}{\newfontfamily\fallbackfont{DejaVu Sans}}
\IfFontExistsTF{Heiti SC}{\newunicodechar{}{{\fontspec{Heiti SC}}}}{\newunicodechar{}{{\fontspec{Noto Sans CJK SC}}}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
% Math / symbols absent from the Latin text font
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{±}{{\fallbackfont ±}}
\newunicodechar{²}{{\fallbackfont ²}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont }}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{}{{\fallbackfont}}
\newunicodechar{ư}{{\fallbackfont ư}}
% Greek letters used inline in body text (Latin Modern lacks them here)
\newunicodechar{Σ}{{\fallbackfont Σ}}
\newunicodechar{Φ}{{\fallbackfont Φ}}
\newunicodechar{α}{{\fallbackfont α}}
\newunicodechar{β}{{\fallbackfont β}}
\newunicodechar{γ}{{\fallbackfont γ}}
\newunicodechar{δ}{{\fallbackfont δ}}
\newunicodechar{ε}{{\fallbackfont ε}}
\newunicodechar{η}{{\fallbackfont η}}
\newunicodechar{θ}{{\fallbackfont θ}}
\newunicodechar{κ}{{\fallbackfont κ}}
\newunicodechar{λ}{{\fallbackfont λ}}
\newunicodechar{μ}{{\fallbackfont μ}}
\newunicodechar{π}{{\fallbackfont π}}
\newunicodechar{ρ}{{\fallbackfont ρ}}
\newunicodechar{σ}{{\fallbackfont σ}}
\newunicodechar{τ}{{\fallbackfont τ}}
\newunicodechar{φ}{{\fallbackfont φ}}
\newunicodechar{ω}{{\fallbackfont ω}}
% ── Residual CJK glyphs ──────────────────────────────────────────
% This is an English book, but a handful of Han characters still appear as
% inline examples (Chinese tokenization / translation / speech-recognition
% cases). Route just those code points to a CJK font so they render, without
% pulling in the whole ctex/xeCJK Chinese-typesetting stack. macOS: Heiti SC;
% Linux: Noto Sans CJK SC.
\IfFontExistsTF{Heiti SC}{\newfontfamily\cjkfallback{Heiti SC}}{\newfontfamily\cjkfallback{Noto Sans CJK SC}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
\newunicodechar{}{{\cjkfallback}}
% ── Code/monospace font (covers Arabic comments + box drawing) ───
% Menlo and SF Mono omit Arabic, which turns translated code comments into
% empty squares. Courier New is monospaced and includes Arabic on macOS;
% DejaVu Sans Mono provides the same coverage on Linux.
\IfFontExistsTF{Courier New}{%
\setmonofont[Scale=0.86]{Courier New}%
\newfontfamily\englishfonttt[Scale=0.86]{Courier New}%
\newfontfamily\arabicfonttt[Script=Arabic,Scale=0.86]{Courier New}%
}{%
\setmonofont[Scale=0.86]{DejaVu Sans Mono}%
\newfontfamily\englishfonttt[Scale=0.86]{DejaVu Sans Mono}%
\newfontfamily\arabicfonttt[Script=Arabic,Scale=0.86]{DejaVu Sans Mono}%
}
% Pandoc's highlighted-code environments process text under the English
% language and a verbatim scanner. Even with an Arabic-capable mono font,
% unmarked Arabic runs are therefore laid out left-to-right. Detect Arabic in
% the token classes that can carry prose and hand just those runs back to
% Polyglossia so they are shaped and ordered correctly.
\ExplSyntaxOn
\cs_new_protected:Npn \arabic_code_wrap:n #1
{
\regex_match:nnTF { [\x{0600}-\x{06FF}] } { #1 }
{ \textarabic{#1} }
{ #1 }
}
\NewDocumentCommand{\arabiccodewrap}{m}{\arabic_code_wrap:n{#1}}
\ExplSyntaxOff
\makeatletter
\@ifundefined{CommentTok}{}{%
\let\arabiccodeoriginalcomment\CommentTok
\renewcommand{\CommentTok}[1]{\arabiccodeoriginalcomment{\arabiccodewrap{#1}}}%
}
\@ifundefined{NormalTok}{}{%
\let\arabiccodeoriginalnormal\NormalTok
\renewcommand{\NormalTok}[1]{\arabiccodeoriginalnormal{\arabiccodewrap{#1}}}%
}
\@ifundefined{StringTok}{}{%
\let\arabiccodeoriginalstring\StringTok
\renewcommand{\StringTok}[1]{\arabiccodeoriginalstring{\arabiccodewrap{#1}}}%
}
\makeatother
% ── tcolorbox (ElegantBook already loads it; just add libraries) ──
\tcbuselibrary{skins,breakable}
% ── TikZ (for the full-bleed image cover in cover.tex) ───────────
\usepackage{tikz}
\usetikzlibrary{fadings}
% Vertical fade: invisible at the top, solid at the bottom — lets the cover
% art melt smoothly into the navy title band.
\tikzfading[name=titlefade, top color=transparent!100, bottom color=transparent!0]
% Theme accent color: override ElegantBook's bright cyan with a deep navy
% (classic, authoritative technical-book tone). Drives headings, rules, links,
% code chrome and the cover. Tweak this one line to restyle the whole book.
\definecolor{structurecolor}{RGB}{30,58,107}
% Accent shade for code/figure chrome (follows the theme color above)
\colorlet{accent}{structurecolor}
% Internal cross-reference links (图N-M / 第N章) — clickable + subtle teal,
% independent of the document class's global linkcolor. Used by crossref.lua.
\newcommand{\crossreflink}[2]{\hyperref[#1]{\textcolor{structurecolor}{#2}}}
\colorlet{accentbg}{structurecolor!6}
\colorlet{accentrule}{structurecolor!35}
% ── Wrap long lines in code blocks ───────────────────────────────
% pandoc emits fancyvrb's Verbatim (via `Highlighting` for ```lang blocks
% and plain `verbatim` for un-tagged blocks); neither wraps by default, so
% long lines overflow the page. fvextra adds breaklines; re-define both
% environments to wrap, breaking mid-token when a chunk has no spaces (URLs,
% long identifiers, CJK comments). A teal ↳ marks each continued line.
\usepackage{fvextra}
\fvset{breaklines=true,breakanywhere=true,%
breaksymbolleft={\footnotesize\textcolor{accentrule}{$\hookrightarrow$}\,}}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{%
commandchars=\\\{\},breaklines,breakanywhere}
% ── Code blocks: subtle O'Reilly-style framed box ────────────────
% One shared box style for ALL code blocks so highlighted (```lang → Shaded)
% and plain (``` → verbatim) blocks look identical. The plain verbatim nests
% a fvextra Verbatim inside the box; \VerbatimEnvironment makes it end at
% \end{verbatim} (the outer env name) rather than \end{Verbatim}.
\newtcolorbox{codebox}{%
breakable, enhanced,
colback=black!3, colframe=accentrule,
boxrule=0.5pt, leftrule=2.5pt,
left=0.7em, right=0.6em, top=0.4em, bottom=0.4em,
arc=0.6mm,
before skip=0.7em, after skip=0.7em
}
\makeatletter
\@ifundefined{Shaded}{\newenvironment{Shaded}{}{}}{}
\makeatother
\renewenvironment{Shaded}{\begin{english}\begin{codebox}}{\end{codebox}\end{english}}
\renewenvironment{verbatim}%
{\VerbatimEnvironment\begin{english}\begin{codebox}\begin{Verbatim}}%
{\end{Verbatim}\end{codebox}\end{english}}
% ── Experiment & question callouts (used by experiment_box.lua) ───
\newtcolorbox{experimentbox}{
breakable, enhanced,
colback=accentbg, colframe=accent,
boxrule=0.6pt, left=0.8em, right=0.8em, top=0.6em, bottom=0.6em,
arc=1mm, before skip=0.9em, after skip=0.9em
}
\newtcolorbox{questionbox}{
breakable, enhanced,
colback=accentbg, colframe=accent,
boxrule=0.8pt, left=0.9em, right=0.9em, top=0.9em, bottom=0.7em,
arc=1.2mm, before skip=0.9em, after skip=0.9em,
title={\sffamily\bfseries للتفكير المتعمق},
fonttitle=\normalsize\color{white},
coltitle=white,
attach boxed title to top left={xshift=1em, yshift=-\tcboxedtitleheight/2},
boxed title style={colback=accent, colframe=accent, arc=0.8mm, outer arc=0.8mm}
}
% ── Figures: force [H] so they can sit inside callout/quote boxes ─
\usepackage{float}
\let\origfigure\figure
\let\origendfigure\endfigure
\renewenvironment{figure}[1][htbp]{\origfigure[H]}{\origendfigure}
\setkeys{Gin}{width=\linewidth,height=0.38\textheight,keepaspectratio}
% ── Captions: manual numbering in text, so suppress auto labels ──
\usepackage{caption}
% Sans, bold, centered. Figure numbers live in the image caption itself, and
% table numbers are likewise written immediately above each table as
% "جدول N-M ...". Suppress labels globally; limiting this to figures would
% duplicate every manual table number with an automatic "الجدول N:" prefix.
\captionsetup{font={normalsize,sf,bf}, labelformat=empty, skip=4pt, justification=centering}
% ── Blockquotes: teal left bar (experiments render as quotes) ────
\renewenvironment{quote}{%
\begin{tcolorbox}[
blanker, breakable,
borderline east={2.5pt}{0pt}{accent},
colback=accent!4,
left=1em, right=0.6em, top=0.5em, bottom=0.5em,
before skip=0.6em, after skip=0.6em
]%
}{%
\end{tcolorbox}%
}
% ── Inline code: keep it tight ───────────────────────────────────
\setlength{\parindent}{2em}
% Arabic structural labels and document direction.
\AtBeginDocument{%
\renewcommand{\contentsname}{المحتويات}%
\renewcommand{\chaptername}{الفصل}%
\renewcommand{\figurename}{الشكل}%
\renewcommand{\tablename}{الجدول}%
}
% A long code block may cross a page while English is the active language.
% Force stored Arabic chapter/section marks back through the Arabic font so
% running headers never lose glyphs on those pages.
\makeatletter
\if@twoside
\fancyhead[EL]{\color{structurecolor}\cnormal\textarabic{\leftmark}}
\fancyhead[OR]{\color{structurecolor}\cnormal\textarabic{\rightmark}}
\else
\fancyhead[R]{\color{structurecolor}\cnormal\textarabic{\rightmark}}
\fi
\makeatother
% ElegantBook empties the `plain` page style used on chapter-opening pages.
% Keep those pages numbered, matching the centered footer on regular pages.
\fancypagestyle{plain}{%
\fancyhf{}%
\fancyfoot[C]{\color{structurecolor}\small\thepage}%
\renewcommand{\headrulewidth}{0pt}%
\renewcommand{\footrulewidth}{0pt}%
}