% Package `RJournal' to use with LaTeX2e
% Copyright (C) 2010 by the R Foundation
% Copyright (C) 2013 by the R Journal
%
% Originally written by Kurt Hornik and Friedrich Leisch with subsequent 
% edits by the editorial board
%
% CAUTION:
% Do not modify this style file. Any changes to this file will be reset when your
% article is submitted.
% If you must modify the style or add LaTeX packages to the article, these 
% should be specified in RJwrapper.tex

\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{RJournal}[2022/06/27 v0.14 RJournal package]

\RequirePackage{tikz}

% Overall page layout, fonts etc -----------------------------------------------

% Issues of of \emph{The R Journal} are created from the standard \LaTeX{}
% document class \pkg{report}. 

\RequirePackage{geometry}
\geometry{a4paper, 
   textwidth=14cm, top=1cm, bottom=1cm,
   includehead,includefoot,centering,
   footskip=1.5cm}
\raggedbottom

\RequirePackage{fancyhdr}
\fancyhead{}
\fancyheadoffset{2cm} 
\fancyhead[L]{\textsc{\RJ@sectionhead}}
\fancyhead[R]{\thepage}
\fancyfoot{}
\fancyfoot[L]{The R Journal Vol. \RJ@volume/\RJ@number, \RJ@month~\RJ@year}
\fancyfoot[R]{ISSN 2073-4859}
\pagestyle{fancy}

% We use the following fonts (all with T1 encoding):
% 
%   rm   & palatino
%   tt   & inconsolata
%   sf   & helvetica
%   math & palatino

\RequirePackage{microtype}

\RequirePackage[scaled=0.92]{helvet}
\RequirePackage{palatino,mathpazo}
\RequirePackage[scaled=1.02]{inconsolata}
\RequirePackage[T1]{fontenc}

\RequirePackage[hyphens]{url}
\RequirePackage[pagebackref]{hyperref}
\renewcommand{\backref}[1]{[p#1]}

% Dark blue colour for all links
\RequirePackage{color}
\definecolor{link}{rgb}{0.45,0.51,0.67}
\hypersetup{
  colorlinks,%
  citecolor=link,%
  filecolor=link,%
  linkcolor=link,%
  urlcolor=link
}

% Give the text a little room to breath
\setlength{\parskip}{3pt}
\RequirePackage{setspace}
\setstretch{1.05}

% Issue and article metadata ---------------------------------------------------

% Basic front matter information about the issue: volume, number, and
% date.

\newcommand{\volume}[1]{\def\RJ@volume{#1}}
\newcommand{\volnumber}[1]{\def\RJ@number{#1}}
\renewcommand{\month}[1]{\def\RJ@month{#1}}
\renewcommand{\year}[1]{\def\RJ@year{#1}}


% Individual articles correspond to
% chapters, and are contained in |article| environments.  This makes it
% easy to have figures counted within articles and hence hyperlinked
% correctly.

% An article has an author, a title, and optionally a subtitle.  We use
% the obvious commands for specifying these. Articles will be put in certain
% journal sections, named by \sectionhead.

\newcommand  {\sectionhead}  [1]{\def\RJ@sectionhead{#1}}
\renewcommand{\author}       [1]{\def\RJ@author{#1}}
\renewcommand{\title}        [1]{\def\RJ@title{#1}}
\newcommand  {\subtitle}     [1]{\def\RJ@subtitle{#1}}

% Control appearance of titles: make slightly smaller than usual, and 
% suppress section numbering. See http://tex.stackexchange.com/questions/69749
% for why we don't use \setcounter{secnumdepth}{-1} 

\usepackage[medium]{titlesec}
\usepackage{titletoc}
\titleformat{\section}   {\normalfont\large\bfseries}{\arabic{section}}{1em}{}
\titleformat{\subsection}{\normalfont\normalsize\bfseries}{\arabic{section}.\arabic{subsection}}{0.5em}{}
\titlecontents{chapter}   [0em]{}{}{}{\titlerule*[1em]{.}\contentspage}

% Article layout ---------------------------------------------------------------

% Environment |article| clears the article header information at its beginning. 
% We use |\FloatBarrier| from the placeins package to keep floats within
% the article.
\RequirePackage{placeins}
\newenvironment{article}{\author{}\title{}\subtitle{}\FloatBarrier}{\FloatBarrier}

% Refereed articles should have an abstract, so we redefine |\abstract| to
% give the desired style

\renewcommand{\abstract}[1]{%
\setstretch{1}%
\noindent%
\small%
\textbf{Abstract} #1 
}

% The real work is done by a redefined version of |\maketitle|.  Note
% that even though we do not want chapters (articles) numbered, we
% need to increment the chapter counter, so that figures get correct
% labelling.

\renewcommand{\maketitle}{%
\noindent
  \chapter{\RJ@title}\refstepcounter{chapter}
  \ifx\empty\RJ@subtitle
  \else
    \noindent\textbf{\RJ@subtitle}
    \par\nobreak\addvspace{\baselineskip}
  \fi
  \ifx\empty\RJ@author
  \else
    \noindent\textit{\RJ@author}
    \par\nobreak\addvspace{\baselineskip}
  \fi
  \@afterindentfalse\@nobreaktrue\@afterheading
}

% Now for some ugly redefinitions.  We do not want articles to start a
% new page. (Actually, we do, but this is handled via explicit
% \newpage
%
% The name@of@eq is a hack to get hyperlinks to equations to work
% within each article, even though there may be multiple eq.(1)
%    \begin{macrocode}
\renewcommand\chapter{\secdef\RJ@chapter\@schapter}
\providecommand{\nohyphens}{%
  \hyphenpenalty=10000\exhyphenpenalty=10000\relax}
\newcommand{\RJ@chapter}{%
  \edef\name@of@eq{equation.\@arabic{\c@chapter}}%
  \renewcommand{\@seccntformat}[1]{}%
  \@startsection{chapter}{0}{0mm}{%
    -2\baselineskip \@plus -\baselineskip \@minus -.2ex}{\p@}{%
    \phantomsection\normalfont\huge\bfseries\raggedright}}

% Book reviews should appear as sections in the text and in the pdf bookmarks,
% however we wish them to appear as chapters in the TOC. Thus we define an
% alternative to |\maketitle| for reviews.
\newcommand{\review}[1]{
  \pdfbookmark[1]{#1}{#1}
  \section*{#1}
  \addtocontents{toc}{\protect\contentsline{chapter}{#1}{\thepage}{#1.1}}
}

% We want bibliographies as starred sections within articles.
% 
\RequirePackage[sectionbib,round]{natbib}
\bibliographystyle{abbrvnat}
\renewcommand{\bibsection}{\section*{References}}

% Equations, figures and tables are counted within articles, but we do
% not show the article number. For equations it becomes a bit messy to avoid
% having hyperref getting it wrong. 

% \numberwithin{equation}{chapter}
\renewcommand{\theequation}{\@arabic\c@equation}
\renewcommand{\thefigure}{\@arabic\c@figure}
\renewcommand{\thetable}{\@arabic\c@table}

% Issue layout -----------------------------------------------------------------

% Need to provide our own version of |\tableofcontents|. We use the
% tikz package to get the rounded rectangle. Notice that |\section*|
% is really the same as |\chapter*|.
\renewcommand{\contentsname}{Contents}
\renewcommand\tableofcontents{%
   \vspace{1cm}
   \section*{\contentsname}
   { \@starttoc{toc} }
}

\renewcommand{\titlepage}{%
  \thispagestyle{empty}
  \hypersetup{
    pdftitle={The R Journal Volume \RJ@volume/\RJ@number, \RJ@month \RJ@year},%
    pdfauthor={R Foundation for Statistical Computing},%
  }
  \noindent
  \begin{center}
    \fontsize{50pt}{50pt}\selectfont
    The \raisebox{-8pt}{\includegraphics[height=77pt]{Rlogo-5}}\hspace{10pt}
    Journal
  
  \end{center}
  {\large \hfill Volume \RJ@volume/\RJ@number, \RJ@month{} \RJ@year \quad}

  \rule{\textwidth}{1pt}
  \begin{center}
    {\Large A peer-reviewed, open-access publication of the \\
    R Foundation for Statistical Computing}
  \end{center}

  % And finally, put in the TOC box. Note the way |tocdepth| is adjusted
  % before and after producing the TOC: thus, we can ensure that only
  % articles show up in the printed TOC, but that in the PDF version,
  % bookmarks are created for sections and subsections as well (provided
  % that the non-starred forms are used).
  \setcounter{tocdepth}{0}
  \tableofcontents
  \setcounter{tocdepth}{2}
  \clearpage
}

% Text formatting --------------------------------------------------------------

\newcommand{\R}{R}
\newcommand{\address}[1]{\addvspace{\baselineskip}\noindent\emph{#1}}
\newcommand{\email}[1]{\href{mailto:#1}{\normalfont\texttt{#1}}}

% Simple font selection is not good enough.  For example, |\texttt{--}|
% gives `\texttt{--}', i.e., an endash in typewriter font.  Hence, we
% need to turn off ligatures, which currently only happens for commands
% |\code| and |\samp| and the ones derived from them.  Hyphenation is
% another issue; it should really be turned off inside |\samp|.  And
% most importantly, \LaTeX{} special characters are a nightmare.  E.g.,
% one needs |\~{}| to produce a tilde in a file name marked by |\file|.
% Perhaps a few years ago, most users would have agreed that this may be
% unfortunate but should not be changed to ensure consistency.  But with
% the advent of the WWW and the need for getting `|~|' and `|#|' into
% URLs, commands which only treat the escape and grouping characters
% specially have gained acceptance

\DeclareRobustCommand\code{\bgroup\@noligs\@codex}
\def\@codex#1{\texorpdfstring%
{{\normalfont\ttfamily\hyphenchar\font=-1 #1}}%
{#1}\egroup}
\newcommand{\kbd}[1]{{\normalfont\texttt{#1}}}
\newcommand{\key}[1]{{\normalfont\texttt{\uppercase{#1}}}}
\DeclareRobustCommand\samp{`\bgroup\@noligs\@sampx}
\def\@sampx#1{{\normalfont\texttt{#1}}\egroup'}
\newcommand{\var}[1]{{\normalfont\textsl{#1}}}
\let\env=\code
\newcommand{\file}[1]{{`\normalfont\textsf{#1}'}}
\let\command=\code
\let\option=\samp
\newcommand{\dfn}[1]{{\normalfont\textsl{#1}}}
% \acronym is effectively disabled since not used consistently
\newcommand{\acronym}[1]{#1}
\newcommand{\strong}[1]{\texorpdfstring%
{{\normalfont\fontseries{b}\selectfont #1}}%
{#1}}
\let\pkg=\strong
\newcommand{\CRANpkg}[1]{\href{https://CRAN.R-project.org/package=#1}{\pkg{#1}}}%
\let\cpkg=\CRANpkg
\newcommand{\ctv}[1]{\href{https://CRAN.R-project.org/view=#1}{\emph{#1}}}
\newcommand{\BIOpkg}[1]{\href{https://www.bioconductor.org/packages/release/bioc/html/#1.html}{\pkg{#1}}}

% Example environments ---------------------------------------------------------
\RequirePackage{fancyvrb}
\RequirePackage{alltt}

\DefineVerbatimEnvironment{example}{Verbatim}{}
\renewenvironment{example*}{\begin{alltt}}{\end{alltt}}

% Support for output from Sweave, and generic session style code
% These used to have fontshape=sl for Sinput/Scode/Sin, but pslatex
% won't use a condensed font in that case.

% Update (2015-05-28 by DS): remove fontsize=\small to match example environment

\DefineVerbatimEnvironment{Sinput}{Verbatim}{}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{}
\DefineVerbatimEnvironment{Scode}{Verbatim}{}
\DefineVerbatimEnvironment{Sin}{Verbatim}{}
\DefineVerbatimEnvironment{Sout}{Verbatim}{}
\newenvironment{Schunk}{}{}

% Mathematics ------------------------------------------------------------------

% The implementation of |\operatorname| is similar to the mechanism
% \LaTeXe{} uses for functions like sin and cos, and simpler than the
% one of \AmSLaTeX{}.  We use |\providecommand| for the definition in
% order to keep the one of the \pkg{amstex} if this package has
% already been loaded.
%    \begin{macrocode}
\providecommand{\operatorname}[1]{%
  \mathop{\operator@font#1}\nolimits}
\RequirePackage{amsfonts}

\renewcommand{\P}{%
  \mathop{\operator@font I\hspace{-1.5pt}P\hspace{.13pt}}}
\newcommand{\E}{%
  \mathop{\operator@font I\hspace{-1.5pt}E\hspace{.13pt}}}
\newcommand{\VAR}{\operatorname{var}}
\newcommand{\COV}{\operatorname{cov}}
\newcommand{\COR}{\operatorname{cor}}

% Figures ----------------------------------------------------------------------

\RequirePackage[font=small,labelfont=bf]{caption}

% Wide environments for figures and tables -------------------------------------
\RequirePackage{environ}

% An easy way to make a figure span the full width of the page
\NewEnviron{widefigure}[1][]{
\begin{figure}[#1]
\advance\leftskip-2cm
\begin{minipage}{\dimexpr\textwidth+4cm\relax}%
  \captionsetup{margin=2cm}
  \BODY
\end{minipage}%
\end{figure}
}

\NewEnviron{widetable}[1][]{
\begin{table}[#1]
\advance\leftskip-2cm
\begin{minipage}{\dimexpr\textwidth+4cm\relax}%
  \captionsetup{margin=2cm}
  \BODY
\end{minipage}%
\end{table}
}
