%
% SNOUG.tex
% Main LaTeX document for formatting SNOUG
%
% SatNOGS Optical Unofficial Guide
%
% Copyright (C) 2022, Jeff Moe
%
% Previous versions of this LaTeX template (with different content):
% Copyright (C) 2017, Jeff Moe
% Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016, 2017 Aleph Objects, Inc.
% Copyright (C) 2009, 2010 Jeff Moe
%
% This document is licensed under the Creative Commons Attribution 4.0
% International Public License (CC BY-SA 4.0) by Jeff Moe.
%

% LaTeX Docs:
% http://en.wikibooks.org/wiki/Category:LaTeX

% LaTeX Memoir Class
% Docs: /usr/share/doc/texlive-doc/latex/memoir/memman.pdf
\documentclass[twoside,10pt,openright,final,english]{memoir}

\usepackage{tikz}
\usetikzlibrary{shadows,shapes,arrows,chains,calc,decorations.pathmorphing,patterns,trees,positioning,automata,fit}
% This breaks things, but would be great to use
%\usepackage{tikz-qtree,tikz-qtree-compat}
\usepackage{rotating}
\usepackage{forest}

\usetikzlibrary{arrows,shapes}
\usetikzlibrary{positioning,calendar,er}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{shapes.geometric}

% Tables
\usepackage{tabularx}

% Source code highlighting
\usepackage{mdframed} % /usr/share/doc/texlive-doc/latex/mdframed/mdframed.pdf
\usepackage{minted}   % /usr/share/doc/texlive-doc/latex/minted/minted.pdf
\setminted{autogobble, breaklines, breakanywhere, breakautoindent, funcnamehighlighting,style=manni,fontsize=\scriptsize}
% fontsize
\setmintedinline{breaklines, breakanywhere}
\surroundwithmdframed{minted}
\mdfsetup{roundcorner=10pt,
linewidth=3pt,
linecolor=jebba-light-blue,
backgroundcolor=white,
leftmargin=1cm,rightmargin=1cm
}

\usepackage{smartdiagram}
\usesmartdiagramlibrary{additions}
\usepackage{comment}  % /usr/share/doc/texlive-doc/latex/comment/comment.pdf
\usepackage{graphicx} % /usr/share/doc/texlive-doc/latex/graphics/graphicx.pdf
\usepackage{epstopdf} % /usr/share/doc/texlive-doc/latex/oberdiek/epstopdf.pdf

\graphicspath{{./figures/}}

\usepackage{xcolor}

%%% LIST OF LISTINGS %%%
%\usepackage{listings}
%%% END LIST OF LISTINGS %%%

%%% PREAMBLE FONTS %%%
% For XeTeX
% http://www.ctan.org/pkg/fontspec
% http://mirrors.ctan.org/macros/latex/contrib/fontspec/fontspec.pdf
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX} % To support LaTeX quoting style
\defaultfontfeatures{Path=fonts/} % Specify the location of font files
% To get font name, run, for example:
% otfinfo -i `kpsewhich lmroman12-regular.otf` | grep "^Full name:"
% STIX
\setsansfont{STIX2Text}[
	UprightFont=*-Regular.otf,
	BoldFont=*-Bold.otf,
	ItalicFont=*-Italic.otf,
	BoldItalicFont=*-BoldItalic.otf
]
% Latin Modern
\setmonofont{LMMono12-Regular.otf}
% Linux Biolinum
\setsansfont{LinBiolinum}[
       UprightFont=*_R.otf,
       BoldFont=*_RB.otf,
       ItalicFont=*_RI.otf
]
%%% END PREAMBLE FONTS %%%

%%% MISC %%%
\usepackage[normalem]{ulem} % underline
\usepackage{floatpag} % Full page figures without page numbers.
%%% END MISC %%%

%%% PAGE, STOCK, AND MARGIN SIZE %%%
% 7.44 x 9.68"   18.90 x 24.58cm
% "Crown Quarto/Crown 4vo" at Lulu and Lightning Source 
\setstocksize{24.58cm}{18.90cm} % { height }{ width }
\settrimmedsize{\stockheight}{\stockwidth}{*}

%\settypeblocksize{ height }{ width }{ ratio }
\settypeblocksize{19.0cm}{*}{*}

%\setlrmarginsandblock{ spine }{ edge }{ ratio }
% make the spine have more space than outer edge
\setlrmarginsandblock{*}{2.5cm}{1.2}

% \setulmargins{ upper }{ lower }{ ratio }
\setulmargins{2.0cm}{*}{*}

% \setheadfoot{ headheight }{ footskip }
\setheadfoot{12pt}{2cm}

\checkandfixthelayout[fixed]
%%% END PAGE, STOCK, AND MARGIN SIZE %%%

\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}
\setsecnumdepth{subsection}

\usepackage[english]{babel}
\usepackage{datetime} % Docs: /usr/share/doc/texlive-doc/latex/datetime/datetime.pdf

%%% Not all features are used in XeTeX
\usepackage[protrusion,babel,final]{microtype}

\usepackage{smartdiagram}
\usesmartdiagramlibrary{additions}

%%% Barcode
\usepackage{pst-barcode}

%%% SNOUG PAGE STYLE %%%
\makepagestyle{jebbastyle}
\pagestyle{jebbastyle}
\makeevenhead{jebbastyle}{}{\hspace{2em}\itshape\small\leftmark}{}
\makeoddhead{jebbastyle}{}{\scshape\small\rightmark}{}
\makeevenfoot{jebbastyle}{}{\hspace{2em}\thepage}{}
\makeoddfoot{jebbastyle}{}{\thepage}{}
%%% END SNOUG PAGE STYLE %%%

%%% SNOUGSKI CHAPTER STYLE %%%
\makechapterstyle{jebbaski}{%
  \renewcommand*{\printchaptername}{}	% Clear out the chapter name (e.g. capítulo)
  \renewcommand*{\printchapternum}{}	% Clear out the chapter number
  \renewcommand*{\chaptitlefont}{\fontspec{GemunuLibre-ExtraBold.otf}\fontsize{20pt}{3em}\selectfont}	% Set chapter title font
  \renewcommand*{\printchaptertitle}[1]{%
     \color{jebba-light-orange}
     \hrule\vskip\onelineskip \centering \chaptitlefont{##1}\par}
  % Set chapter title font
  \renewcommand*{\afterchaptertitle}{\vskip\onelineskip \hrule\vskip
  \afterchapskip} 
}
%%% END SNOUGSKI CHAPTER STYLE %%%

%%% FORMATTING... %%%
\midsloppy
\setlength{\emergencystretch}{3em}
\tolerance=5000
\hyphenpenalty=500
\setlength{\topskip}{1.6\topskip}
\checkandfixthelayout
\raggedbottom
\widowpenalty=10000
\clubpenalty=10000
%%% END FORMATTING... %%%

%%% FOOTNOTES %%%
% no horizontal rule before footnotes:
%\let\oldfootnoterule\footnoterule
%\renewcommand*{\footnoterule}{}
\renewcommand*{\footnotesize}{\scriptsize}
\setlength{\footmarkwidth}{3.5em}
%%% END FOOTNOTES %%%

%%% BIBLIOGRAPHY %%%
\usepackage[style=verbose]{biblatex}
\addbibresource{SNOUG.bib}
%%% END BIBLIOGRAPHY %%%

%%% COLORS %%%
\definecolor{jebba-purple}{cmyk}{0.40,0.70,0.00,0.43}
\definecolor{jebba-dark-blue}{cmyk}{0.83 0.24 0.00 0.22}
\definecolor{jebba-light-blue}{cmyk}{0.42 0.24 0.00 0.08}
\definecolor{jebba-light-orange}{cmyk}{0.00 0.40 0.88 0.03}
\definecolor{jebba-dark-orange}{cmyk}{0.00 0.42 0.83 0.22}
\definecolor{jebba-gold}{cmyk}{0.00 0.28 0.74 0.00}
\definecolor{jebba-greyblack}{cmyk}{0.00 0.22 0.08 0.87}
\definecolor{jebba-white}{cmyk}{0.00 0.00 0.00 0.00}
\definecolor{jebba-black}{cmyk}{1.00 1.00 1.00 1.00}
\definecolor{jebba-green}{cmyk}{0.22 0.00 0.78 0.24}
\definecolor{jebba-darker-blue}{cmyk}{0.84 0.76 0.00 0.70}
%%% END COLORS %%%

%%% CAPTION %%%
% /usr/share/doc/texlive-doc/latex/caption/caption.pdf
\usepackage{caption}
\captionsetup{labelfont={color=blue,bf},
	textfont={color=jebba-dark-orange}}
%%% END CAPTION %%%

%%% FLOATS %%%
\setfloatadjustment{figure}{\centering}
%%% END FLOATS %%%

%%% FRAMES %%%
\usepackage{mdframed}
%%% END FRAMES %%%

%%% HYPERREF %%%
% Should be "last" in preamble.
% http://www.tug.org/applications/hyperref/manual.html
% /usr/share/doc/texlive-doc/latex/hyperref/hyperref.pdf
\usepackage[bookmarksdepth=3, unicode, breaklinks=True, xetex, final=True, hyperfootnotes=True, hyperfigures=True, hyperindex=False, linktoc=all, colorlinks=true, anchorcolor=jebba-darker-blue, citecolor=jebba-darker-blue, linkcolor=jebba-darker-blue, urlcolor=jebba-darker-blue, bookmarks=True, bookmarksopen=True]{hyperref}
\hypersetup{plainpages=False, backref=True, pagebackref=True, pdftitle={SatNOGS Optical Unofficial Guide}, pdfauthor={Jeff Moe}, pdfdisplaydoctitle=True}
%%% END HYPERREF %%%

%%% INDEX %%%
\usepackage{robustindex}
\makeindex
%%% END INDEX %%%

%%% GLOSSARY %%%
\let\printindex\relax % or conflicts with memoir

\usepackage[
	automake, % Automatically create glossary files when compiling
	acronyms, % Allow the use of acronyms as a separate glossary type
	abbreviations, % Allow the use of abbreviations as a separate glossary type
	nonumberlist,record=hybrid,index,toc,indexcrossrefs,translate=babel, % TODO comment what each of these does
]{glossaries-extra}

%%% Multiple Columns (Abbreviations)
\usepackage{glossary-mcols}

\renewcommand{\abbreviationsname}{List of Abbreviations} % Change the default name of the abbreviations

\makeindex

% Specify the in-text glossary abbreviation style for each glossary type
\setabbreviationstyle{long-short}
\setabbreviationstyle[acronym]{long-short} % The first time an acronym is used in text, display it as <long> (<short>), e.g. "Network Time Protocol (NTP)"

% Load all glossary type entries from separate .bib files. Acronyms and abbreviations are specified using @dual<acronym/abbreviation> entries which the code below duplicates and puts into 2 separate glossary types: 1) acronym/abbreviation and 2) main. The former populate the acronyms/abbreviations glossaries and the latter populates the main glossary.
\GlsXtrLoadResources[
	src={Glossary.bib, Acronyms-newformat.bib, Abbreviations-newformat.bib}, % Load all glossary bib files together in one \GlsXtrLoadResources call, NOTE that this is a must or the entries will never be sorted together in the main glossary (i.e. they will appear as blocks, one per .bib file). Search the bib2gls manual for this line for more information: "Note that the sorting is applied to each resource set independently of other resource sets. This means that if you have multiple instances of \glsxtrresourcefile but only one glossary type, the glossary will effectively contain blocks of sorted entries."
	sort={en-US}, % Sorting for the main glossary
	type=main, % All entries fed into the main combined glossary
	field-aliases={longremap=name}, % Rename the "longremap" field in abbreviations/acronyms to "name" so it is used in the main glossary correctly, which expects name to be the main identifier for each entry. NOTE that this "longremap" field is required because the main glossary entries already have a long field which is used in a different way to the long field in abbreviations and acronyms so you can't just remap all long -> name or you break the main glossary entries.
	replicate-fields={name=long}, % Replicate the name fields (which are originally from "longremap") to "long" so they can be used for outputting abbreviation/acronym glossary citations in the format long-short
	missing-sort-fallback={name}, % Sort acronyms and abbreviations in the main glossary by their name field (which is actually the long field)
	dual-sort={letter-nocase}, % Sorting style for dual entries in the acronyms and abbreviations glossaries
	dual-sort-field={short}, % Field to sort dual entries by, confirm this is correct by looking in the .glstex file and find the sort={} value for "dual.*" entries
	%entry-type-aliases={customentryname=entry}, % Use this for mapping a custom entry style to a known entry style, this may be useful if new glossary types are required in the future
]

\GlsXtrEnableIndexFormatOverride

\glsdefpostname{acronym}{ (\glsentryshort{\glscurrententrylabel})} % Add " (<short>) to acronym entries in the main glossary
\glsdefpostname{abbreviation}{ (\glsentryshort{\glscurrententrylabel})} % Add " (<short>) to abbreviation entries in the main glossary

% Define a new glossary style that outputs short and long fields from glossary entries in a description list environment
\newglossarystyle{shortlongglossary}{%
	% Put glossary entries into a description environment
	\renewenvironment{theglossary}{
		% Put entries into 2 columns
		\begin{multicols}{2}
			\begin{description}
	}{
			\end{description}
		\end{multicols}
	}%
	% Define which fields are output for each glossary entry
	\renewcommand*{\glossentry}[2]{%
		\item[\glstarget{##1}{\glsentryshort{##1}}] \glsentrylong{##1} % Each glossary entry is output as a description item in the format: \item[<short>] <long>
	}%
}

% Adds acronym to index like:
% World Coordinate System (WCS), 67, 116
% Adds glossary to index like:
% photon, xii
\renewcommand*{\glsxtrautoindexentry}[1]{%
  \ifglshasshort{#1}%
  {\string\glsentrylong{#1} (\glsentryshort{#1})}%
  {\string\glsentrylong{#1}}%
}

% use long form for the sort value in the index, if provided:
\renewcommand*{\glsxtrautoindexassignsort}[2]{%
  \ifglshaslong{#2}%
  {\glsletentryfield{#1}{#2}{long}}%
  {\glsletentryfield{#1}{#2}{sort}}%
}

% OK, ACRONYM shows description, then short.
% Showshort first better.
% Shows both when desc=long, only one needed...
% OK, but me. XXX borken?
%\glsdefpostname{acronym}{TEST} % This outputs something after the name, e.g. NTPTEST
%\renewcommand*{\glsxtrpostdescacronym}{\glsentrylong{\glscurrententrylabel}} % This outputs something after the description

% crossref for see:
\renewcommand*{\glsxtrpostdescgeneral}{%
\ifglshasfield{see}{\glscurrententrylabel}
{, \glsxtrusesee{\glscurrententrylabel}}%
{}%
}

% Index glossary, abbreviations, acronyms
\glssetcategoryattribute{general}{dualindex}{hyperpage}
\glssetcategoryattribute{abbreviation}{dualindex}{hyperpage}
\glssetcategoryattribute{acronym}{dualindex}{hyperpage}

% First letter upper
%\glssetcategoryattribute{general}{glossname}{firstuc}
%%% END GLOSSARY %%%

%%% DEBUG %%%
%\showoutput
%\typeoutlayout
%\typeoutstandardlayout
%%% END DEBUG %%%

%%% END OF PREAMBLE %%%

%%% BEGIN DOCUMENT %%%
\begin{document}

%%% BEGIN FRONT MATTER %%%
\frontmatter

%%% HALF TITLE PAGE %%%
% We want the half title to be on the recto (right/odd) page.
% Needs blank verso (left/even) page. XXX ?
\include{Half_Title}
%%% END HALF TITLE PAGE

%%% FRONTISPIECE PAGE %%%
% We want the frontispiece to be on the verso (left/even) page. XXX
\include{Frontispiece}
%%% END FRONTISPIECE PAGE

% Set page numbers to lowercase roman numerals, and reset the count to 1 (no *)
\pagenumbering{roman}

%%% TITLE PAGE %%%
% We want the title to be on the recto (right/odd) page. XXX
% If we pad a page, it gives us two with openright
\include{Title}
%%% END TITLE PAGE

%%% COPYRIGHT PAGE %%%
\include{Copyright}
%%% END COPYRIGHT PAGE %%%

%%% DEDICATION PAGE %%%
\include{Dedication}
%%% END DEDICATION PAGE %%%

%%% EPIGRAPH PAGE %%%
\include{Epigraph}
%%% END EPIGRAPH PAGE %%%

%%% TABLE OF CONTENTS %%%
{
	\maxtocdepth{subsection}
	\settocdepth{subsection}
	%\setsecnumdepth{subsection}
	% space between dots
	\renewcommand{\cftchapterdotsep}{15}
	% dot symbol (default is period)
	\renewcommand{\cftdot}{\textperiodcentered}	% centered period
	% Set space between each entry in ToC
	\setlength{\cftbeforechapterskip}{5pt}
	\tableofcontents*}
%%% END TABLE OF CONTENTS %%%

%%% CHAPTER STYLE %%%
\chapterstyle{jebbaski} % defined in preamble
\def\topblockvspace{0.11}
%%% END CHAPTER STYLE %%%

%%% CHAPTER CONFIG %%%
\newcommand{\chapterheader}{SatNOGS Optical Unofficial Guide}
% See \chapterconf below for examples of how this is used.
% value 1 is file to include
% value 2 is title of chapter
% value 3 is sub title of chapter
\newcommand{\chapterconf}[3]{
\chapter{\emph{#2}\protect \\
{#3}}
\thispagestyle{empty}
\markboth{#2}{\chapterheader}
{\include{#1}}
}
% \chapterconf{Name of file to include}{Title of Chapter}
%%% END CHAPTER CONFIG %%%

%%% LIST OF FIGURES %%%
\clearpage
\listoffigures
%%% END LIST OF FIGURES %%%

%%% LIST OF LISTINGS %%%
%\clearpage
%\listoflistings
%%% END LIST OF LISTINGS %%%

%%% LIST OF TABLES %%%
\clearpage
\listoftables
%%% END LIST OF TABLES %%%

%%% FRONTMATTER CHAPTERS %%%

%%% FOREWARD PAGE %%%
%\include{Foreward}
%%% END FOREWARD PAGE %%%

%%% PREFACE PAGE %%%
%\include{Preface}
%%% END PREFACE PAGE %%%

%%% ACKNOWLEDGEMENTS PAGE %%%
%\include{Acknowledgements}
%%% END ACKNOWLEDGEMENTS PAGE %%%

%%% ACRONYM %%%
\printunsrtglossary[type={acronym}, style={shortlongglossary}]
%%% END ACRONYM %%%

% Format:
% \chapterconf{Name of file to include}{Title of Chapter}
\chapterconf{Introduction}{Introduction}{SatNOGS Optical}
%\chapterconf{Prologue}{Prologue}{Prologue}
%%% END FRONTMATTER CHAPTERS %%%

%%% END FRONTMATTER %%%

%%% BEGIN MAINMATTER %%%
\mainmatter*

% Set page numbering to arabic, but don't reset numbering (*)
\pagenumbering*{arabic}

%% MAINMATTER CHAPTERS %%%

% Format:
% \chapterconf{Name of file to include}{Title of Chapter}{Subtitle}
% Comment out a line to not render that chapter
\chapterconf{Satellites}{Satellites}{Artificial Satellites in Space}
\chapterconf{Ground_Stations}{Ground Stations}{Looking up from Earth}
\chapterconf{SatNOGS_Optical}{Toolchain and Pipeline}{Make Old Photons Count}
\chapterconf{Hardware}{Hardware}{Design and Set Up Equipment}
\chapterconf{Software}{Software}{Satellite Applications on Earth}
\chapterconf{Acquire}{Acquire Data}{Twinkle, Twinkle, Little Starlink}
\chapterconf{Solve}{Plate Solvers}{Place from Pixels}
\chapterconf{Detect}{Detect}{Find Satellites}
\chapterconf{Identify}{Identify}{More Than Just Old Rocket Bodies}
\chapterconf{Upload}{Upload \& Share}{Send Data to the SatNOGS Network}
\chapterconf{Support}{Support}{Help!}
\chapterconf{Contact}{Contact}{Email, Chat, Forum}
%% END MAINMATTER CHAPTERS %%%

%%% END MAINMATTER %%%

%%% BEGIN BACKMATTER %%%
\backmatter

%%% APPENDIX %%%
%\clearpage
%\chapterconf{Appendix}{Appendix}{Even More for Free}
%%% END APPENDIX %%%

%%% ABBREVIATIONS %%%
\printunsrtglossary[type={abbreviations}, style={shortlongglossary}]
%%% END ABBREVIATIONS %%%

%%% GLOSSARY %%%
\printunsrtglossary[type={main}, style={indexgroup}]
%%% END GLOSSARY %%%

%%% BIBLIOGRAPHY %%%
\clearpage
\printbibliography
%%% END BIBLIOGRAPHY %%%

%%% INDEX %%%
\clearpage
\printindex
%%% END INDEX %%%

%%% COLOPHON %%%
%%% skip a couple pages
\pagebreak{}
\thispagestyle{empty}
\begingroup
\vfill\null
\endgroup
\pagebreak{}
\thispagestyle{empty}
{\include{Colophon}}
%%% END COLOPHON %%%

%%% END BACKMATTER %%%

\end{document}
%%% END DOCUMENT %%%

