% José Cláudio Faria
%\VignetteIndexEntry{LaTeX tables for bpca objects}
%\VignetteEngine{knitr::knitr_notangle}
%\VignetteEncoding{UTF-8}

\documentclass[11pt,a4paper]{article}

\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[sc]{mathpazo}
\usepackage{microtype}
\usepackage{xcolor}
\usepackage{geometry}
\usepackage{titlesec}

\geometry{
  a4paper,
  tmargin=2.5cm,
  bmargin=2.5cm,
  lmargin=2.5cm,
  rmargin=2.5cm
}

\definecolor{accent}{RGB}{26, 82, 118}
\definecolor{rulegray}{RGB}{180, 180, 180}
\definecolor{titlemuted}{RGB}{90, 90, 90}

\usepackage[
  colorlinks=true,
  linkcolor=accent,
  citecolor=accent,
  urlcolor=accent
]{hyperref}
\hypersetup{
  pdftitle={LaTeX tables for bpca objects},
  pdfauthor={José C. Faria, Ivan B. Allaman}
}

\usepackage{url}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{parskip}
\usepackage[round]{natbib}

% Required by latex.bpca
\usepackage{multirow}
\usepackage{colortbl}
\usepackage{array}
\usepackage{Sweave}

\setlength{\parindent}{0pt}
\setlength{\parskip}{0.65em}

\titleformat{\section}
  {\normalfont\Large\bfseries\color{accent}}
  {\thesection}{0.75em}{}
\titlespacing*{\section}{0pt}{2.25ex plus .5ex minus .2ex}{1.25ex plus .2ex}

\titleformat{\subsection}
  {\normalfont\large\bfseries\color{titlemuted}}
  {\thesubsection}{0.65em}{}

\newcommand{\HRule}{%
  \leavevmode\leaders\hrule height 0.6pt\hfill\kern0pt\relax}
\newcommand{\titleaccentline}{%
  {\color{accent}\rule{\linewidth}{1.4pt}}}

\begin{document}

\begin{titlepage}
  \centering
  \vspace*{1.2cm}

  {\color{rulegray}\HRule}
  \vspace{0.55cm}

  {\Huge\bfseries\color{accent}%
    \LaTeX\ tables for \texttt{bpca} objects}

  \vspace{0.35cm}
  {\large\color{titlemuted}%
    Practical examples with \texttt{xtable} and clean formatting}

  \vspace{0.55cm}
  {\color{rulegray}\HRule}

  \vspace{2.4cm}

  \begin{minipage}[t]{0.48\textwidth}
    \raggedright
    \textbf{\color{titlemuted}Authors}\\[0.35em]
    José C.\ \textsc{Faria}\\
    Ivan B.\ \textsc{Allaman}
  \end{minipage}%
  \hfill
  \begin{minipage}[t]{0.48\textwidth}
    \raggedleft
    \textbf{\color{titlemuted}\LaTeX\ customization}\\[0.35em]
    José C.\ \textsc{Faria}
  \end{minipage}

  \vfill

  \titleaccentline
  \vspace{0.6cm}
  {\large\today}
\end{titlepage}

<<setup, include=FALSE>>=
knitr::opts_chunk$set(
  results = "asis",
  echo = TRUE,
  comment = NA
)
@

\vspace{1.25cm}
\tableofcontents
\newpage

\section{Quick start}

This vignette presents a simple and reproducible workflow to generate
publication-ready \LaTeX\ tables from \texttt{bpca} objects:

\begin{enumerate}
  \item Fit a biplot model with \texttt{bpca()}.
  \item Convert it to an \texttt{xtable} object.
  \item Use \texttt{print()} options to refine labels and presentation.
\end{enumerate}

\subsection{Most used \texttt{print.xtable} arguments}

\begin{center}
\begin{tabular}{p{0.36\linewidth} p{0.55\linewidth}}
\hline
\textbf{Argument} & \textbf{Purpose} \\
\hline
\texttt{caption} & Add a descriptive caption to reference in text. \\
\texttt{label} & Create a \LaTeX\ label for cross-referencing (\texttt{\textbackslash ref\{\}}). \\
\texttt{sanitize.colnames.function} & Customize the rendering of column labels (for example, bold text). \\
\texttt{sanitize.rownames.function} & Customize row labels (for example, italic text). \\
\hline
\end{tabular}
\end{center}

\vspace{1.25cm}

<<libraries, message=FALSE, warning=FALSE>>=
library(bpca)
library(xtable)
@

\section{First table}

Start with a minimal example using the \texttt{iris} data.

<<simple-example>>=
bp <- bpca(iris[-5])
tb_simple <- xtable(bp)

## Build with xtable and print with print() so the bpca S3 method is used.
print(tb_simple)
@

\section{Cross-referencing tables}

Captions and labels make documents easier to navigate.
The following examples are referenced as Table \ref{tbl_iris} and
Table \ref{tbl_gabriel}.

<<cross-ref-iris>>=
tb_iris <- xtable(
  bpca(iris[-5]),
  caption = "Biplot of iris data (package: datasets).",
  label = "tbl_iris"
)
print(tb_iris)
@

<<cross-ref-gabriel>>=
tb_gabriel <- xtable(
  bpca(gabriel1971),
  caption = "Biplot of gabriel1971 data (package: bpca).",
  label = "tbl_gabriel"
)
print(tb_gabriel)
@

\section{Formatting for readability}

\subsection{Bold column names}

Table \ref{tbl_rock} highlights column names in bold.

<<bold-columns>>=
tb_rock <- xtable(
  bpca(rock),
  caption = "Biplot of rock data (package: datasets).",
  label = "tbl_rock"
)

bold <- function(x) paste0("\\textbf{", x, "}")

print(tb_rock, sanitize.colnames.function = bold)
@

\subsection{Italic row names}

Table \ref{tbl_usarrests} highlights row names in italic.

<<italic-rows>>=
tb_usarrests <- xtable(
  bpca(USArrests),
  caption = "Biplot of USArrests data (package: datasets).",
  label = "tbl_usarrests"
)

italic <- function(x) paste0("\\textit{", x, "}")

print(tb_usarrests, sanitize.rownames.function = italic)
@

\section{Localization of labels}

You can adapt labels to Portuguese and keep the same table structure.
Table \ref{tbl_rock_pt} shows this approach.

<<portuguese-labels>>=
tb_rock_pt <- xtable(
  bpca(rock),
  caption = "Biplot of rock data (package: datasets), Portuguese labels.",
  label = "tbl_rock_pt"
)

rownames(tb_rock_pt) <- gsub(
  "Eigenvalues",
  "Autovalores",
  rownames(tb_rock_pt)
)
rownames(tb_rock_pt) <- gsub(
  "Eigenvectors",
  "Autovetores",
  rownames(tb_rock_pt)
)
rownames(tb_rock_pt) <- gsub(
  "Variance retained",
  "Variancia retida",
  rownames(tb_rock_pt)
)
rownames(tb_rock_pt) <- gsub(
  "Variance accumulated",
  "Variancia acumulada",
  rownames(tb_rock_pt)
)

colnames(tb_rock_pt) <- c("CP1", "CP2")

print(tb_rock_pt)
@

\section{Custom print options in one call}

It is often convenient to customize row and column formatting directly in
a single \texttt{print()} call (Table \ref{tbl_directly}).

<<custom-print>>=
print(
  xtable(
    bpca(rock),
    caption = "Direct customization using print() on an xtable.bpca object.",
    label = "tbl_directly"
  ),
  sanitize.colnames.function = bold,
  sanitize.rownames.function = italic
)
@

\section{Takeaways}

\begin{itemize}
  \item Use \texttt{xtable(bpca(...))} + \texttt{print()} as the default workflow.
  \item Add \texttt{caption} and \texttt{label} early when writing reports.
  \item Use sanitization functions to improve readability (bold/italic, localized labels).
  \item Keep examples compact and reproducible to simplify maintenance.
\end{itemize}

\end{document}
