%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LaTeX class file for The Observatory
%% Based on work by Chris Lloyd
%% Written by Phillip Helbig, October 2025
%%
%% Dimensions in pt inherited from Chris (no pt dimensions changed)
%% New dimensions in mm
%% Inherited dimensions in inches changed to mm, value sometimes changed

%% Key to comments
%%   %% | documentation which should stay
%%   %|   lines which might be uncommented at some point
%%   %%%| consider deleting the corresponding line entirely or use it and remove comments and comment characters
%%   command %inline comment (which should stay)

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{obsmag}[2025/10/30]

%RequirePackage[2024/11/01]{latexrelease} %de-comment if needed
%\LoadClass[10pt,twoside]{article}
\LoadClass[8pt,twoside]{extarticle}

\RequirePackage{graphicx} 
\RequirePackage[super,compress]{natbib}
\RequirePackage[symbol*,norule,multiple,hang,perpage,bottom]{footmisc}
\RequirePackage{fp}
\RequirePackage{fancyhdr}
\RequirePackage[font=footnotesize]{caption}
\RequirePackage{booktabs}
\RequirePackage{indentfirst}
\RequirePackage{titlesec}
\RequirePackage{multirow}
\RequirePackage{mathtools}
\RequirePackage{longtable}
\RequirePackage[dvipsnames]{xcolor}
\RequirePackage{extsizes}
\RequirePackage[greek,english]{babel}
\RequirePackage{subcaption}
\RequirePackage{expl3}[2020/10/01] %probably not needed
\RequirePackage{siunitx}
\RequirePackage{array}
\RequirePackage[colorlinks=true,urlcolor=blue,linkcolor=magenta,citecolor=red]{hyperref} 

%% References
%%
\bibliographystyle{obsmaga}
\setcitestyle{citesep={,}}
\renewcommand\bibnumfmt[1]{(#1)}
\renewcommand{\bibfont}{\footnotesize}
\renewcommand{\bibsection}{\centerline{\itshape\normalsize References}}
\setlength{\bibsep}{.08ex} %%% what is default?

%% Page size
%%
%% The margins and so on are really relevant only when printing on 
%% paper, as most screen viewers will fit the text to the window.  Since
%% The Observatory has a page size substantially smaller than A4 or U.S.
%% letter size, they really don't matter for a single page.  However, if 
%% printing on both sides of the paper, we want the text to line up so 
%% that the extra blank space is the same on both sides of the paper.
%% Thus the arithmetic gymnastics here, complicated by the fact that TeX
%% wants both \oddsidemargin and \evensidemargin, even though one needs
%% only one if the text is to line up properly.  Also, if using letter 
%% rather than A4 paper, only the \textwidth and \textheight need to be
%% changed.
%%
%% A4 paper is 210*297 mm.  US paper is 8.5*11 in or 215.9*279.4 mm.
%% The width of the paper magazine (page spread) is 290 mm (width of two
%% pages plus margins) and the height is 225 mm.  The text width is 110
%% mm.  The text height is actually variable but at most 187 mm.  The
%% text width including the inner margin is 125 mm.  (Thus the outer
%% margin is 20 mm.)  That means that it is possible to print a two-page
%% spread in actual size on one side of A4 or US paper without cutting
%% off any text.  For A4, the outer margins will be larger (23.5 instead
%% of 20 mm) and the top and bottom margins together 15 mm rather than
%% 30 mm.  For US, the outer margins will be smaller (14.7 rather than
%% 20 mm) and the top and bottom margins together 20.9 rather than 30
%% mm.  The area of A4 is slightly more than US, so there will be more
%% margin area altogether, but US paper will be a better fit, with all
%% margins reduced somewhat rather than the outer margins increased
%% slightly but top and bottom reduced greatly. 
%% 
\setlength{\paperwidth}{210mm}  %A4
\setlength{\paperheight}{297mm} %A4
%\setlength{\paperwidth}{8.5in}  %letter
%\setlength{\paperheight}{11in}  %letter
\setlength{\textwidth}{110mm}
\newlength{\outermargin}
\setlength{\outermargin}{20mm}
\newlength{\innermargin}
\setlength{\innermargin}{\dimexpr \paperwidth - \outermargin - \textwidth\relax}
\setlength{\evensidemargin}{\dimexpr \outermargin - 1in\relax}
\setlength{\oddsidemargin}{\dimexpr \innermargin - 1in\relax}
%% top of page to top of header: 15mm
%% height of header: 3mm
%% bottom of header to top of text: 5mm
%% bottom of main text to footnote: 5mm
%% we have no footers since all information is at the top
%% bottom margin nominally 15mm 
\setlength{\textheight}{187mm}
\setlength{\topmargin}{\dimexpr 15mm - 1in\relax}
\setlength{\headheight}{3mm}
\setlength{\headsep}{5mm}
\setlength{\footnotesep}{3mm}
\setlength{\footnotemargin}{0em}
\setlength{\footskip}{0mm}
\setlength{\parindent}{2.54mm} 
\setlength{\parskip}{0pt plus2pt}
\raggedbottom
%% Theoretically, the above commands should produce a layout 
%% identical to the printed versions of the Magazine; in particular, 
%% the printed area on odd- and even-numbered pages should be the
%% same.  The top and outer margins are correct and don't need to be
%% cropped; the bottom and inner margins will be too big if the paper
%% is larger than the dimensions of the Magazine.  However, on my 
%% printer the following command is needed to make the printed areas
%% on each side correspond.  Presumably it is an idiosyncrasy of my
%% printer and hence might need to be adjusted for other printers.
\setlength{\hoffset}{3.5mm} %2.9
%% I have no need to adjust \voffset but if you do insert the 
%% appropriate value and uncomment the following command.  Of course, 
%% this has nothing to do with even- vs. odd-numbered pages in a 
%% twoside style.  Rather, the reason to adjust it would be so that 
%% the top margin is the required height (top of page to top of 
%% header: 15mm) so that cropping is needed only at the bottom.
%\setlength{\voffset}{3mm} 

%% Formatting
%%
\setcounter{topnumber}{2}
\setcounter{bottomnumber}{2}
\setcounter{totalnumber}{4}
\renewcommand{\topfraction}{.9}
\renewcommand{\bottomfraction}{.8}
\renewcommand{\floatpagefraction}{.7} %must be less than topfraction
\renewcommand{\textfraction}{.2}
\renewcommand{\textfloatsep}{8pt}
\renewcommand{\intextsep}{8pt}

\titlespacing*{\section}{0pt}{10pt plus2pt}{6pt plus1pt}
\titleformat{name=\section}{\normalsize\itshape}{}{0pt}{}
%% following allows \section* to still be used
\titleformat{name=\section,numberless}{\normalsize\itshape}{}{0pt}{}

\let\oldtabular\tabular
\renewcommand{\tabular}{\footnotesize\oldtabular}

\renewcommand{\thetable}{\Roman{table}} 

\frenchspacing

\pagestyle{myheadings}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Environments

\ExplSyntaxOn

%% I need the third command below for the hack for the long author list.
%% That stems from the fact that I am using \newenvironment which has 
%% only one possible optional argument which must be first and am using
%% that for the more common short title.  Long author lists are rare.
%% I could move to \NewDocumentEnvironment with more optional arguments,
%% but that seems like overkill at the moment.
%% Note that if there are several authors, then the argument will be
%% longer than 58 even if it isn't really since the affiliation markers
%% will be present.  But that is OK since by the same token (pun 
%% intended a \shortauthorlist must be specified to get rid of them.

\NewDocumentCommand{\ifstringlongerthan}{m m m m}{
\str_if_longer:nNTF {#1} {#2} {#3} {#4}
}

\NewDocumentCommand{\iflongerthann}{m m m m}{
\int_compare:nNnTF {\str_count:n{#1}} > {#2} {#3} {#4}
}

\NewDocumentCommand{\ifexpandedlongerthann}{m m m m}{
\int_compare:nNnTF {\str_count:n{\text_expand:n{#1}}} > {#2} {#3} {#4}
}

\ExplSyntaxOff

\newcommand*{\shortauthorlist}{}

% --------------------------------------------------
\newcommand*{\obsyear}{}
\newcommand*{\obsmonth}{}
\newcommand*{\obsvolume}{}
\newcommand*{\obsnumber}{}
\newcommand*{\issueinfo}[4]{% <--- used by the user (author/editor)
\renewcommand{\obsvolume}{#1}%
\renewcommand{\obsyear}{#2}%
\renewcommand{\obsmonth}{#3}%
\renewcommand{\obsnumber}{#4}%
}
% Capture fixed parts exactly once at \begin{document}
\AtBeginDocument{%
% Set up fancyhdr page style
\pagestyle{fancy}%
\fancyhf{}%
\fancyhead[LE]{\thepage}%
\fancyhead[CE]{\itshape\leftmark}%
\fancyhead[RE]{Vol.~\obsvolume}%
\fancyhead[LO]{\obsyear~\obsmonth}%
\fancyhead[CO]{\itshape\rightmark}%
\fancyhead[RO]{\thepage}%
\renewcommand{\headrulewidth}{0pt}%
%\setlength{\headwidth}{\textwidth}%
}
  

%% EDITORIAL
\newenvironment{editorial}{%
\markboth{Editorial}{Editorial}
\centering\uppercase{Editorial}
\setcounter{figure}{0}
\setcounter{table}{0}
\setcounter{instno}{0}
}{%
}

%% RAS MEETING
%% day year month date hours minutes place chair position
\newenvironment{RAS}[9]{%
\markboth{#2 #3 #4 Meeting of the}{Royal Astronomical Society}
\centering{\uppercase{Meeting of the Royal Astronomical Society}}
\bigskip
\centering{#1 #2 #3 #4 at #5\textsuperscript{h}\,#6\textsuperscript{m}}
\par
\centering{in #7}
\par
\bigskip
\centering{#8, #9}
\par
\centering{in the Chair}
\setcounter{figure}{0}
\setcounter{table}{0}
\setcounter{instno}{0}
}{%
}

%% ARTICLE
%% [short title,] title, author, institute, abstract
%% [how long can a running header be well pretty long methinks]%
\newenvironment{article}[5][]{%
\@ifempty{#1}{\newcommand*{\titleheader}{#2}}%
{\newcommand*{\titleheader}{#1}}
%above seems to work whether [full], [] or no [] at all
%maybe move to something more complicated (several possibilities
%even with classic \newenvironment
\ifexpandedlongerthann%
{#3}%
{58}%
{\newcommand*{\authorheader}{\shortauthorlist}}%
{\newcommand*{\authorheader}{#3}}%
\begingroup
\markboth{\titleheader}{\authorheader}%
\endgroup
\kern0pt %avoids last page getting header from next page
        \begin{center}
        \parbox{0.81818\textwidth} %9 cm of 11 cm textwidth
        {
        \centering\uppercase{#2}                  %TITLE
        }
        \par
        \end{center}
%\bigskip
        \begin{center}
        \parbox{0.81818\textwidth} %9 cm of 11 cm textwidth
        {
        \centering\textit{#3}                     %AUTHOR
        }
        \par
        \end{center}
%\bigskip
        \begin{center}
        \parbox{0.81818\textwidth} %9 cm of 11 cm textwidth
        {
        \centering{\itshape#4}                     %INSTITUTE
        }
        \par
        \end{center}
%\bigskip
{\large \par #5 \par}   %second \par has no effect; 
\setcounter{figure}{0}  %any solution here must work with and without
\setcounter{table}{0}   %following \section
\setcounter{instno}{0}
}{%
\bigskip
\bigskip
}

%% CORRESPONDENCE
\newcommand{\correspondencebanner}{%
\bigskip\centerline{\uppercase{Correspondence}}\bigskip%
\markboth{Correspondence}{Correspondence}%
}

\newenvironment{correspondence}[1]{}{} %!!! %<---- not yet implemented

%% BOOK REVIEWS
\newcommand{\reviewbanner}{%
\bigskip\centerline{\uppercase{Reviews}}\bigskip%
\markboth{Review}{Review}%
}

\newcommand{\reviewsbanner}{%
\bigskip\centerline{\uppercase{Reviews}}\bigskip%
\markboth{Reviews}{Reviews}%
}

\newenvironment{bookreview1}[5][]% [edited,] title, author, publisher, year
{%
\begingroup %not strictly needed, but keeps definitions local
\hangafter=1
\setlength{\hangindent}{4mm}
\par\noindent\textbf{#2}, \@ifempty{#1}{}{#1\ }by #3 (#4), #5.
}{%
\endgroup%
}
\newenvironment{bookreview2}[7]% pages, height, width, price, type, isbn
{%
\begingroup
\hangafter=1
\setlength{\hangindent}{4mm}
\newcommand{\reviewauthor}{\textsc{#7}}
Pp.~{#1}, #2 $\times$ #3~cm.  Price~#4 (#5; ISBN #6).\par
\medskip
}{%
---\reviewauthor
\endgroup%
\bigskip
}
%% 

%% OTHER BOOKS RECEIVED
\newcommand{\otherbooksreceived}{\textsc{Other Books Received}}

%% FROM THE LIBRARY
\newcommand{\fromthelibrary}{\textsc{From the Library}}

%% CENTENARIES
\newenvironment{centenaries}[1]{%          %%<---- not yet implemented
}{
}

%% THESIS ABSTRACTS
\newcommand{\thesisbanner}{%
\bigskip\centerline{\uppercase{Thesis Abstract}}\bigskip%
\markboth{Thesis Abstract}{Thesis Abstract}%
}

\newcommand{\thesesbanner}{%
\bigskip\centerline{\uppercase{Thesis Abstracts}}\bigskip%
\markboth{Thesis Abstracts}{Thesis Abstracts}%
}

\newenvironment{thesisabstract}[5]% title, author, institute, year, month
{%
\begingroup
\newcommand{\university}{\textit{#3}}
\newcommand{\acceptanceyear}{\textit{#4}}
\newcommand{\acceptancemonth}{\textit{#5}}
\hfill \textsc{#1} \hfill {} \par 
\bigskip
\hfill \textit{By #2} \hfill {} \par 
\bigskip
}{%
---\university; \textit{accepted} \acceptanceyear\ \acceptancemonth
\endgroup%
\bigskip
\bigskip
}

%% OBITUARY
\newcommand{\obituarybanner}{%
\bigskip\centerline{\uppercase{Obituary}}\bigskip%
\markboth{Obituary}{Obituary}%
}
\newcommand{\obituariesbanner}{%
\bigskip\centerline{\uppercase{Obituaries}}\bigskip%
\markboth{Obituaries}{Obituaries}%
}

%% OBITUARY NOTICE
\newcommand{\obituarynoticebanner}{%
\bigskip\centerline{{\uppercase{Obituary notice}}\bigskip}
\markboth{Obituaries}{Obituaries}%
}
\newcommand{\obituarynoticesbanner}{%
\bigskip\centerline{\uppercase{Obituary Notices}}\bigskip%
\markboth{Obituary Notices}{Obituary Notices}%
}

% name birth death author                 %%<---- not yet implemented
% no author for obituary notice
\newenvironment{obituary}[4]{%
\setcounter{figure}{0}
\setcounter{table}{0}
\setcounter{instno}{0}
}{%
}


%% CORRIGENDUM
\newenvironment{corrigendum}[1]{%       %<---- not yet implemented
\setcounter{figure}{0}
\setcounter{table}{0}
\setcounter{instno}{0}
}{%
}

%% HERE AND THERE
\newcommand{\hereandtherebanner}{%
\bigskip\centerline{\textit{Here and There}}\bigskip%
\markboth{Here and There}{Here and There}%
}

\newenvironment{hereandthere}[2]% witty description, source
{%
\begingroup
\newcommand{\source}{#2}
\noindent\textsc{\lowercase{#1}}\par\medskip%
\setcounter{figure}{0}
\setcounter{table}{0}
\setcounter{instno}{0}
}{%
---\source
\endgroup%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Observatory macros

% Definition of banner macro \OBSbanner
\def\OBSbanner#1#2#3 {
	\thispagestyle{empty}
	\begin{center}
                \resizebox{\linewidth}{!}{\bf \huge THE OBSERVATORY}
		\bigskip
		\hrule\vspace*{10pt}
		{\large Vol. #1 \hfill #2 \hfill No. #3}
		\vspace*{8pt}\hrule
		\bigskip
	\end{center}
}


%%% Works, but redundant formatting with environments.
%%% There must be a line break between the last \OBSinst and the }
%%% closing the institute parameter of .e.g. book.  Need to rethink 
%%% this stuff, but the formatting is perfect and there are no warnings.
%%%
% Definition of institution/affiliation macro \OBSinst
 \newcounter{instno}
 \def\OBSinst#1 {\addtocounter{instno}{1}
 	{\centering
 		$^{\arabic{instno}}${\itshape{#1}}
 	\par}
 }

\renewenvironment{abstract}{\large}{\medskip}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  Figures

\DeclareCaptionFormat{figoneline}{%
{\centering#1#2}                          %space is a bit too large
\par
\vspace{2pt}
\noindent
\parbox{\linewidth}{%
\setlength{\parindent}{1em}
#3
}%
\par
}

\captionsetup[figure]{%
belowskip=10pt,          %between caption and text below
format=figoneline,       %see above
justification=centering,
labelsep=newline,        %label on own line
singlelinecheck=false,   %also for one-line captions
skip=16pt                %space between figure and caption
}

\AtBeginDocument{\renewcommand*{\figurename}{\textsc{Fig}.}}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  Tables

\DeclareCaptionFormat{taboneline}{%
{\centering#1#2}                          %space is a bit too large
\par
\vspace{6pt}
\noindent
#3
\par
}

\captionsetup[table]{%
belowskip=10pt,          %between caption and text below
format=taboneline,       %see above
justification=centerlast,
labelfont=normalsize,    %not small like for figures
labelsep=newline,        %label on own line
singlelinecheck=false,   %also for one-line captions
skip=16pt,               %space between figure and caption
textfont=it              %not normalfont like for figures
}

%\AtBeginDocument{\renewcommand*{\tablename}{Table}}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Other control sequences

\newcommand*{\eg}{\emph{e.g.,}}		
\newcommand*{\ie}{\emph{i.e.,}}		
\newcommand*\etc{\emph{etc}\@ifnextchar.{}{.\@}}
\newcommand*\etal{\emph{et~al}\@ifnextchar.{}{.\@}}

\newcommand*{\rightascension}[3]{%
\mbox{
#1\textsuperscript{\normalfont{h}}%
#2\textsuperscript{\normalfont{m}}%
\@ifempty{#3}{}{#3\textsuperscript{\normalfont{s}}}%
}%
}

\newcommand{\filter}[1]{$#1$} %works also for e.g. B-V, otherwise need
                              %better idea

%% end
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

