%\VignetteIndexEntry{LaTeX table for fdt objects}
%\VignetteEngine{knitr::knitr}
%\VignetteEncoding{UTF-8}

\documentclass[11pt,a4paper]{article}

\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[sc]{mathpazo}
\usepackage{microtype}
\usepackage{geometry}
\usepackage{titlesec}

\geometry{
  a4paper,
  tmargin=2.5cm,
  bmargin=2.5cm,
  lmargin=2.5cm,
  rmargin=2.5cm
}

%% Accent and subtle shades (tables / code remain from xtable / knitr)
\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 table for fdt objects},
  pdfauthor={José C. Faria, Ivan B. Allaman}
}

\usepackage{url}
\usepackage{longtable}
\usepackage{float}
\usepackage{parskip}
\usepackage[round]{natbib}
\usepackage{morefloats}

% Required by \texttt{latex.fdt}
\usepackage{multirow}
\usepackage{colortbl}
\usepackage{array}
\usepackage{threeparttable}

\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\ table for \texttt{fdt} objects}

  \vspace{0.35cm}
  {\large\color{titlemuted}%
    Examples with \texttt{xtable} and 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,
  tidy.opts = list(width.cutoff = 72)
)
@

\vspace{1.25cm}
\tableofcontents
\newpage

\section{Quick start}

This vignette shows a practical workflow to create publication-ready \LaTeX\
tables from \texttt{fdt} and \texttt{fdt\_cat} objects.

\begin{enumerate}
  \item Create a frequency distribution object with \texttt{fdt()} or \texttt{fdt\_cat()}.
  \item Convert it to an \texttt{xtable} object.
  \item Use \texttt{print()} options to control row names, sanitization and table layout.
\end{enumerate}

\subsection{Most used \texttt{xtable::print} arguments}

\begin{center}
\begin{tabular}{p{0.28\linewidth} p{0.63\linewidth}}
\hline
\textbf{Argument} & \textbf{Purpose} \\
\hline
\texttt{include.rownames} & Include or hide row names (usually \texttt{FALSE} for cleaner tables). \\
\texttt{sanitize.text.function} & Control escaping/sanitization (useful for math delimiters and custom symbols). \\
\texttt{table.placement} & Preferred placement for floating tables (for example, \texttt{"H"}). \\
\texttt{tabular.environment} & Choose environment (\texttt{"tabular"}, \texttt{"longtable"}, etc.). \\
\texttt{floating} & Enable/disable floating behavior (commonly \texttt{FALSE} with \texttt{longtable}). \\
\hline
\end{tabular}
\end{center}

\section{First table}

Creating a simple table.

<<tab, message=FALSE, warning=FALSE>>=
library(fdth)
library(xtable)
set.seed(123)

t1 <- fdt(rnorm(n = 1e3,
                mean = 10,
                sd = 2),
          x.round = 3)

t1x <- xtable(t1)
t1x
@

\subsection{Using \texttt{print}}

The default output is not ideal. We use the \texttt{print} function.

<<print-default>>=
print(t1x,
      include.rownames = FALSE,
      sanitize.text.function = function(x) x)
@

This is much better.

\section{Delimiters and class limits}

\subsection{Replacing delimiters with \texttt{\textbackslash dashv}}

Replacing class interval delimiters \texttt{[} and \texttt{)} with
\texttt{\$\textbackslash dashv\$}.

<<dashv>>=
newclass <- gsub("[$\\\\[\\\\)$]",
                 "",
                 t1x[, 1],
                 perl = TRUE)
t3x <- t1x
t3x[, 1] <- newclass

print(t3x,
      include.rownames = FALSE,
      sanitize.text.function = function(x) {
        gsub(",", "$\\\\dashv$", x)
      },
      table.placement = "H")
@

\subsection{Two decimal places for limits}

Standardizing class limits to two decimal places.

<<two-decimals>>=
clim <- t1$table[1]
clim1 <- sapply(clim, as.character)
right <- t1$breaks[4]
pattern <- "%05.2f"

clim2 <- fdth:::make.fdt.format.classes(clim1,
                                        right,
                                        pattern)
clim3 <- sapply(clim2,
                function(x) paste0("$", x, "$"))
t4x <- t1x
t4x[, 1] <- clim3

print(t4x,
      include.rownames = FALSE,
      sanitize.text.function = function(x) x)
@

\section{Objects of class \texttt{fdt.multiple}}

<<fdt-multiple>>=
t5 <- fdt(iris[, c(1:2, 5)],
          by = "Species")
attr(t5, "subheadings") <- paste0("Variable = ", names(t5))
print(xtable(t5),
      table.placement = "H")
@

This output is not ideal for wide layouts; the \texttt{longtable} environment
works better.

<<fdt-multiple-long>>=
t51 <- xtable(t5)
print(t51,
      table.placement = "H",
      include.rownames = FALSE,
      sanitize.text.function = function(x) x,
      tabular.environment = "longtable",
      floating = FALSE)
@

\section{Objects of class \texttt{fdt\_cat}}

<<fdt-cat-one>>=
set.seed(321)
t6 <- fdt_cat(sample(LETTERS[1:3],
                     replace = TRUE,
                     size = 30))

t6x <- xtable(t6)
print(t6x,
      table.placement = "H",
      include.rownames = FALSE)


t61 <- fdt_cat(data.frame(c1 = sample(LETTERS[1:3],
                                      replace = TRUE,
                                      size = 10),
                          c2 = sample(letters[4:5],
                                      replace = TRUE,
                                      size = 10),
                          stringsAsFactors = TRUE))

t61x <- xtable(t61)
print(t61x,
      table.placement = "H",
      include.rownames = FALSE)
@

\section{Objects of class \texttt{fdt\_cat.multiple} with subheadings}

<<fdt-cat-multi>>=
set.seed(654)
t62 <- fdt_cat(data.frame(c1 = sample(LETTERS[1:3],
                                      replace = TRUE,
                                      size = 20),
                          c2 = sample(letters[4:6],
                                      replace = TRUE,
                                      size = 20),
                          stringsAsFactors = TRUE))
attr(t62, "subheadings") <- paste0("Variable = ", names(t62))
print(xtable(t62),
      table.placement = "H")
@

\section{Custom numeric formatting}

<<custom-digits>>=
print(xtable(t1,
             auto = FALSE,
             digits = c(0, 0, 0, 3, 2, 0, 2)),
      include.rownames = FALSE,
      sanitize.text.function = function(x) x,
      table.placement = "H")
@

\section{Table header in Portuguese}

<<portuguese-header>>=
portugueseT <- c("Intervalo de classes",
                 "f",
                 "fr",
                 "fr(%)",
                 "fa",
                 "fa(%)")
t7 <- t1$table
names(t7) <- portugueseT
t71 <- list(table = t7,
            breaks = t1$breaks)
class(t71) <- "fdt"
t7x <- xtable(t71)

print(t7x,
      table.placement = "H",
      include.rownames = FALSE,
      sanitize.text.function = function(x) x)
@

\section{Takeaways}

\begin{itemize}
  \item For a clean default layout, use \texttt{xtable()} followed by \texttt{print(..., include.rownames = FALSE)}.
  \item For custom class labels (delimiters or decimal pattern), combine class-string editing with \texttt{sanitize.text.function}.
  \item For wide or multi-table outputs, prefer \texttt{tabular.environment = "longtable"} and \texttt{floating = FALSE}.
\end{itemize}

\end{document}
