\BLOCK{ set base_docclass_options = documentclass_options|default('', true) }
\BLOCK{ set combined_docclass_options = base_docclass_options[1:-1] if base_docclass_options else '' }
\BLOCK{ set column_count = columns|default(1)|int }
\BLOCK{ set option_parts = [] }
\BLOCK{ if combined_docclass_options }\BLOCK{ set option_parts = option_parts + [combined_docclass_options] }\BLOCK{ endif }
\BLOCK{ set effective_documentclass_options = '[' ~ option_parts|join(',') ~ ']' if option_parts else '' }
\documentclass\VAR{effective_documentclass_options}{article}

% morewrites virtualises \newwrite so a document that combines many glossary
% groups (each costs one \write), biblatex (\bcfout), hyperref (\@outlinefile),
% the toc, and the LOT/LOF can exceed XeLaTeX's 16-register limit. Must be
% loaded before any package that allocates writes; that is why it sits ahead
% of xcolor here.
\usepackage{morewrites}

\usepackage[svgnames,dvipsnames,x11names]{xcolor}
\usepackage{graphicx}
% Babel must be loaded before any package that hooks into its localisation (notably
% the glossaries fragment in ``extra_packages``), otherwise the default acronym
% table title falls back to English regardless of ``language: french`` etc.
\usepackage[\VAR{language|default('english')}]{babel}
% Hyperref must be loaded before ``glossaries`` (pulled in via ``extra_packages``)
% so glossaries can wrap glossary page numbers in ``\hyperpage`` for clickable
% backreferences. Loading it after glossaries leaves the page numbers as plain
% text without hyperlinks.
\usepackage[colorlinks=true, linkcolor=NavyBlue, urlcolor=NavyBlue, citecolor=NavyBlue]{hyperref}
\VAR{extra_packages}

\usepackage{lastpage} % pour le label LastPage
\usepackage{refcount} % pour \getpagerefnumber
\makeatletter
\def\ps@plain{%
  \let\@oddhead\@empty
  \let\@evenhead\@empty
  \def\@oddfoot{%
    \hfil
    % Afficher le numéro seulement si le document a plus d'une page
    \ifnum\getpagerefnumber{LastPage}>1\relax
      \thepage
    \fi
    \hfil}%
  \let\@evenfoot\@oddfoot
}
\makeatother

\BLOCK{ set has_title_metadata = title|default('')|trim }
\BLOCK{ set effective_date = date|default('')|trim }
\BLOCK{ set effective_version = version|default('')|trim }
\BLOCK{ if has_title_metadata }
\title{\VAR{title}\BLOCK{ if subtitle }\\[0.5em]\large \VAR{subtitle}\BLOCK{ endif }}
\BLOCK{ if author }\author{\VAR{author}}\BLOCK{ else }\author{}\BLOCK{ endif }
\BLOCK{ if effective_date and effective_version }\date{\VAR{date}\\[0.25em]{\small \VAR{effective_version}}}\BLOCK{ elif effective_date }\date{\VAR{date}}\BLOCK{ elif effective_version }\date{{\small \VAR{effective_version}}}\BLOCK{ else }\date{}\BLOCK{ endif }
\BLOCK{ endif }

\BLOCK{ if column_count > 1 }
% In two-column mode an overfull line bleeds into the adjacent column, which is
% far worse than a slightly loose line.  \emergencystretch lets TeX add extra
% inter-word stretch as a last resort before declaring an overfull box.
\setlength{\emergencystretch}{3em}
% Forbid widows and orphans of 1 or 2 lines pushed to the next column/page.
% \widowpenalties N p1..pN: p1=penalty when last 1 line is alone at top,
% p2=when last 2 lines are alone, etc.  10000 = forbidden, 150 = default.
% \clubpenalties: same logic for first lines left alone at the bottom.
\widowpenalties 3 10000 10000 150
\clubpenalties  3 10000 10000 150
\displaywidowpenalty 10000
% Restore the single-column abstract style (small font + quotation indentation).
% The article class degrades \begin{abstract} to a bare \section* when the
% twocolumn class option is in effect; we manage column switching manually
% below with \twocolumn[...] so that option is not set here.
\renewenvironment{abstract}{%
  \small
  \begin{center}{\bfseries\abstractname}\end{center}%
  \begin{quotation}%
}{\end{quotation}}
% \thanks{} footnote fix for two-column layout.
% Three bugs defeat the naive toks approach:
%   1. \the\c@footnote is stored literally in toks, not expanded — at replay
%      time article.cls has reset c@footnote to 0 via \@addtoreset{footnote}{page}
%      triggered by the \shipout inside \twocolumn.
%   2. \noexpand before \footnotetext in a toks list corrupts \@ifnextchar
%      scanning when the list is replayed.
%   3. \@maketitle temporarily sets \thefootnote to \@fnsymbol; at replay time
%      that redefinition is gone, so \footnotetext[N] would use arabic numbering
%      and the marks in the title and the text at the bottom would not match.
% Fix: store the mark string and affiliation text in globally-defined csname
% macros indexed by a counter, then replay with \@footnotetext directly.
\makeatletter
\newcount\ts@affilcnt
\newcount\ts@loopcnt
\renewcommand{\thanks}[1]{%
  \stepcounter{footnote}%
  \protected@xdef\@thefnmark{\thefootnote}%
  \@footnotemark
  \global\advance\ts@affilcnt\@ne
  % xdef: globally store the current mark string (e.g. * or †) as a literal
  \expandafter\xdef\csname ts@affilM\the\ts@affilcnt\endcsname{\@thefnmark}%
  % gdef: globally store the affiliation text verbatim (no expansion)
  \expandafter\gdef\csname ts@affilT\the\ts@affilcnt\endcsname{#1}%
}
\newcommand\tsreplayfn{%
  \ts@loopcnt\z@
  \loop
    \advance\ts@loopcnt\@ne
    \ifnum\ts@loopcnt>\ts@affilcnt\else
      \xdef\@thefnmark{\csname ts@affilM\the\ts@loopcnt\endcsname}%
      \@footnotetext{\csname ts@affilT\the\ts@loopcnt\endcsname}%
  \repeat
}
\makeatother
\BLOCK{ endif }

\begin{document}

\BLOCK{ if not page_numbers }
\pagenumbering{gobble}
\thispagestyle{empty}
\pagestyle{empty}
\BLOCK{ endif }
\BLOCK{ if numbering is defined and not numbering }
\setcounter{secnumdepth}{-1}
\BLOCK{ endif }

\BLOCK{ if column_count > 1 }
% Two-column layout: \maketitle is called outside the twocolumn class option so
% it does not internally invoke \twocolumn — \@thanks remains populated and is
% flushed explicitly after the block so affiliation footnotes are preserved.
\twocolumn[{%
\BLOCK{ if has_title_metadata }
\maketitle
\BLOCK{ endif }
\BLOCK{ if abstract }
\begin{abstract}
\VAR{abstract}
\end{abstract}
\vspace{1em}
\BLOCK{ endif }
}]
\tsreplayfn
\BLOCK{ else }
\BLOCK{ if has_title_metadata }
\maketitle
\BLOCK{ endif }
\BLOCK{ if abstract }
\begin{abstract}
\VAR{abstract}
\end{abstract}
\BLOCK{ endif }
\BLOCK{ endif }
\BLOCK{ if toc }
\tableofcontents
\BLOCK{ endif }
\BLOCK{ set lists_pos = lists_position|default('toc') }
\BLOCK{ if (listoffigures or listoftables) and lists_pos == 'toc' }
\BLOCK{ if listoffigures }
\ifnum\value{figure}>0
\clearpage
\listoffigures
\fi
\BLOCK{ endif }
\BLOCK{ if listoftables }
\ifnum\value{table}>0
\clearpage
\listoftables
\fi
\BLOCK{ endif }
\BLOCK{ endif }

\VAR{mainmatter}

\BLOCK{ if appendix }
\appendix
\VAR{appendix}
\BLOCK{ endif }

\BLOCK{ if (listoffigures or listoftables) and lists_pos == 'backmatter' }
\BLOCK{ if listoffigures }
\ifnum\value{figure}>0
\clearpage
\listoffigures
\fi
\BLOCK{ endif }
\BLOCK{ if listoftables }
\ifnum\value{table}>0
\clearpage
\listoftables
\fi
\BLOCK{ endif }
\BLOCK{ endif }

\VAR{backmatter}
\VAR{fragment_backmatter}

\end{document}
