%-------------------------------------------------------------------------------
%	Packages and other Document configurations
%-------------------------------------------------------------------------------
\documentclass[a4paper,11pt]{article}
% Package declaration
%-------------------------------------------------------------------------------
% Specify input encoding
\usepackage[utf8]{inputenc}
% For A4 paper set all margins to 3cm
\usepackage[paper=a4paper,left=1.5cm,top=2cm,right=1.5cm,bottom=2cm]{geometry}%
% Set linespace, usage \doublespacing \singlespacing \onehalfspacing
\usepackage{setspace}%
% Set palatino font with small caps as default
\usepackage[sc]{mathpazo}%
% Rotation tools, including rotated full-page floats.
\usepackage{rotating}%
% Create subfigures
\usepackage{subfigure}%
% Extensive support for hypertext in LaTeX
\usepackage{hyperref}%
% For adding bookmarks to the document
\usepackage{bookmark}%
% For adding time to the document
\usepackage{datetime}
% For alignment of captions
\usepackage{caption}
% For multiple columns.
\usepackage{multicol}

% Start Article header
%-------------------------------------------------------------------------------
% Title
\title{Stability analysis report for \Sexpr{escapeLatex(x$trait)}}%
% Authors
\author{\vspace{-5ex}}
%-------------------------------------------------------------------------------
% Dates
\date{\vspace{-5ex}}
%-------------------------------------------------------------------------------
% End article header

% For left aligning captions
\captionsetup{justification=raggedright,singlelinecheck=false}

% Start Document
%-------------------------------------------------------------------------------
\begin{document}

% Article title and date.
\maketitle
% Start single line spacing
\singlespacing

%-------------------------------------------------------------------------------
\section{General information}
%-------------------------------------------------------------------------------
<<label=general, echo=FALSE, results="asis">>=
bgItems <- c(format(attr(x, "timestamp"), "%y-%m-%d %H:%M:%S"),
             as.character(packageVersion("statgenGxE")))
names(bgItems) <- c("Analysis done on",
                    "statgenGxE version")
print(xtable::xtable(x = data.frame(bgItems), label = "general",
                     align = c("l", "l")),
      latex.environments = "flushleft", hline.after = NULL,
      include.rownames = TRUE, include.colnames = FALSE)
@

%-------------------------------------------------------------------------------
\section{Summary}
%-------------------------------------------------------------------------------
<<label=summary, echo=FALSE, results="asis">>=
nPlots <- sum(c(!is.null(x$superiority), !is.null(x$static), !is.null(x$wricke)))
if (!is.null(x$superiority)) {
  top10pct <- x$superiority[1:(ceiling(nrow(x$superiority) / 10)), ]
  print(xtable::xtable(x = top10pct,
                       caption = "Cultivar-superiority measure (Top 10\\% genotypes)",
                       label = "cultSup",
                       align = c("l", "l", "r", "r"),
                       digits = c(0, 0, 2, 2)),
        latex.environments = "flushleft", hline.after = NULL,
        include.rownames = FALSE, include.colnames = TRUE,
        caption.placement = "top")
}
if (!is.null(x$static)) {
  top10pct <- x$static[1:(ceiling(nrow(x$static) / 10)), ]
  print(xtable::xtable(x = top10pct,
                       caption = "Static stability (Top 10\\% genotypes)",
                       label = "static",
                       align = c("l", "l", "r", "r"),
                       digits = c(0, 0, 2, 2)),
        latex.environments = "flushleft", hline.after = NULL,
        include.rownames = FALSE, include.colnames = TRUE,
        caption.placement = "top")
}
if (!is.null(x$wricke)) {
  top10pct <- x$wricke[1:(ceiling(nrow(x$wricke) / 10)), ]
  print(xtable::xtable(x = top10pct,
                       caption = "Wricke's ecovalence (Top 10\\% genotypes)",
                       label = "wricke",
                       align = c("l", "l", "r", "r"),
                       digits = c(0, 0, 2, 2)),
        latex.environments = "flushleft", hline.after = NULL,
        include.rownames = FALSE, include.colnames = TRUE,
        caption.placement = "top")
}
@

\clearpage
%-------------------------------------------------------------------------------
\section{Diagnostic plots}
%-------------------------------------------------------------------------------
<<label=diagplots, echo=FALSE, fig.align="left", fig.height=4*((nPlots-1)%/%2 + 1)>>=
plot(x)
@

%-------------------------------------------------------------------------------
% End Document
\end{document}
