%-------------------------------------------------------------------------------
%	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{Variance covariance models for multi-environment data}%
% 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")),
             x$engine)
names(bgItems) <- c("Analysis done on", "statgenGxE version",
                    "Mixed model engine")
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">>=
summaryTab <- x$summary
summaryTab[summaryTab == Inf] <- NA
print(xtable::xtable(x = summaryTab, caption = paste("Fitted models ordered by",
                                                     x$criterion),
                     label = "summary",
                     align = c("l", "r", "r", "r", "r"),
                     digits = 0),
      latex.environments = "flushleft", NA.string = "NA",
      include.rownames = TRUE, include.colnames = TRUE,
      caption.placement = "top")
@
\clearpage
%-------------------------------------------------------------------------------
\section{Covariance matrix}
%-------------------------------------------------------------------------------
<<label=covmat, echo=FALSE, results="asis">>=
## xtable doesn't support \resizebox needed for fitting table to page.
## By using floating = FALSE only a tabular is printed and the full latex
## command is constructed around that.
cat("\\begin{table}[ht]")
cat("\\begin{flushleft}")
cat("\\caption{Covariance matrix for the best model}")
if (sum(nchar(colnames(x$vcov))) + max(nchar(colnames(x$vcov))) + ncol(x$vcov) > 70) {
  cat("\\resizebox{\\textwidth}{!} {")
}
print(xtable::xtable(x = x$vcov,
                     display = c("s",
                                 rep(x = ifelse(max(x$vcov) > 0.1, "f", "e"),
                                     times = ncol(x$vcov)))),
      floating = FALSE,
      include.rownames = TRUE, include.colnames = TRUE)
if (sum(nchar(colnames(x$vcov))) + max(nchar(colnames(x$vcov))) + ncol(x$vcov) > 70) {
  cat("}")
}
cat("\\end{flushleft}")
cat("\\end{table}")
@

%-------------------------------------------------------------------------------
\section{Heatmap}
%-------------------------------------------------------------------------------
Heatmap for the fitted correlation between environments from the best model
<<label=heatmap, echo=FALSE, out.width="15cm", fig.align="left">>=
plot(x)
@

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