\BLOCK{ set exam_type_raw = type|default('exam')|string }
\BLOCK{ set exam_type = exam_type_raw|lower }
\BLOCK{ set title_value = title|default('')|trim }
\BLOCK{ set subtitle_value = subtitle|default('')|trim }
\BLOCK{ set author_value = author|default('')|trim }
\BLOCK{ set date_value = date|default('')|trim }
\BLOCK{ set titlepage_raw = titlepage|default('cover')|string|lower }
\BLOCK{ set titlepage_minimal = titlepage_raw == 'minimal' }
\BLOCK{ set fillin_style_raw = fillin_style|default('line')|string|lower|trim }
\BLOCK{ set fillin_style = fillin_style_raw if fillin_style_raw in ['line', 'dotted'] else 'line' }
\BLOCK{ set logo_value = logo|default(none) }
\BLOCK{ if exam is defined and exam }
  \BLOCK{ set logo_value = exam.get('logo', logo_value) }
\BLOCK{ endif }
\BLOCK{ set logo_disabled = false }
\BLOCK{ set logo_builtin = '' }
\BLOCK{ set logo_file = '' }
\BLOCK{ set logo_layout_key = 'minimal' if titlepage_minimal else 'cover' }
\BLOCK{ set logo_defaults = {
  'minimal': {'top': '10mm', 'left': '24mm', 'height': '1.45cm'},
  'cover': {'top': '10mm', 'left': '10mm', 'height': '1.8cm'}
} }
\BLOCK{ set logo_layout_default = logo_defaults.get(logo_layout_key, {}) }
\BLOCK{ if logo_value is mapping }
  \BLOCK{ set logo_base = logo_value }
  \BLOCK{ set logo_layout = logo_value.get(logo_layout_key, {}) if logo_value.get(logo_layout_key) is mapping else {} }
  \BLOCK{ set logo_builtin = logo_layout.get('builtin', logo_base.get('builtin', '')) }
  \BLOCK{ set logo_file = logo_layout.get('file', logo_base.get('file', '')) }
  \BLOCK{ set logo_top = logo_layout.get('top', logo_base.get('top', logo_layout_default.get('top', ''))) }
  \BLOCK{ set logo_left = logo_layout.get('left', logo_base.get('left', logo_layout_default.get('left', ''))) }
  \BLOCK{ set logo_height = logo_layout.get('height', logo_base.get('height', logo_layout_default.get('height', ''))) }
\BLOCK{ if not logo_builtin and not logo_file }
  \BLOCK{ set logo_disabled = true }
\BLOCK{ endif }
\BLOCK{ elif logo_value is string }
  \BLOCK{ set logo_text = logo_value|trim|lower }
  \BLOCK{ if logo_text in ['none', 'false', 'off', 'no'] }
    \BLOCK{ set logo_disabled = true }
  \BLOCK{ elif logo_text in ['heig-vd', 'heigvd', 'heig_vd'] }
    \BLOCK{ set logo_builtin = 'heig-vd' }
  \BLOCK{ elif logo_text }
    \BLOCK{ set logo_file = logo_value|trim }
  \BLOCK{ endif }
  \BLOCK{ set logo_top = logo_layout_default.get('top', '') }
  \BLOCK{ set logo_left = logo_layout_default.get('left', '') }
  \BLOCK{ set logo_height = logo_layout_default.get('height', '') }
\BLOCK{ if not logo_text }
  \BLOCK{ set logo_disabled = true }
\BLOCK{ endif }
\BLOCK{ elif logo_value is boolean }
  \BLOCK{ if not logo_value }
    \BLOCK{ set logo_disabled = true }
  \BLOCK{ endif }
\BLOCK{ endif }
\BLOCK{ set fillin_solution_underline_value = fillin_solution_underline|default(false) }
\BLOCK{ if exam is defined and exam }
  \BLOCK{ set fillin_solution_underline_value = exam.get('fillin_solution_underline', exam.get('fillin-solution-underline', fillin_solution_underline_value)) }
\BLOCK{ endif }
\BLOCK{ set lang = language|default('english')|lower }
\BLOCK{ if lang in ['french', 'fr', 'francais'] }
  \BLOCK{ set label_problem_default = "Probl\\`eme" }
  \BLOCK{ set label_rules = "Consignes" }
  \BLOCK{ set label_next_page = "Allez \\`a la page suivante..." }
  \BLOCK{ set label_page_of = "Page \\thepage\\ sur \\pageref{LastPage}" }
  \BLOCK{ set label_end_exam = "" }
  \BLOCK{ set label_name = "Nom, Pr\\'enom" }
  \BLOCK{ set label_duration = "Dur\\'ee" }
\BLOCK{ elif lang in ['italian', 'it', 'italiano'] }
  \BLOCK{ set label_problem_default = "Problema" }
  \BLOCK{ set label_rules = "Istruzioni" }
  \BLOCK{ set label_next_page = "Vai alla pagina successiva..." }
  \BLOCK{ set label_page_of = "Pagina \\thepage\\ di \\pageref{LastPage}" }
  \BLOCK{ set label_end_exam = "" }
  \BLOCK{ set label_name = "Cognome, Nome" }
  \BLOCK{ set label_duration = "Durata" }
\BLOCK{ elif lang in ['german', 'de', 'deutsch'] }
  \BLOCK{ set label_problem_default = "Aufgabe" }
  \BLOCK{ set label_rules = "Hinweise" }
  \BLOCK{ set label_next_page = "Bitte zur n\\\"achsten Seite..." }
  \BLOCK{ set label_page_of = "Seite \\thepage\\ von \\pageref{LastPage}" }
  \BLOCK{ set label_end_exam = "" }
  \BLOCK{ set label_name = "Nachname, Vorname" }
  \BLOCK{ set label_duration = "Dauer" }
\BLOCK{ else }
  \BLOCK{ set label_problem_default = "Problem" }
  \BLOCK{ set label_rules = "Instructions" }
  \BLOCK{ set label_next_page = "Go to the next page..." }
  \BLOCK{ set label_page_of = "Page \\thepage\\ of \\pageref{LastPage}" }
  \BLOCK{ set label_end_exam = "" }
  \BLOCK{ set label_name = "Surname, First name" }
  \BLOCK{ set label_duration = "Duration" }
\BLOCK{ endif }
\BLOCK{ set label_problem_override = '' }
\BLOCK{ if exam is defined and exam }
  \BLOCK{ set label_problem_override = exam.get('problem-label', exam.get('problem_label', ''))|string|trim }
\BLOCK{ endif }
\BLOCK{ if not label_problem_override }
  \BLOCK{ set label_problem_override = problem_label|default('', true)|string|trim }
\BLOCK{ endif }
\BLOCK{ set label_problem = label_problem_override|default(label_problem_default, true)|trim }
\BLOCK{ set next_page_advice = next_page_advice|default(true) }
\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 option_parts = [] }
\BLOCK{ if combined_docclass_options }\BLOCK{ set option_parts = option_parts + [combined_docclass_options] }\BLOCK{ endif }
\BLOCK{ set option_parts = option_parts + ['addpoints'] }
\BLOCK{ set effective_documentclass_options = '[' ~ option_parts|join(',') ~ ']' if option_parts else '' }
\documentclass\VAR{effective_documentclass_options}{exam}
\pointsinrightmargin
\BLOCK{ if solution }
\printanswers
\BLOCK{ endif }

\usepackage[svgnames,dvipsnames,x11names]{xcolor}
\VAR{extra_packages}
\usepackage[defaults=exam]{columen}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{ifthen}
\usepackage{eso-pic}
\usepackage{etoolbox}
\usepackage{afterpage}
\usepackage{needspace}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[\VAR{language|default('english')}]{babel}
\BLOCK{ if lang in ['french', 'fr', 'francais'] }
\frenchsetup{ThinColonSpace=true}
\BLOCK{ endif }
\usepackage[normalem]{ulem}
\usepackage{lastpage}
\BLOCK{ if not ts_typesetting_enabled|default(false) }
\setlength{\parskip}{1.2em}
\setlength{\parindent}{0pt}
\BLOCK{ endif }
\BLOCK{ if not logo_disabled and logo_builtin == 'heig-vd' }
\usepackage{heiglogo}
\BLOCK{ endif }
\usepackage{xparse}

\newcommand{\ExamLogoCustom}[4]{%
  \AddToShipoutPictureFG*{%
    \AtPageUpperLeft{%
      \put(#2,-#3){\includegraphics[height=#4]{#1}}%
    }%
  }%
  \vspace*{-\baselineskip}%
}
\newcommand{\ExamLogoSpacer}{\vspace*{-\baselineskip}}

% Increase table row height only when a row contains at least one \fillin.
\newcount\texsmithtabledepth
\texsmithtabledepth=0
\newcommand{\texsmithtablefillinrowheight}{3.4ex}
\newcommand{\texsmithtablefillinstrut}{\rule{0pt}{\texsmithtablefillinrowheight}}
\newcommand{\texsmithtabledepthinc}{\global\advance\texsmithtabledepth by 1\relax}
\newcommand{\texsmithtabledepthdec}{%
  \ifnum\texsmithtabledepth>0
    \global\advance\texsmithtabledepth by -1\relax
  \fi
}
\AddToHook{env/tabularx/begin}{\texsmithtabledepthinc}
\AddToHook{env/tabularx/end}{\texsmithtabledepthdec}
\AddToHook{env/longtable/begin}{\texsmithtabledepthinc}
\AddToHook{env/longtable/end}{\texsmithtabledepthdec}
\makeatletter
\newif\iftexsmithfillinsolutionunderline
\BLOCK{ if fillin_solution_underline_value }
\texsmithfillinsolutionunderlinetrue
\BLOCK{ else }
\texsmithfillinsolutionunderlinefalse
\BLOCK{ endif }
\let\texsmith@origfillin\fillin
\RenewDocumentCommand{\fillin}{o o}{%
  \ifprintanswers
    % In solution mode, render only the answer text (no reserved width).
    \IfNoValueTF{#1}{%
      \texsmith@origfillin
    }{%
      \iftexsmithfillinsolutionunderline
        \IfNoValueTF{#2}{%
          \texsmith@origfillin[#1]%
        }{%
          \texsmith@origfillin[#1][#2]%
        }%
      \else
        \ifnum\texsmithtabledepth>0\relax
          {\bfseries #1}%
        \else
          {\bfseries #1}\nobreak\ \ignorespaces
        \fi
      \fi
    }%
  \else
    \IfNoValueTF{#1}{%
      \IfNoValueTF{#2}{%
        \texsmith@origfillin
      }{%
        \texsmith@origfillin[][#2]%
      }%
    }{%
      \IfNoValueTF{#2}{%
        \texsmith@origfillin[#1]%
      }{%
        \texsmith@origfillin[#1][#2]%
      }%
    }%
  \fi
  \ifnum\texsmithtabledepth>0\relax
    \texsmithtablefillinstrut
  \fi
}
\makeatother

\newif\ifexamquestionsopen
\examquestionsopenfalse
\newcommand{\ExamQuestionsBegin}{%
  \ifexamquestionsopen\else
    \begin{questions}%
    \examquestionsopentrue
  \fi
}
\newcommand{\ExamQuestionsEnd}{%
  \ifexamquestionsopen
    \end{questions}%
    \examquestionsopenfalse
  \fi
}

\makeatletter
\newcommand{\texsmithfillinline}[1]{%
  \BLOCK{ if fillin_style == 'dotted' }
  \hbox to #1{\dotfill}%
  \BLOCK{ else }
  \hbox to #1{\hrulefill}%
  \BLOCK{ endif }
}
\def\@fillin@relay[#1]{%
  \leavevmode
  \ifprintanswers
    \iftexsmithfillinsolutionunderline
      \rlap{\raise -\answerclearance \texsmithfillinline{#1}}%
      \begingroup
        \setbox0 \hbox{\color@begingroup\bfseries\fillin@ans\color@endgroup}%
        \ifdim\wd0 > #1\relax
          \hbox{\color@begingroup\bfseries\fillin@ans\color@endgroup}%
        \else
          \hbox to #1{\color@begingroup\bfseries\hfil\fillin@ans\hfil\color@endgroup}%
        \fi
      \endgroup
    \else
      {\bfseries\color@begingroup\fillin@ans\color@endgroup}\nobreak%
    \fi
  \else
    \raise -\answerclearance \texsmithfillinline{#1}%
  \fi
}
\unframedsolutions
\renewcommand{\solutiontitle}{}
\renewenvironment{TheSolution}%
  {%
    \par\addvspace{1em}\noindent%
    \leftskip=0pt
    \rightskip=0pt
    \begingroup
    \def\PY##1##2{##2}%
    \begin{examanswerbar}%
    \ignorespaces
  }%
  {%
    \end{examanswerbar}%
    \unskip
    \endgroup
    \par\addvspace{1em}%
  }%
\CorrectChoiceEmphasis{\bfseries}
\@ifpackageloaded{tcolorbox}{}{%
  \PassOptionsToPackage{most,skins,breakable}{tcolorbox}%
  \usepackage{tcolorbox}%
}
\newtcolorbox{examanswerbar}[1][]{%
  enhanced,
  breakable,
  colback=white,
  boxrule=0pt,
  frame hidden,
  overlay unbroken={\draw[line width=2pt,black] ([yshift=1mm]frame.north west) -- ([yshift=-1mm]frame.south west);},
  overlay first={\draw[line width=2pt,black] ([yshift=1mm]frame.north west) -- ([yshift=-1mm]frame.south west);},
  overlay middle={\draw[line width=2pt,black] ([yshift=1mm]frame.north west) -- ([yshift=-1mm]frame.south west);},
  overlay last={\draw[line width=2pt,black] ([yshift=1mm]frame.north west) -- ([yshift=-1mm]frame.south west);},
  before skip=\parskip,
  after skip=\parskip,
  left=6pt,
  right=0pt,
  top=0pt,
  bottom=0pt,
  boxsep=0pt,
  #1
}

% Keep part labels with the following code block and reduce vertical offset.
\makeatletter
\@ifpackageloaded{tcolorbox}{%
  \tcbset{code/.append style={before skip=-0.95\baselineskip,after skip=0.4\baselineskip,top=0pt}}%
}{}
\makeatother
\BeforeBeginEnvironment{code}{%
  \nopagebreak\needspace{6\baselineskip}%
  \makeatletter
  \if@inlabel\else\vspace{0.5em}\fi
  \makeatother
  \noindent%
}
\AfterEndEnvironment{code}{\par}
\renewenvironment{solutionorgrid}[1][0pt]%
  {%
    \@insolutiontrue
    \@addpointsfalse
    \@ifundefined{ifsolutionsreseteqcounter}{}{%
      \ifsolutionsreseteqcounter
        \setcounter{exam@saved@eqnum}{\value{equation}}%
        \setcounter{equation}{0}%
      \fi
    }%
    \ifprintanswers
      \begingroup
      \Solution@Emphasis
      \begin{TheSolution}%
    \else
      \ifcancelspace
        % Do nothing
      \else
        \par
        \penalty 0
        \vspace{1em}%
        \fillwithgrid{#1}%
        \vspace{1em}%
      \fi
      \setbox\z@\vbox\bgroup
    \fi
  }{%
    \ifprintanswers
      \end{TheSolution}%
      \endgroup
    \else
      \egroup
    \fi
    \@ifundefined{ifsolutionsreseteqcounter}{}{%
      \ifsolutionsreseteqcounter
        \setcounter{equation}{\value{exam@saved@eqnum}}%
      \fi
    }%
  }%
\makeatother

\makeatletter
\renewenvironment{solutionorlines}[1][0pt]%
  {%
    \@insolutiontrue
    \@addpointsfalse
    \@ifundefined{ifsolutionsreseteqcounter}{}{%
      \ifsolutionsreseteqcounter
        \setcounter{exam@saved@eqnum}{\value{equation}}%
        \setcounter{equation}{0}%
      \fi
    }%
    \ifprintanswers
      \begingroup
      \Solution@Emphasis
      \begin{TheSolution}%
    \else
      \ifcancelspace
        % Do nothing
      \else
        \par
        \penalty 0
        \vspace{1em}%
        \fillwithlines{#1}%
        \vspace{1em}%
      \fi
      \setbox\z@\vbox\bgroup
    \fi
  }{%
    \ifprintanswers
      \end{TheSolution}%
      \endgroup
    \else
      \egroup
    \fi
    \@ifundefined{ifsolutionsreseteqcounter}{}{%
      \ifsolutionsreseteqcounter
        \setcounter{equation}{\value{exam@saved@eqnum}}%
      \fi
    }%
  }%

\renewenvironment{solutionordottedlines}[1][0pt]%
  {%
    \@insolutiontrue
    \@addpointsfalse
    \@ifundefined{ifsolutionsreseteqcounter}{}{%
      \ifsolutionsreseteqcounter
        \setcounter{exam@saved@eqnum}{\value{equation}}%
        \setcounter{equation}{0}%
      \fi
    }%
    \ifprintanswers
      \begingroup
      \Solution@Emphasis
      \begin{TheSolution}%
    \else
      \ifcancelspace
        % Do nothing
      \else
        \par
        \penalty 0
        \vspace{1em}%
        \fillwithdottedlines{#1}%
        \vspace{1em}%
      \fi
      \setbox\z@\vbox\bgroup
    \fi
  }{%
    \ifprintanswers
      \end{TheSolution}%
      \endgroup
    \else
      \egroup
    \fi
    \@ifundefined{ifsolutionsreseteqcounter}{}{%
      \ifsolutionsreseteqcounter
        \setcounter{equation}{\value{exam@saved@eqnum}}%
      \fi
    }%
  }%
\makeatother

% Grid styling for solution boxes
\colorgrids
\definecolor{GridColor}{gray}{0.7}
\setlength{\gridsize}{5mm}

\makeatletter
\newcommand{\texsmithprintpointsnumeric}[2]{%
  \begingroup
  \edef\texsmith@tmp{#1}%
  \if\relax\detokenize{\texsmith@tmp}\relax
  \else
    \ifdim\texsmith@tmp pt=0pt\relax
    \else
      #2%
    \fi
  \fi
  \endgroup
}
\newcommand{\texsmithpointname}[1]{%
  \begingroup
  \edef\texsmith@tmp{#1}%
  \if\relax\detokenize{\texsmith@tmp}\relax
  \else
    \ifdim\texsmith@tmp pt=1pt\relax
      \point@sing
    \else
      \point@plur
    \fi
  \fi
  \endgroup
}
\newcommand{\ExamQuestionPoints}{%
  \if@placepoints
    \texsmithprintpointsnumeric{\@points}{(\@points\ \texsmithpointname{\@points})}%
  \else
    \@ifundefined{pointsofq@\romannumeral\arabic{question}}{}{%
      \begingroup
        \edef\@points{\pointsofquestion{\arabic{question}}}%
        \texsmithprintpointsnumeric{\@points}{(\@points\ \texsmithpointname{\@points})}%
      \endgroup
    }%
  \fi
}
\makeatother
\newcommand{\texsmithquestiontitle}{%
  \ifthenelse{\equal{\thequestiontitle}{}}{}{%
    \ifthenelse{\equal{\thequestiontitle}{\thequestion}}{}{:\space\thequestiontitle}%
  }%
}
\qformat{\vbox{\hbox to \hsize{\textbf{\VAR{label_problem} \thequestion\texsmithquestiontitle}\hfill\ExamQuestionPoints}\vspace{0.8em}}}
\renewcommand{\partlabel}{\textbf{(\thepartno)}}
\renewcommand{\subpartlabel}{\textbf{\thesubpart.}}
\renewcommand{\subsubpartlabel}{\textbf{\thesubsubpart)}}
\renewcommand{\questionshook}{%
  % Only reduce spacing after question labels, keep parts/subparts defaults.
  \setlength{\labelsep}{0.4em}%
  \settowidth{\leftmargin}{2.\hskip\labelsep}%
  \labelwidth\leftmargin\advance\labelwidth-\labelsep
  \setlength{\itemindent}{0pt}%
  \setlength{\itemsep}{1em plus 0.5em minus 0.5em}%
  \setlength{\parsep}{0.4em}%
}
\renewcommand{\partshook}{%
  \setlength{\topsep}{0.25em}%
  \setlength{\partopsep}{0pt}%
  \setlength{\parskip}{0pt}%
  \setlength{\parsep}{0pt}%
  \setlength{\itemsep}{0pt}%
}
\renewcommand{\subpartshook}{%
  \setlength{\topsep}{0.25em}%
  \setlength{\partopsep}{0pt}%
  \setlength{\parskip}{0pt}%
  \setlength{\parsep}{0pt}%
  \setlength{\itemsep}{0pt}%
}
\renewcommand{\choiceshook}{%
  % Reduce the extra 2.5em indent that exam.cls adds for choices.
  \settowidth{\leftmargin}{W.\hskip\labelsep\hskip 1em}%
  \labelwidth\leftmargin\advance\labelwidth-\labelsep
  \setlength{\topsep}{0.4em}%
  \setlength{\partopsep}{0pt}%
  \setlength{\itemsep}{0.2em}%
  \setlength{\parsep}{0pt}%
}
\renewcommand{\checkboxeshook}{%
  \settowidth{\leftmargin}{W.\hskip\labelsep\hskip 1em}%
  \labelwidth\leftmargin\advance\labelwidth-\labelsep
  \setlength{\topsep}{0.4em}%
  \setlength{\partopsep}{0pt}%
  \setlength{\itemsep}{0.2em}%
  \setlength{\parsep}{0pt}%
}

\newtcolorbox{examrules}[1][]{%
  enhanced jigsaw,
  sharp corners,
  colback=white,
  borderline={1pt}{-2pt}{black},
  #1
}

\BLOCK{ set document_type = exam_type_raw|lower }
\BLOCK{ if document_type in ["te", "travail ecrit", "travail écrit"] }
\BLOCK{ set document_type_label = "Travail \\'Ecrit" }
\BLOCK{ else }
\BLOCK{ set document_type_label = exam_type_raw|default("Exam") }
\BLOCK{ endif }
\BLOCK{ set display_title = title_value if title_value else (document_type_label ~ " " ~ course) }
\BLOCK{ set author_payload = author|default('') }
\BLOCK{ set author_ns = namespace(lines=[]) }
\BLOCK{ if author_payload is string }
  \BLOCK{ set author_text = author_payload|trim }
  \BLOCK{ if author_text }
    \BLOCK{ set author_ns.lines = [author_text|latex_escape] }
  \BLOCK{ endif }
\BLOCK{ elif author_payload is mapping }
  \BLOCK{ set author_name = author_payload.get('name')|default('')|trim }
  \BLOCK{ set author_aff = author_payload.get('affiliation')|default('')|string|trim }
  \BLOCK{ set author_aff_lower = author_aff|lower }
  \BLOCK{ if author_aff_lower in ['none', 'null'] }
    \BLOCK{ set author_aff = '' }
  \BLOCK{ endif }
  \BLOCK{ if author_name }
    \BLOCK{ set author_ns.lines = [author_name|latex_escape] }
    \BLOCK{ if author_aff }
      \BLOCK{ set author_ns.lines = author_ns.lines + [author_aff|latex_escape] }
    \BLOCK{ endif }
  \BLOCK{ endif }
\BLOCK{ elif author_payload is sequence }
  \BLOCK{ for entry in author_payload }
    \BLOCK{ if entry is string }
      \BLOCK{ set entry_name = entry|trim }
      \BLOCK{ set entry_aff = '' }
    \BLOCK{ elif entry is mapping }
      \BLOCK{ set entry_name = entry.get('name')|default('')|trim }
      \BLOCK{ set entry_aff = entry.get('affiliation')|default('')|string|trim }
      \BLOCK{ set entry_aff_lower = entry_aff|lower }
      \BLOCK{ if entry_aff_lower in ['none', 'null'] }
        \BLOCK{ set entry_aff = '' }
      \BLOCK{ endif }
    \BLOCK{ else }
      \BLOCK{ set entry_name = '' }
      \BLOCK{ set entry_aff = '' }
    \BLOCK{ endif }
    \BLOCK{ if entry_name }
      \BLOCK{ set author_ns.lines = author_ns.lines + [entry_name|latex_escape] }
      \BLOCK{ if entry_aff }
        \BLOCK{ set author_ns.lines = author_ns.lines + [entry_aff|latex_escape] }
      \BLOCK{ endif }
      \BLOCK{ if not loop.last }
        \BLOCK{ set author_ns.lines = author_ns.lines + [''] }
      \BLOCK{ endif }
    \BLOCK{ endif }
  \BLOCK{ endfor }
\BLOCK{ endif }
\BLOCK{ set author_rendered = author_ns.lines|join('\\\\') }
\BLOCK{ set author_trim = author_rendered|trim }
\BLOCK{ set date_formatted = date_value|exam_date(lang) }
\BLOCK{ set date_trim = date_formatted|default('')|trim }
\BLOCK{ set school_trim = school|default('')|trim }
\BLOCK{ set course_trim = course|default('')|trim }
\BLOCK{ set duration_trim = duration|default('')|string|trim }
\BLOCK{ set header_left_parts = [] }
\BLOCK{ if titlepage_minimal }
  \BLOCK{ if title_value }
    \BLOCK{ set header_left_parts = header_left_parts + [title_value] }
  \BLOCK{ endif }
  \BLOCK{ if subtitle_value }
    \BLOCK{ set header_left_parts = header_left_parts + [subtitle_value] }
  \BLOCK{ endif }
\BLOCK{ else }
  \BLOCK{ if school_trim }
    \BLOCK{ set header_left_parts = header_left_parts + [school_trim] }
  \BLOCK{ endif }
  \BLOCK{ if title_value }
    \BLOCK{ set header_left_parts = header_left_parts + [title_value] }
  \BLOCK{ endif }
\BLOCK{ endif }
\BLOCK{ set header_left_text = header_left_parts|join(' / ') }

\title{\VAR{display_title}}
\author{\VAR{author_rendered}}

\def\thedate{\VAR{date_formatted if date_formatted else "\\today"}}
\def\department{\VAR{department}}
\def\school{\VAR{school}}
\def\subtitle{\VAR{subtitle}}
\def\documentType{\VAR{document_type_label}}
\def\course{\VAR{course}}
\def\duration{\VAR{duration|default('')}}

\makeatletter
\BLOCK{ if titlepage_minimal }
\def\@maketitle{%
  \BLOCK{ if not logo_disabled }
    \BLOCK{ if logo_builtin == 'heig-vd' }
  \logo[top=\VAR{logo_top},left=\VAR{logo_left},height=\VAR{logo_height}]
    \BLOCK{ elif logo_file }
  \ExamLogoCustom{\VAR{logo_file}}{\VAR{logo_left}}{\VAR{logo_top}}{\VAR{logo_height}}
    \BLOCK{ endif }
  \BLOCK{ else }
  \ExamLogoSpacer
  \BLOCK{ endif }
  \begingroup
  \vskip \dimexpr0pt-\headheight-\headsep-\topskip-3mm\relax%
  \noindent%
  \ifprintanswers
    \rlap{%
      \begin{minipage}[t]{\linewidth}%
        \raggedleft\large\textbf{\textcolor{red}{Solution}}%
      \end{minipage}%
    }%
  \fi
  \begin{minipage}[t]{\linewidth}%
    \centering{\LARGE \@title\par}%
  \end{minipage}\par
  \centering
  \vskip .15em%
  {\itshape\subtitle\par}%
  \vskip .5em%
  \BLOCK{ if author_trim }
  {%
    \lineskip .2em%
    \begin{tabular}[t]{c}%
      \@author
    \end{tabular}\par
  }%
  \vskip 0.4em%
  \BLOCK{ endif }
  {\large \noindent\makebox[\linewidth]{\course\hfill\thedate}\par}%
  \vskip 0.2em%
  \BLOCK{ set department_trim = department|default('')|trim }
  \BLOCK{ if school_trim or department_trim }
  \vskip 0.3em%
  {\itshape\school~--~\department\par}%
  \BLOCK{ endif }
  \endgroup
  \vskip 0.6em%
  \hrule
  \vskip 1em%
}
\BLOCK{ else }
\def\@maketitle{%
  \newpage
  \BLOCK{ if not logo_disabled }
    \BLOCK{ if logo_builtin == 'heig-vd' }
  \logo[top=\VAR{logo_top},left=\VAR{logo_left},height=\VAR{logo_height}]
    \BLOCK{ elif logo_file }
  \ExamLogoCustom{\VAR{logo_file}}{\VAR{logo_left}}{\VAR{logo_top}}{\VAR{logo_height}}
    \BLOCK{ endif }
  \BLOCK{ else }
  \ExamLogoSpacer
  \BLOCK{ endif }
  \AddToShipoutPictureFG*{%
    \AtPageUpperLeft{%
      \put(1cm,-2.8cm){%
        \begin{minipage}[t]{\dimexpr\paperwidth-2.5cm\relax}
          \hfill
          \begin{minipage}[t]{5cm}\raggedleft\bfseries \VAR{label_name}~:\end{minipage}%
          \hspace{0.5em}%
          \begin{minipage}[t]{7cm}\fbox{\makebox[\linewidth][l]{\rule{0pt}{0.8cm}}}\end{minipage}%
        \end{minipage}%
      }%
    }%
  }
  \null
  \vskip 1cm%
  \vfil%
  \begin{center}%
    {\Huge \bfseries \@title \par}%
    \vskip .2em%
    {\itshape\subtitle\par}%
    {\itshape\department\par}%
    \vfil
    \ifprintanswers
      {\large \textbf{\textcolor{red}{SOLUTION}}}\par
      \vskip 0.4em%
    \fi
    {\large \thedate}%
    \vskip 1em%
    \BLOCK{ if author_trim }
    {%
      \lineskip .2em%
      \begin{minipage}{0.8 \textwidth}%
        \begin{center}%
          \begin{tabular}[t]{c}%
            \@author
          \end{tabular}\par
        \end{center}%
      \end{minipage}%
    }%
    \vskip 1em%
    \BLOCK{ endif }
  \end{center}%
  \par
  \vskip 1.5em%
}
\BLOCK{ endif }

\firstpageheader{\VAR{header_left_text}}{}{\VAR{label_problem} \thequestion}
\runningheader{\VAR{header_left_text}}{}{\VAR{label_problem} \thequestion}
\footer{\ifthenelse{\equal{\VAR{next_page_advice|lower}}{true}}{\ifthenelse{\value{page}=\pageref{LastPage}}{\VAR{label_end_exam}}{\VAR{label_next_page}}}{}}{\VAR{label_page_of}}{\course}
\pagestyle{headandfoot}
\setlength{\headheight}{14pt}
\setlength{\headsep}{1.5em}
\makeatother

\begin{document}
\BLOCK{ if titlepage_minimal }
  \maketitle
  \thispagestyle{foot}
\BLOCK{ else }
\begin{coverpages}
  \maketitle
  \thispagestyle{empty}

  \begin{center}
    % Make the header bold without changing labels or alignment.
    \makeatletter
    \let\exam@vqword\@vqword
    \let\exam@vpgword\@vpgword
    \let\exam@vtword\@vtword
    \let\exam@vpword\@vpword
    \let\exam@vsword\@vsword
    \def\@vqword{\textbf{\exam@vqword}}
    \def\@vpgword{\textbf{\exam@vpgword}}
    \def\@vtword{\textbf{\exam@vtword}}
    \def\@vpword{\textbf{\exam@vpword}}
    \def\@vsword{\textbf{\exam@vsword}}
    \makeatother
    \gradetable[v][questions]
  \end{center}
  \vfil

\BLOCK{ if rules }
  \begin{center}
    \begin{examrules}
      \textbf{\VAR{label_rules} : }
      \vskip 1em%
      \begin{itemize}
      \BLOCK{ if duration_trim }
        \item Durée du travail \duration~minutes.
      \BLOCK{ endif }
      \BLOCK{ for rule in rules }
        \item \VAR{rule|markdown_to_latex}
      \BLOCK{ endfor }
      \end{itemize}
    \end{examrules}
  \end{center}
  \BLOCK{ endif }
\end{coverpages}
% Add extra spacing between the page edge and the header after the cover page.
\addtolength{\topmargin}{0.5cm}
\BLOCK{ endif }

\BLOCK{ set has_questions = ("\\question" in mainmatter) or ("\\titledquestion" in mainmatter) or ("\\input" in mainmatter) }
\BLOCK{ if has_questions }
\VAR{mainmatter}
\ExamQuestionsEnd
\BLOCK{ else }
\PackageWarning{texsmith}{No questions found; exam.cls may fail to compile.}
\VAR{mainmatter}
\BLOCK{ endif }

\VAR{fragment_backmatter}
\end{document}
