# -*- mode: Perl -*-
# /=====================================================================\ #
# |  mn2e support                                                       | #
# | Implementation for LaTeXML                                          | #
# |=====================================================================| #
# | Part of LaTeXML:                                                    | #
# |  Public domain software, produced as part of work done by the       | #
# |  United States Government & not subject to copyright in the US.     | #
# |---------------------------------------------------------------------| #
# | Bruce Miller <bruce.miller@nist.gov>                        #_#     | #
# | http://dlmf.nist.gov/LaTeXML/                              (o o)    | #
# \=========================================================ooo==U==ooo=/ #
package LaTeXML::Package::Pool;
use strict;
use warnings;
use LaTeXML::Package;

if (LookupValue('@usedcolumn')) {
  RequirePackage('dcolumn'); }
if (LookupValue('@usenatbib')) {
  RequirePackage('natbib'); }
if (LookupValue('@usegraphicx')) {
  RequirePackage('graphicx'); }

#======================================================================
# Front Matter

DefMacro('\title[]{}',
  '\ifx.#1.\else\@add@frontmatter{ltx:toctitle}{#1}\fi'
    . '\@add@frontmatter{ltx:title}{#2}');
DefMacro('\author[]{}', sub { andSplit(T_CS('\lx@author'), $_[2]); });

DefMacro('\newauthor', Tokens());    # ????

DefMacro('\journal{}',    '\@add@frontmatter{ltx:note}[role=journal]{#1}');
DefMacro('\volume{}',     '\@add@frontmatter{ltx:note}[role=volume]{#1}');
DefMacro('\pubyear{}',    '\@add@frontmatter{ltx:note}[role=pubyear]{#1}');
DefMacro('\microfiche{}', '\@add@frontmatter{ltx:note}[role=microfiche]{#1}');
DefMacro('\pagerange{}',  '\@add@frontmatter{ltx:note}[role=pagerange]{#1}');

# Editorial comments (?)
DefConstructor('\BSLquery{}', "<ltx:note role='query'>#1</ltx:note>");
DefConstructor('\aquery{}',   "<ltx:note role='query'>#1</ltx:note>");
DefConstructor('\tquery{}',   "<ltx:note role='query'>#1</ltx:note>");
DefEnvironment('{query}', "<ltx:note role='query'>#body</ltx:note>");
DefConstructor('\authorquery{}{}', "<ltx:note role='query'>#1: #2</ltx:note>");

DefEnvironment('{keywords}', '',
  afterDigest => sub {
    my $frontmatter = LookupValue('frontmatter');
    push(@{ $$frontmatter{'ltx:classification'} },
      ['ltx:classification', { scheme => 'keywords' }, @LaTeXML::LIST]);
    return; });
DefMacro('\nokeywords', Tokens());

#======================================================================
DefMacro('\eqsecnum',
  '\@addtoreset{equation}{section}'
    . '\def\theequation{\arabic{section}.\arabic{equation}}');
DefMacro('\eqsubsecnum',
  '\@addtoreset{equation}{subsection}'
    . '\def\theequation{\arabic{subsection}.\arabic{equation}}');

#======================================================================
# Math & Symbols
DefConstructor('\mn@boldsymbol{}', '#1', bounded => 1, requireMath => 1, font => { forcebold => 1 });

DefMacroI('\balpha',      undef, '\mn@boldsymbol{\alpha}');
DefMacroI('\bbeta',       undef, '\mn@boldsymbol{\beta}');
DefMacroI('\bgamma',      undef, '\mn@boldsymbol{\gamma}');
DefMacroI('\bdelta',      undef, '\mn@boldsymbol{\delta}');
DefMacroI('\bepsilon',    undef, '\mn@boldsymbol{\epsilon}');
DefMacroI('\bzeta',       undef, '\mn@boldsymbol{\zeta}');
DefMacroI('\btheta',      undef, '\mn@boldsymbol{\theta}');
DefMacroI('\biota',       undef, '\mn@boldsymbol{\iota}');
DefMacroI('\bkappa',      undef, '\mn@boldsymbol{\kappa}');
DefMacroI('\blambda',     undef, '\mn@boldsymbol{\lambda}');
DefMacroI('\bmu',         undef, '\mn@boldsymbol{\mu}');
DefMacroI('\bnu',         undef, '\mn@boldsymbol{\nu}');
DefMacroI('\bxi',         undef, '\mn@boldsymbol{\xi}');
DefMacroI('\bpi',         undef, '\mn@boldsymbol{\pi}');
DefMacroI('\brho',        undef, '\mn@boldsymbol{\rho}');
DefMacroI('\bsigma',      undef, '\mn@boldsymbol{\sigma}');
DefMacroI('\btau',        undef, '\mn@boldsymbol{\tau}');
DefMacroI('\bupsilon',    undef, '\mn@boldsymbol{\upsilon}');
DefMacroI('\bphi',        undef, '\mn@boldsymbol{\phi}');
DefMacroI('\bchi',        undef, '\mn@boldsymbol{\chi}');
DefMacroI('\bpsi',        undef, '\mn@boldsymbol{\psi}');
DefMacroI('\bomega',      undef, '\mn@boldsymbol{\omega}');
DefMacroI('\bvarepsilon', undef, '\mn@boldsymbol{\varepsilon}');
DefMacroI('\bvartheta',   undef, '\mn@boldsymbol{\vartheta}');
DefMacroI('\bvarrho',     undef, '\mn@boldsymbol{\varrho}');
DefMacroI('\bvarsigma',   undef, '\mn@boldsymbol{\varsigma}');
DefMacroI('\bvarphi',     undef, '\mn@boldsymbol{\varphi}');
DefMacroI('\bvarpi',      undef, '\mn@boldsymbol{\varpi}');

DefMacroI('\boldeta', undef, '\mn@boldsymbol{\eta}');

# Various degrees etc.
# Copied from aas_support
DefConstructor('\aas@@fstack{}',
  "<ltx:XMApp role='POSTFIX'>"
    . "<ltx:XMTok role='SUPERSCRIPTOP' scriptpos='#scriptpos'/>"
    . "<ltx:XMTok>.</ltx:XMTok>"
    . "<ltx:XMWrap>#1</ltx:XMWrap>"
    . "</ltx:XMApp>",
  properties => { scriptpos => sub { "mid" . $_[0]->getScriptLevel; } },
  mode       => 'math', bounded => 1);
DefMacro('\aas@fstack{}', '\ensuremath{\aas@@fstack{#1}}');
DefMacro('\fd',           '\aas@fstack{d}');
DefMacro('\fh',           '\aas@fstack{h}');
DefMacro('\fm',           '\aas@fstack{m}');
DefMacro('\fs',           '\aas@fstack{s}');
DefMacro('\fdg',          '\aas@fstack{\circ}');
DefMacro('\farcm',        '\aas@fstack{\prime}');
DefMacro('\farcs',        '\aas@fstack{\prime\prime}');
DefMacro('\fp',           '\aas@fstack{p}');
DefPrimitiveI('\degr',   undef, UTF(0xB0));
DefPrimitiveI('\arcmin', undef, "\x{2032}");
DefPrimitiveI('\arcsec', undef, "\x{2033}");

DefConstructor('\squareforqed',
  "?#isMath(<ltx:XMTok role='PUNCT'>\x{220E}</ltx:XMTok>)(\x{220E})");
Let('\sq',       '\squareforeqd');
Let('\proofbox', '\squareforeqd');

DefPrimitiveI('\sun',      undef, "\x{2609}");
DefPrimitiveI('\diameter', undef, "\x{2300}");
DefPrimitiveI('\earth',    undef, "\x{2295}");

DefMath('\la',    "\x{2272}", role => 'RELOP', meaning => 'less-than-or-similar-to');
DefMath('\ga',    "\x{2273}", role => 'RELOP', meaning => 'greater-than-or-similar-to');
DefMath('\cor',   "\x{2258}", role => 'RELOP', meaning => 'corresonds-to');
DefMath('\sol',   "\x{2A9D}", role => 'RELOP', meaning => 'similar-to-or-less-than');
DefMath('\sog',   "\x{2A9E}", role => 'RELOP', meaning => 'similar-to-or-greater-than');
DefMath('\lse',   "\x{2A8D}", role => 'RELOP', meaning => 'less-than-or-similar-to-or-equal');
DefMath('\gse',   "\x{2A8E}", role => 'RELOP', meaning => 'greater-than-or-similar-to-or-equal');
DefMath('\leogr', "\x{2276}", role => 'RELOP', meaning => 'less-than-or-greater-than');
DefMath('\grole', "\x{2277}", role => 'RELOP', meaning => 'greater-than-or-less-than');
DefMath('\loa',   "\x{2A85}", role => 'RELOP', meaning => 'less-than-or-approximately-equals');
DefMath('\goa',   "\x{2A86}", role => 'RELOP', meaning => 'greater-than-or-approximately-equals');
DefMath('\lid',   "\x{2266}", role => 'RELOP', meaning => 'less-than-or-equals');
DefMath('\gid',   "\x{2267}", role => 'RELOP', meaning => 'greater-than-or-equals');

DefMath('\getsto', "\x{21C6}", role => 'ARROW');
DefMath('\leqslant', "\x{2A7D}", role => 'RELOP',
  meaning => 'less-than-or-equals');
DefMath('\geqslant', "\x{2A7E}", role => 'RELOP',
  meaning => 'greater-than-or-equals');
DefPrimitiveI('\micron', undef, UTF(0xB5) . "m");
DefMacro('\ion{}{}', '\text{#1\,\textsc{\lowercase{#2}}}');

DefMacro('\rmn{}',      '\mathrm{#1}');
DefMacro('\romn{}',     '\mathrm{#1}');
DefMacro('\itl{}',      '\mathit{#1}');
DefMacro('\bld{}',      '\mathbf{#1}');
DefMacro('\textbfit{}', '\textbf{\textit{#1}}');
DefMacro('\textbfss{}', '\textbf{\textsf{#1}}');
DefMacro('\mathbfit{}', '\textbf{\textit{#1}}');
DefMacro('\mathbfss{}', '\textbf{\textsf{#1}}');
DefMacro('\bmath{}',    '\mn@boldsymbol{#1}');     # ?
Let('\oldle',  '\le');
Let('\oldleq', '\leq');
Let('\oldge',  '\ge');
Let('\oldgeq', '\geq');

Let('\upi',      '\pi');
Let('\umu',      '\mu');
Let('\upartial', '\partial');

#======================================================================
# Table, Figure, etc.

DefMacro('\contcaption', '\caption{continued}');

DefMacro('\proofname', 'Proof');
DefEnvironment('{proof}',
  '<ltx:proof><ltx:title>#title</ltx:title>#body',
  afterConstruct => sub { $_[0]->maybeCloseElement('ltx:proof'); },
  properties     => sub {
    (title => Digest(T_CS('\proofname'))); });
DefEnvironment('{lquote}', '<ltx:quote>#body</ltx:quote>');
#======================================================================
# Random..

DefMacro('\bsp',                '');
DefMacro('\loadboldmathitalic', '');
DefMacro('\loadboldgreek',      '');
DefMacro('\fixfootnotes',       '');
DefMacro('\bibtitle',           "References");
DefMacro('\bibheadtitle',       "REFERENCES");

DefMacro('\makeRLlabel{}',  '#1');
DefMacro('\makeRRlabel{}',  '#1');
DefMacro('\makenewlabel{}', '#1');
DefMacro('\boxit{}',        '#1');

DefRegister('\smallindent' => Glue('1.5em'));
Let('\fullhline', '\hline');
RawTeX('\newif\ifCUPmtlplainloaded');
RawTeX('\newif\iffirstta');
DefMacro('\sevensize',   '\small');
DefMacro('\plate',       '');
DefMacro('\hexnumber{}', sub { ExplodeText(sprintf('%x', ToString($_[1]))); });
DefMacro('\mathch{}{}',  '\ensuremath{#2}');

Let('\@internalcite', '\cite');
DefMacro('\cite',       '\def\citename##1{##1}\@internalcite');
DefMacro('\shortcite',  '\def\citename##1{}\@internalcite');
DefMacro('\citename{}', '#1');                                    # ?
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Redefine equations (bizarrely) to allow $ within
DefEnvironment('{equation}',
  "<ltx:equation xml:id='#id'>"
    . "#tags"
    . "<ltx:Math mode='display'>"
    . "<ltx:XMath>"
    . "#body"
    . "</ltx:XMath>"
    . "</ltx:Math>"
    . "</ltx:equation>",
  mode         => 'display_math',
  beforeDigest => sub {
    prepareEquationCounter(numbered => 1, preset => 1);
    beforeEquation();
    Let(T_MATH, T_CS('\@dollar@in@mathmode')); },
  afterDigestBody => sub {
    afterEquation($_[1]); },
  locked => 1);

# Note: In ams, this DOES get a number if \tag is used!
DefEnvironment('{equation*}',
  "<ltx:equation xml:id='#id'>"
    . "#tags"
    . "<ltx:Math mode='display'>"
    . "<ltx:XMath>"
    . "#body"
    . "</ltx:XMath>"
    . "</ltx:Math>"
    . "</ltx:equation>",
  mode         => 'display_math',
  beforeDigest => sub {
    prepareEquationCounter(numbered => undef, preset => 1);
    beforeEquation();
    Let(T_MATH, T_CS('\@dollar@in@mathmode')); },
  afterDigestBody => sub {
    afterEquation($_[1]); },
  locked => 1);

#======================================================================

1;
