# -*- mode: Perl -*-
# /=====================================================================\ #
# |  aas_support.sty.ltxml                                              | #
# | Support for various AAS styles/classes 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.     | #
# |---------------------------------------------------------------------| #
# | Thanks to the arXMLiv group for initial implementation              | #
# |    http://arxmliv.kwarc.info/                                       | #
# | Released to the Public Domain                                       | #
# |---------------------------------------------------------------------| #
# | 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;

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# AAS : American Astronomical Society
# Derived from aasguide
# I have the suspicion that AAS style is strongly related to RevTeX,
# but I don't see it ever made explicit.
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
RequirePackage('aas_macros');

### New as of august 2015:
RequirePackage('url');
RequirePackage('longtable');
RequirePackage('xcolor');
RequirePackage('hyperref');
RequirePackage('array');
RequirePackage('lineno');
RequirePackage('ulem');
#======================================================================
# 2.1.3 Editorial Information

DefMacro('\received{}', '\@add@frontmatter{ltx:date}[role=received,name=Received]{#1}');
DefMacro('\revised{}',  '\@add@frontmatter{ltx:date}[role=revised,name=Revised]{#1}');
DefMacro('\accepted{}', '\@add@frontmatter{ltx:date}[role=accepted,name=Accepted]{#1}');

# Could add more metadata..
DefMacro('\journalid{}{}', '');
DefMacro('\articleid{}{}', '');
DefMacro('\paperid{}',     '');
DefMacro('\msid{}',        '');    # Manuscript id.
DefMacro('\added{}',       '');
DefMacro('\replaced{}',    '');
DefMacro('\deleted{}',     '');
DefMacro('\explain{}',     '');
DefMacro('\edit{}{}',      '');

#\ccc{code}
DefMacro('\ccc{}', '');            # Ignorable?
#\cpright{type}{year}   Should be recorded somehow?
# type is AAS, ASP, PD, none
DefMacro('\cpright{}{}', '\@add@frontmatter{ltx:note}[role=copyright]{\copyright #2: #1}');

# Editorial additions?
DefMacro('\journal{}',    '');
DefMacro('\volume{}',     '');
DefMacro('\issue{}',      '');
DefMacro('\SGMLbi{}',     '#1');
DefMacro('\SGMLbsc{}',    '#1');
DefMacro('\SGMLclc{}',    '#1');
DefMacro('\SGMLentity{}', '#1');    # Actually should produce &#1; !!!
DefMacro('\SGML{}',       '');

#======================================================================
# 2.1.4 Short Comment

DefMacro('\slugcomment{}', '\@add@frontmatter{ltx:note}[role=slugcomment]{#1}');

#======================================================================
# 2.1.5 Running Heads

DefMacro('\shorttitle{}',   '\@add@frontmatter{ltx:toctitle}{#1}');
DefMacro('\shortauthors{}', '');                                      # not useful?
# Meant to be used redundantly with an \author macro, so just register the correspondence here
DefMacro('\correspondingauthor{}', '\lx@contact{correspondent}{#1}');

# I had thought that \lefthead,\righthead were obsolete forms of \shorttitle,\shortauthors,
# but who knows which goes on which side.... anyway, they get misused too.
# better have them just disappear.
DefMacro('\lefthead{}',  '');    # Obsolete form
DefMacro('\righthead{}', '');    # Obsolete form

#======================================================================
# 2.2 Starting the Main Body
# normal LaTeX

#======================================================================
# 2.3 Title and Author Information
AssignMapping('DOCUMENT_CLASSES', ltx_authors_multiline => 1);
DefConstructor('\@@personname[]{}', "<ltx:personname>" .
    "?#1(<ltx:ref href='https://orcid.org/#1' class='orcid'>#2</ltx:ref>)(#2)" .
    "</ltx:personname>)");
DefMacro('\author[]{}',
  '\@add@frontmatter{ltx:creator}[role=author]{\@@personname[#1]{#2}}');

DefConstructor('\@@@affiliation{}', "^ <ltx:contact role='affiliation'>#1</ltx:contact>");
DefMacro('\affiliation{}', '\@add@to@frontmatter{ltx:creator}{\@@@affiliation{#1}}');
DefMacro('\affil',         '\affiliation');
DefConstructor('\@@@altaffil{}', "^ <ltx:contact role='affiliation'>#1</ltx:contact>");
DefMacro('\altaffiliation{}', '\@add@to@frontmatter{ltx:creator}{\@@@altaffil{#1}}');
DefConstructor('\@@@authoraddr{}', "^ <ltx:contact role='address'>#1</ltx:contact>");
DefMacro('\authoraddr{}', '\@add@to@frontmatter{ltx:creator}{\@@@authoraddr{#1}}');

DefConstructor('\@@@email{}', "^ <ltx:contact role='email'>#1</ltx:contact>");
DefMacro('\email{}', '\@add@to@frontmatter{ltx:creator}{\@@@email{#1}}');
# Redefine to straight email address after document begin.
AddToMacro(T_CS('\@startsection@hook'), TokenizeInternal('\let\email\@@email'));

DefPrimitive('\and', undef);
DefMacro('\authoremail', '\email');    # Obsolete form

# NOTE: the the footnote machinery in LaTeX.pool will connect these.
# Ideally, we'd like these comma separated!
DefMacro('\altaffilmark{}', sub {
    my ($gullet, $marks) = @_;
    map { (T_CS('\@altaffilmark'), T_BEGIN, @$_, T_END) } SplitTokens($marks, T_OTHER(',')); });
DefConstructor('\@altaffilmark{}',
  "<ltx:note role='affiliationmark' mark='#1'/> ");
DefConstructor('\altaffiltext{}{}',
  "<ltx:note role='affiliationtext' mark='#1'>#2</ltx:note>");

DefMacro('\software{}',      '\@add@frontmatter{ltx:note}[role=software]{#1}');
DefMacro('\submitjournal{}', '\@add@frontmatter{ltx:note}[role=journal]{#1}');
# Alas \doi is not frontmatter.
DefConstructor('\doi{}',             '<ltx:ref href="https:/doi.org/#1">#1</ltx:ref>');
DefConstructor('\@@@collaborator{}', "<ltx:note role='collaborator'>#1</ltx:note>");
DefMacro('\collaboration{}{}', '\@add@to@frontmatter{ltx:creator}{\@@@collaborator{#2}}');
DefMacro('\nocollaboration{}', '');
#======================================================================
# 2.4 Abstract
# normal LaTeX

#======================================================================
# 2.5 Keywords

DefMacro('\keywords{}', '\@add@frontmatter{ltx:keywords}[name={\@ifundefined{keywordsname}{}{\keywordsname}}]{#1}');
Let('\subjectheadings', '\keywords');

#======================================================================
# 2.6 Comments to Editors

# Perhaps this should actually disappear?
# DefConstructor('\notetoeditor{}',"<ltx:note role='toeditor'>#1</ltx:note>");
DefMacro('\notetoeditor{}', '');

NewCounter('editornote');
DefMacroI('\theeditornote', undef, 'E\arabic{editornote}');
#======================================================================
# 2.7 Sections
# normal LaTeX

# Except that they apparently allow subsubsubsections! Ugh!
#======================================================================
# 2.8 Figure and Table Placement

# These tell where the table/figure labeled with \label{key} ought to appear;
# the assumption is the tables/figures are at the end of the document.
# Best would be if we moved them to there the \placeXXX is!
# \placetable{key}
# \placefigure{key}
# For now, we ignore them, however.
DefMacro('\placetable{}',  '');
DefMacro('\placefigure{}', '');
DefMacro('\placeplate{}',  '');

NewCounter('plate');
DefMacroI('\platename', undef, 'Plate');

# Is there any kind of "list of plates" ? Then we'd need @inlists
DefEnvironment('{plate}[]',
  "<ltx:float xml:id='#id' inlist='#inlist' ?#1(placement='#1') class='ltx_float_plate'>"
    . "#tags"
    . "#body"
    . "</ltx:float>",
  beforeDigest => sub { DefMacroI('\@captype', undef, 'plate'); },
  afterDigest  => sub { RescueCaptionCounters('plate', $_[1]); });

DefEnvironment('{plate*}[]',
  "<ltx:float xml:id='#id' inlist='#inlist' ?#1(placement='#1') class='ltx_float_plate'>"
    . "#tags"
    . "#body"
    . "</ltx:float>",
  beforeDigest => sub { DefMacroI('\@captype', undef, 'plate'); },
  afterDigest  => sub { RescueCaptionCounters('plate', $_[1]); });

DefMacro('\platewidth{Dimension}', '');                    # Ignorable?
DefMacro('\platenum{}',            '\def\theplate{#1}');

DefMacro('\gridline{}', '');
DefMacro('\fig Semiverbatim {Dimension}{}',
  '\begin{figure}\caption{#3}\includegraphics[width=#2]{#1}\end{figure}');
Let('\leftfig',  '\fig');
Let('\rightfig', '\fig');
Let('\boxedfig', '\fig');
DefMacro('\rotatefig {Number} Semiverbatim {Dimension}{}',
  '\begin{figure}\caption{#4}\includegraphics[width=#3,angle=#1]{#2}\end{figure}');

DefEnvironment('{interactive}{}{}', '#body');

#======================================================================
# 2.9 Acknowledgements

# acts like \section{Acknowledgements}, rather than container.
Tag("ltx:acknowledgements", autoClose => 1);
DefConstructor('\acknowledgements', "<ltx:acknowledgements>");
Let('\acknowledgments', '\acknowledgements');

#======================================================================
# 2.10 Facilities

# Ultimately, this would get some more explicit semantic markup, but..
# \facility{facilityID}
DefConstructor('\facility{}', "<ltx:text class='ltx_ast_facility'>#1</ltx:text>");
DefMacro('\facilities{}', '\@add@frontmatter{ltx:note}[role=facilities]{#1}');

#======================================================================
# 2.11 Appendices
# almost normal LaTeX
DefMacro('\appendix', '\@appendix');
DefPrimitive('\@appendix', sub {
    startAppendices('section');
    NewCounter('equation', 'section', idprefix => 'E');
    DefMacro('\theequation', '\thesection\arabic{equation}', scope => 'global'); });

#======================================================================
# 2.12 Equations
# mostly normal LaTeX

# Basically, {mathletters} is a copy of AMSMath's {subequations}... Isn't it?
DefMacro('\mathletters',    '\lx@equationgroup@subnumbering@begin');
DefMacro('\endmathletters', '\lx@equationgroup@subnumbering@end');

# \eqnum{text}  specifies equation number inside an equation
# Basically, AMSMath's \tag ?
DefMacro('\eqnum {}', '\lx@equation@settag{\edef\theequation{#2}\lx@make@tags{equation}}');

#======================================================================
# 2.13 Citations and Bibliography

DefMacro('\markcite{}', '');    # apparently like \cite w/o text?
#======================================================================
# 2.13.1 The thebibliography Environment
# normal LaTeX

#======================================================================
# 2.13.2 Specifying Bibliographic and Citation Information

RequirePackage('natbib');

# \bibitem[author(year)]{key} bibdata...

#======================================================================
# 2.13.3 The references Environment

# Is this the right treatment?
# Is the same true for RevTeX?
DefConstructor('\references',
  "<ltx:bibliography xml:id='#id' "
    . "bibstyle='#bibstyle' citestyle='#citestyle' sort='#sort'>"
    . "<ltx:title>#title</ltx:title>"
    . "<ltx:biblist>",
  afterDigest => sub { beginBibliography($_[1]); });
DefConstructor('\endreferences',
  "</ltx:biblist></ltx:bibliography>");

Let('\reference', '\bibitem');

#======================================================================
# 2.14.1 Electronic Art

RequirePackage('graphicx');
# \begin{figure}
#   \figurenum{text}
#   \epsscale{num}
#   \plotone{epsfile}
#   \plottwo{epsfile}{epsfile}
#   \caption{text}
# \end{figure}
DefMacro('\figurenum{}', '\def\thefigure{#1}');
# Note: This needs an UnRefStepCounter('figure');
# or at least, defer the refstep till late enough to skip if needed?

DefMacro('\epsscale{}',           '');
DefMacro('\plotone Semiverbatim', '\includegraphics[width=\textwidth]{#1}');
DefMacro('\plottwo Semiverbatim Semiverbatim',
  '\hbox{\includegraphics[width=\textwidth]{#1}\includegraphics[width=\textwidth]{#2}}');

# \plotfiddle{epsfile}{vsize}{rot}{hsf}{vsf}{htrans}{vtrans}
# Ugh...
DefMacro('\plotfiddle Semiverbatim {}{}{}{}{}{}',
  '\includegraphics[width=#4pt,height=#5pt]{#1}');

#======================================================================
# 2.14.2 Figure Captions

# For figures added externally; Used at end of file.
# \figcaption[filename]{text\label{key}}
# But sometimes used just like \caption!
DefMacro('\@figcaption {}', '\begin{figure}#1\end{figure}');
# Note that the optional [filename] seems unused, so we just read and drop it.
DefMacro('\figcaption OptionalSemiverbatim', sub {
    (((LookupValue('current_environment') || '') =~ 'figure')
      ? T_CS('\caption') : T_CS('\@figcaption')); });

#======================================================================
# 2.15 Tables
RequirePackage('deluxetable');
Let(T_CS('\begin{planotable}'), T_CS('\begin{deluxetable}'));
Let(T_CS('\end{planotable}'),   T_CS('\end{deluxetable}'));

DefConditional('\ifcolnumberson');
DefConditional('\ifdeluxedecimals');
DefMacro('\deluxedecimals', '\global\deluxedecimalstrue');
RawTeX('\global\deluxedecimalsfalse');
Let(T_CS('\decimals'), T_CS('\deluxedecimals'));
DefMacro('\colnumbers',              '');                                       # TODO
DefMacro('\deluxedecimalcolnumbers', '\deluxedecimalstrue\colnumbersontrue');
Let(T_CS('\decimalcolnumbers'), T_CS('\deluxedecimalcolnumbers'));

# http://tug.ctan.org/tex-archive/macros/latex/contrib/aastex/sample63.pdf
# decimal alignment, 3.1.2
sub build_d_columns {
  $LaTeXML::BUILD_TEMPLATE->addColumn(
    before => Tokens(T_CS('\hfill'), T_CS('\aas@start@D@column')),
    after  => Tokens(T_CS('\aas@end@D@column')));
  $LaTeXML::BUILD_TEMPLATE->addColumn(
    after => Tokens(T_CS('\hfill')));
  return; }
DefColumnType('D', \&build_d_columns);
DefColumnType('d', \&build_d_columns);
DefMacro('\aas@start@D@column XUntil:\aas@end@D@column', sub {
    my ($gullet, $n) = @_;
    my ($m,      $f) = SplitTokens($n, T_OTHER('.'));
    return ($f ? (@$m,
        T_CS('\@ADDCLASS'), T_BEGIN, T_OTHER('ltx_norightpad'), T_END,
        T_CS('\@alignment@align'),
        T_CS('\@ADDCLASS'), T_BEGIN, T_OTHER('ltx_noleftpad'), T_END,
        T_OTHER('.'), @$f)
      : ($n,
        T_CS('\@ADDCLASS'), T_BEGIN, T_OTHER('ltx_norightpad'), T_END,
        T_CS('\@alignment@align')));
});
DefPrimitive('\aas@end@D@column', '');

# hidden column, 3.1.4
Let(T_CS('\savedollar'), T_MATH);
DefEnvironment('{eatone}', '');
DefColumnType('h', sub {
    $LaTeXML::BUILD_TEMPLATE->addColumn(
      before => Tokens(T_BEGIN,            T_CS('\eatone')),
      after  => Tokens(T_CS('\endeatone'), T_END));
    return; });
Let(T_CS('\begin{splitdeluxetable}'),  T_CS('\begin{deluxetable}'));
Let(T_CS('\end{splitdeluxetable}'),    T_CS('\end{deluxetable}'));
Let(T_CS('\begin{splitdeluxetable*}'), T_CS('\begin{deluxetable*}'));
Let(T_CS('\end{splitdeluxetable*}'),   T_CS('\end{deluxetable*}'));

DefColumnType('B', sub {    # TODO: fake for now, should break table eventually
    $LaTeXML::BUILD_TEMPLATE->addColumn(
      before => Tokens(T_BEGIN,            T_CS('\eatone')),
      after  => Tokens(T_CS('\endeatone'), T_END));
    return; });

DefEnvironment('{longrotatetable}', '#body');

DefMacro('\phn',   '\phantom{0}');
DefMacro('\phd',   '\phantom{.}');
DefMacro('\phs',   '\phantom{+}');
DefMacro('\phm{}', '\phantom{string}');

#======================================================================
# 2.15.4 The table Environment
# normal LaTeX

#======================================================================
# 2.17 Miscellaneous
#======================================================================
# 2.17.1 Celestial Objects and Data Sets

# Ultimately, this would get some more explicit semantic markup, but..
# \objectname[catalogid]{text}
# \dataset[catalogid]{text}
DefConstructor('\objectname OptionalSemiverbatim {}',
  "<ltx:text class='ltx_ast_objectname'>#2 (catalog #1)</ltx:text>");
Let('\object', '\objectname');    # ???
DefConstructor('\dataset OptionalSemiverbatim {}  ',
  "<ltx:text class='ltx_ast_dataset'>#2 (catalog #1)</ltx:text>");

#======================================================================
# 2.17.2 Ionic Species and Chemical Bonds

# Note that semantics could be useful!
#  \ion{element}{level}
DefMacro('\ion{}{}', '{#1 \uppercase{\romannumeral #2}}');

# NOTE: These are almost totally wrong...
DefPrimitiveI('\sbond', undef, "\x{2212}");
DefPrimitiveI('\dbond', undef, "=");
DefPrimitiveI('\tbond', undef, "\x{2261}");

#======================================================================
# 2.17.3 Fractions

# \case{1}{2} == textstyle fraction
# AND, apparently allowed in text mode!
DefMacro('\case{}{}', '\ensuremath{\text@frac{#1}{#2}}');
DefConstructor('\text@frac ScriptStyle ScriptStyle',
  "<ltx:XMApp>"
    . "<ltx:XMTok meaning='divide' role='FRACOP' mathstyle='text'/>"
    . "<ltx:XMArg>#1</ltx:XMArg><ltx:XMArg>#2</ltx:XMArg>"
    . "</ltx:XMApp>",
  sizer => sub { fracSizer($_[0]->getArg(1), $_[0]->getArg(2)); });
Let('\slantfrac', '\case');
#======================================================================
# 2.17.4 Astronomical Symbols
# See aassymbols document.
# Table 1: Additional AASTeX symbols

DefPrimitiveI('\micron', undef, UTF(0xB5) . "m");
DefMacro('\Sun', '\sun');
DefMacro('\Sol', '\sun');
DefPrimitiveI('\sun',     undef, "\x{2609}");
DefPrimitiveI('\Mercury', undef, "\x{263F}");
DefPrimitiveI('\Venus',   undef, "\x{2640}");
DefMacro('\Earth', '\earth');
DefMacro('\Terra', '\earth');
DefPrimitiveI('\earth',   undef, "\x{2295}");
DefPrimitiveI('\Mars',    undef, "\x{2642}");
DefPrimitiveI('\Jupiter', undef, "\x{2643}");
DefPrimitiveI('\Saturn',  undef, "\x{2644}");
DefPrimitiveI('\Uranus',  undef, "\x{2645}");
DefPrimitiveI('\Neptune', undef, "\x{2646}");
DefPrimitiveI('\Pluto',   undef, "\x{2647}");
DefPrimitiveI('\Moon',    undef, "\x{263D}");    # Not sure if this is the right Moon?
DefMacro('\Luna', '\Moon');
DefPrimitiveI('\Aries', undef, "\x{2648}");
DefMacro('\VEq', '\Aries');                      # Vernal Equinox
DefPrimitiveI('\Taurus', undef, "\x{2649}");
DefPrimitiveI('\Gemini', undef, "\x{264A}");
DefPrimitiveI('\Cancer', undef, "\x{264B}");
DefPrimitiveI('\Leo',    undef, "\x{264C}");
DefPrimitiveI('\Virgo',  undef, "\x{264D}");
DefPrimitiveI('\Libra',  undef, "\x{264E}");
DefMacro('\AEq', '\Libra');                      # Autumnal Equinox
DefPrimitiveI('\Scorpius',    undef, "\x{264F}");
DefPrimitiveI('\Sagittarius', undef, "\x{2650}");
DefPrimitiveI('\Capricornus', undef, "\x{2651}");
DefPrimitiveI('\Aquarius',    undef, "\x{2652}");
DefPrimitiveI('\Pisces',      undef, "\x{2653}");

DefPrimitiveI('\diameter', undef, "\x{2300}");
DefPrimitiveI('\sq',       undef, "\x{25A1}");

DefPrimitiveI('\arcdeg', undef, UTF(0xB0));
Let('\degr', '\arcdeg');
DefPrimitiveI('\arcmin', undef, "\x{2032}");
DefPrimitiveI('\arcsec', undef, "\x{2033}");
DefMacro('\nodata', ' ~$\cdots$~ ');

# 1st arg is the original macro so that it can be the reversion for UnTeX!
# 2nd arg is the superscript
DefConstructor('\aas@@fstack Undigested {}',
  "<ltx:XMApp role='POSTFIX'>"
    . "<ltx:XMTok role='SUPERSCRIPTOP' scriptpos='#scriptpos'/>"
    . "<ltx:XMTok>.</ltx:XMTok>"
    . "<ltx:XMWrap>#2</ltx:XMWrap>"
    . "</ltx:XMApp>",
  properties => { scriptpos => sub { "mid" . $_[0]->getScriptLevel; } },
  mode => 'math', font => { shape => 'upright' }, bounded => 1, reversion => '#1');
DefMacro('\aas@fstack{}', '\ensuremath{\aas@@fstack{#1}}');

DefMacro('\fd',    '\ensuremath{\@fd}');
DefMacro('\fh',    '\ensuremath{\@fh}');
DefMacro('\fm',    '\ensuremath{\@fm}');
DefMacro('\fs',    '\ensuremath{\@fs}');
DefMacro('\fdg',   '\ensuremath{\@fdg}');
DefMacro('\farcm', '\ensuremath{\@farcm}');
DefMacro('\farcs', '\ensuremath{\@farcs}');
DefMacro('\fp',    '\ensuremath{\@fp}');

DefMath('\@fd',  '\aas@@fstack{\fd}{d}',      role => 'ID', meaning => 'day',    alias => '\fd');
DefMath('\@fh',  '\aas@@fstack{\fh}{h}',      role => 'ID', meaning => 'hour',   alias => '\fh');
DefMath('\@fm',  '\aas@@fstack{\fm}{m}',      role => 'ID', meaning => 'minute', alias => '\fm');
DefMath('\@fs',  '\aas@@fstack{\fs}{s}',      role => 'ID', meaning => 'second', alias => '\fs');
DefMath('\@fdg', '\aas@@fstack{\fdg}{\circ}', role => 'ID', meaning => 'degree', alias => '\fdg');
DefMath('\@farcm', '\aas@@fstack{\farcm}{\prime}', role => 'ID', meaning => 'arcminute', alias => '\farcm');
DefMath('\@farcs', '\aas@@fstack{\farcs}{\prime\prime}', role => 'ID', meaning => 'arcsecond', alias => '\farcs');
DefMath('\@fp', '\aas@@fstack{\fp}{p}');

DefMacro('\onehalf', '\ifmmode\case{1}{2}\else\text@onehalf\fi');
DefPrimitiveI('\text@onehalf', undef, UTF(0xBD));
DefMacro('\onethird', '\ifmmode\case{1}{3}\else\text@onethird\fi');
DefPrimitiveI('\text@onethird', undef, "\x{2153}");
DefMacro('\twothirds', '\ifmmode\case{2}{3}\else\text@twothirds\fi');
DefPrimitiveI('\text@twothirds', undef, "\x{2154}");
DefMacro('\onequarter', '\ifmmode\case{1}{4}\else\text@onequarter\fi');
DefPrimitiveI('\text@onequarter', undef, UTF(0xBC));
DefMacro('\threequarters', '\ifmmode\case{3}{4}\else\text@threequarters\fi');
DefPrimitiveI('\text@threequarters', undef, UTF(0xBE));
DefPrimitiveI('\ubvr', undef, "UBVR",       bounded => 1, font => { shape => 'italic' });
DefPrimitiveI('\ub',   undef, "U\x{2000}B", bounded => 1, font => { shape => 'italic' });
DefPrimitiveI('\bv',   undef, "B\x{2000}V", bounded => 1, font => { shape => 'italic' });
DefPrimitiveI('\vr',   undef, "V\x{2000}R", bounded => 1, font => { shape => 'italic' });
DefPrimitiveI('\ur',   undef, "U\x{2000}R", bounded => 1, font => { shape => 'italic' });

# Remaining tables standard LaTeX or amssymb
RequirePackage('latexsym');
RequirePackage('amssymb');

# \lesssim,\gtrsim in amssymb
Let('\la', '\lesssim');
Let('\ga', '\gtrsim');

# Nominal Conversion Constants

DefMacro('\nomSolarEffTemp', '\leavevmode\hbox{\boldmath$\mathcal{T}^{\rm N}_{\mathrm{eff}\odot}$}');
DefMacro('\nomTerrEqRadius',      '\leavevmode\hbox{\boldmath$\mathcal{R}^{\rm N}_{E\mathrm e}$}');
DefMacro('\nomTerrPolarRadius',   '\leavevmode\hbox{\boldmath$\mathcal{R}^{\rm N}_{E\mathrm p}$}');
DefMacro('\nomJovianEqRadius',    '\leavevmode\hbox{\boldmath$\mathcal{R}^{\rm N}_{J\mathrm e}$}');
DefMacro('\nomJovianPolarRadius', '\leavevmode\hbox{\boldmath$\mathcal{R}^{\rm N}_{J\mathrm p}$}');
DefMacro('\nomTerrMass',   '\leavevmode\hbox{\boldmath$(\mathcal{GM})^{\rm N}_{\mathrm E}$}');
DefMacro('\nomJovianMass', '\leavevmode\hbox{\boldmath$(\mathcal{GM})^{\rm N}_{\mathrm J}$}');
DefMacro('\Qnom',          '\leavevmode\hbox{\boldmath$\mathcal{Q}^{\rm N}_{\odot}$}');
Let(T_CS('\Qn'), T_CS('\Qnom'));

# Generic commands that can be given an argument:
DefMacro('\nom{}',   '\leavevmode\hbox{\boldmath$\mathcal{#1}^{\rm N}_{\odot}$}');
DefMacro('\Eenom{}', '\leavevmode\hbox{\boldmath$\mathcal{#1}^{\rm N}_{Ee}$}');
DefMacro('\Epnom{}', '\leavevmode\hbox{\boldmath$\mathcal{#1}^{\rm N}_{Ep}$}');
DefMacro('\Jenom{}', '\leavevmode\hbox{\boldmath$\mathcal{#1}^{\rm N}_{Je}$}');
DefMacro('\Jpnom{}', '\leavevmode\hbox{\boldmath$\mathcal{#1}^{\rm N}_{Jp}$}');

#======================================================================
# 2.17.5 Hypertext Constructs
# \anchor{href}{text}
RequirePackage('url');

DefConstructor('\anchor Semiverbatim Semiverbatim',
  "<ltx:ref href='#href'>#2</ltx:ref>",
  properties => sub { (href => ComposeURL(LookupValue('BASE_URL'), $_[1])); });

# This should be in effect only after frontmatter
# \email{address}
DefConstructor('\@@email Semiverbatim',
  "<ltx:ref href='#href'>#1</ltx:ref>",
  properties => sub { (href => CleanURL("mailto:" . ToString($_[1]))); });

# RequirePackage('verbatim');

#======================================================================
# 2.18 Concluding
# normal LaTeX

# Number equations within sections
DefMacro('\eqsecnum',
  '\@addtoreset{equation}{section}'
    . '\def\theequation{\arabic{section}-\arabic{equation}}');

#======================================================================
# Random extra bits
DefMacroI('\singlespace',    undef, '');
DefMacroI('\doublespace',    undef, '');
DefMacroI('\tighten',        undef, '');
DefMacroI('\tightenlines',   undef, '');
DefMacroI('\nohyphenation',  undef, '');
DefMacroI('\offhyphenation', undef, '');
DefMacroI('\ptlandscape',    undef, '');
DefMacroI('\refpar',         undef, '');
DefMacroI('\traceoutput',    undef, '');
DefMacroI('\tracingplain',   undef, '');
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

### These commands requested AAS
DefMacro('\noprint {}',        '');
DefMacro('\figsetstart',       '{\bf Fig. Set}');
DefMacro('\figsetend',         '');
DefMacro('\figsetgrpstart',    '');
DefMacro('\figsetgrpend',      '');
DefMacro('\figsetnum {}',      '{\bf #1.}');
DefMacro('\figsettitle {}',    '{\bf #1}');
DefMacro('\figsetgrpnum {}',   '');
DefMacro('\figsetgrptitle {}', '');
DefMacro('\figsetplot {}',     '');
DefMacro('\figsetgrpnote {}',  '');

1;
