\documentclass[pdflatex, a4paper, 11pt]{article}
<<MAINknitr, message=FALSE, comment=NA, warning=FALSE, echo=FALSE, results='hide', cache=FALSE>>=
### Load and set up knitr
library(knitr)
#library(Cairo)
opts_knit$set(self.contained=FALSE)
opts_chunk$set(message=FALSE,comment=NA,warning=FALSE,echo=FALSE,results='hide', 
               cache=TRUE,fig.align='center',size='normalsize',dev=c('pdf','png'),tidy=FALSE,concordance=FALSE)
@


%%%%% Modified
\usepackage[a4paper,pdftex,hmargin=2.5cm,vmargin=3.5cm]{geometry}			% A4paper margins
\setlength{\oddsidemargin}{0mm}				    % Adjusting margins to center the colorbox, ...
\setlength{\evensidemargin}{0mm}					% ... you might want to change these
\usepackage[english]{babel}
\usepackage{fix-cm}
\usepackage{booktabs, longtable, adjustbox, array, enumerate, mathtools, fancyhdr, graphicx, fancybox, amsmath, lscape, caption}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\usepackage[T1]{fontenc}
\usepackage[affil-it]{authblk}
\usepackage{needspace}
\usepackage{listings}
\usepackage{inconsolata}
\usepackage[para,flushleft]{threeparttablex}
\usepackage{float}
\usepackage{etoolbox}

\newcommand{\HRule}[1]{\hfill \rule{0.2\linewidth}{#1}} 	% Horizontal rule
\definecolor{grey}{rgb}{0.9,0.9,0.9}
\definecolor{aliceblue}{rgb}{0.94, 0.97, 1.0}
\definecolor{ballblue}{rgb}{0.13, 0.67, 0.8}
\makeatletter							% Title
\def\printtitle{%						
    {\centering \@title\par}}
\makeatother									
\makeatletter							% Author
\def\printauthor{%					
    {\centering \large \@author}}				
\makeatother

\title{
  \fontsize{20}{20}\selectfont
	\vspace*{0.7cm}
	\textbf{Report of \textit{ncappc}\textsuperscript{*} package}
}

\author{
  \textsuperscript{*} Acharya, C., Hooker, A. C., Turkyilmaz, G. Y., Jonsson, S., Karlsson, M. O.; A diagnostic tool for population models using non-compartmental analysis: The ncappc package for R, \textit{Computer Methods and Programs in Biomedicine}, 2016, Vol. 127, 83-93
}


%%% Hyperref and cref settings
\usepackage[bookmarksnumbered]{hyperref} % hyperlinks
\hypersetup{colorlinks,breaklinks,
            urlcolor=blue,%[rgb]{0.29020,0.52157,0.56078},
            linkcolor=blue,%[rgb]{0.29020,0.52157,0.56078},
            citecolor=blue,%[rgb]{00.29020,0.52157,0.56078},
            linktoc=page}

\captionsetup{labelformat=empty}

%%% Header and footer
\makeatletter
%\patchcmd{\@fancyhead}{\rlap}{\color{ballblue}\rlap}{}{}
\fancyhead[L]{\textbf{Report}}
\fancyhead[R]{\textit{ncappc}}
\fancyfoot[C]{\thepage}
\patchcmd{\headrule}{\hrule}{\color{grey}\hrule}{}{}
%\patchcmd{\@fancyfoot}{\rlap}{\color{ballblue}\rlap}{}{}
\patchcmd{\footrule}{\hrule}{\color{grey}\hrule}{}{}
\makeatother
\pagestyle{fancy}

% \pagestyle{fancy}
% \fancyhf{}
% \fancyhead[L]{\textbf{Report}}
% \fancyhead[R]{\textit{ncappc}}
% \fancyfoot[C]{\thepage}

% Eqn, Fig and Tab
\numberwithin{equation}{section}
\numberwithin{figure}{section}
\numberwithin{table}{section}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{4pt}}m{#1}}


\begin{document}
\thispagestyle{empty}				% Remove page numbering on this page

\colorbox{grey}{
\parbox[t]{1.0\linewidth}{
\printtitle 
\vspace*{0.7cm}
}}

\vfill
\printauthor								% Print the author data as defined above
\HRule{1pt}
\clearpage

\newpage{}
\normalsize
\tableofcontents
\thispagestyle{empty}
\clearpage
\pagenumbering{arabic}

<<echo=FALSE>>=
options(width=80)
listing <- function(x, options){
  paste("\\begin{lstlisting}[basicstyle=\\ttfamily,breaklines=true]\n",
  x, "\\end{lstlisting}\n", sep = "")
}
knit_hooks$set(source=listing, output=listing)
@

\normalsize
\section{Summary of the data set and the results}

<<main, echo=FALSE, cache=FALSE>>=
arglist <- fnOut$arglist
case    <- fnOut$case
txt     <- fnOut$TXT
pddf    <- fnOut$pddf
prnTab  <- fnOut$prnTab
conc    <- fnOut$conc
histobs <- fnOut$histobs

if(fnOut$spread=="ppi"){
  sprtxt <- "95\\% parametric prediction interval of the NCA metrics"
}else if(fnOut$spread=="npi"){
  sprtxt <- "95\\% nonparametric prediction interval of the NCA metrics"
}
@

<<txt, echo=FALSE, tidy=TRUE, results='markup', cache=FALSE>>=
cat(txt)
@
\paragraph{}

\begin{center}
\textbf{Summary table}
<< Summary table, echo=FALSE, results='asis', cache=FALSE>>==
suppressPackageStartupMessages(require(xtable))
if(case==1) colform <- c(rep("p{1cm}",1),rep("p{2.5cm}",ncol(pddf)))
if(case==2) colform <- c(rep("p{1cm}",2),rep("p{2.5cm}",ncol(pddf)-1))
if(case==3) colform <- c(rep("p{1cm}",3),rep("p{2.5cm}",ncol(pddf)-2))
if(case==4) colform <- c(rep("p{1cm}",4),rep("p{2.5cm}",ncol(pddf)-3))
mtable <- xtable(pddf, align=colform, rotate.colnames = T, digits = 3)
print(mtable, file=paste(usrdir,"/sum.tex",sep=""), include.rownames=FALSE,
      tabular.environment="longtable", caption.placement = "top", size="\\footnotesize", floating=FALSE)
@
\input{sum}
\end{center}
\paragraph{}

\section{Command-line arguments passed to \textit{ncappc} function}
<<cmd, echo=FALSE, results='markup', cache=FALSE>>=
print(arglist)
@

\newpage
\section{Description of the tabular output}

% Figure and Table description in the absence of simulated data
\begin{sloppypar}

\subsection{Table 1 (ncaOutput.tsv)}
The \textbf{\textit{ncappc}} functionality produces this table to report the estimated values of the NCA metrics described in the documentation for each individual along with other stratifiers (eg. population group ID, dose ID, etc.) if specified in the input command. The extension "tsv" stands for "tab separated variable", \textit{i.e.}, the columns in this table are separated by tabs. "NaN" or "NA" is produced for the NCA metrics which are irrelevant for the specified data type. Below is an excerpt of selected columns of top 50 rows.


<<FinalTable, echo=FALSE, results='asis', fig.env="table", cache=FALSE>>==
source("makeLatexRepresentativeDataTable.R")
makeLatexRepresentativeDataTable(dataDir="",dataFile=prnTab,iColWidth=7,iRowNums=50,strLabel="tab:repDataPK",strShortCaption="Representative portion of the ncaOutput.tsv file", strLongCaption="Representative portion of the ncaOutput.tsv file")
@

\newpage
\normalsize
\subsection{Table 2 (Obs\_Stat.tsv)}
A set of statistical parameters calculated for the entire population or the stratified population for the median values of the following NCA metrics estimated from the simulated data: Tmax, Cmax, AUClast, AUClower\_upper, AUCINF\_obs, AUC\_pExtrap\_obs, AUCINF\_pred, AUC\_pExtrap\_pred, AUMClast, AUMCINF\_obs, AUMC\_pExtrap\_obs, AUMCINF\_pred, AUMC\_pExtrap\_pred, HL\_Lambda\_z, Rsq, Rsq\_adjusted, No\_points\_Lambda\_z obtained from the observed data. Brief description of the calculated statistical parameters: \textbf{Ntot} = Total number of data points, \textbf{Nunique} = number of unique data points, \textbf{Min} = minimum value, \textbf{Max} = maximum value, \textbf{Mean} = mean/average value, \textbf{SD} = standard deviation, \textbf{SE} = standard error, \textbf{CVp} = coefficient of variation \%, \textbf{a95CIu} = upper limit of 95\% arithmetic confidence interval, \textbf{a95CIl} = lower limit of 95\% arithmetic confidence interval, \textbf{gMean} = geometric mean, \textbf{gCVp} = geometric coefficient of variation \%.

\newpage
\section{Description of the graphical output}

\subsection{Figure 1. [Individual level] Concentration \textit{vs.} time profile}
<<concplot, echo=FALSE, fig.pos="H", fig.height=4, fig.width=7, out.width='0.7\\textwidth', fig.align="center", cache=FALSE>>=
if (length(conc)>0){
  for (i in 1:length(conc)){suppressMessages(suppressWarnings(grid.draw(conc[[i]]))); Sys.sleep(0.5); grid.newpage()}
}else{
  print("No concentration vs time plot is available.")
}
@

Concentration vs time profile for each individual stratified by dose or population group, if any, as obtained from the observed data. The left panels represent the concentration data in linear scale, while the right panels represent the concentration data in semi-logarithmic scale. Each of the lines represents individual data.

\newpage
\subsection{Figure 2. [Population level] Histogram of the selected NCA metrics estimated from the observed data}
<<histobsplot, echo=FALSE, fig.pos="H", fig.height=8, fig.width=7, out.width='0.8\\textwidth', fig.align='center', cache=FALSE>>=
if (length(histobs)>0){
  suppressPackageStartupMessages(require(gridExtra))
  for (i in 1:length(histobs)){suppressMessages(suppressWarnings(grid.draw(histobs[[i]]))); Sys.sleep(0.5); grid.newpage()}
}else{
  print("No histogram is available as the number of individuals is less than 5 in\nall population strata!")
}
@

Histogram of four selected NCA metrics (AUClast, AUCINF\_obs, Cmax, Tmax) estimated from the observed data. The solid blue vertical and dotted lines represent the population median and the "spread" of the data. The "spread" is defined by \Sexpr{sprtxt} obtained from the observed data.

\end{sloppypar}
\end{document}

