%\VignetteIndexEntry{fdt}
\documentclass[10pt,a4paper]{article}
\usepackage[brazil]{babel}
\usepackage[utf8]{inputenc}
\usepackage[sc]{mathpazo}
\usepackage[T1]{fontenc}
\usepackage{geometry}
\geometry{verbose,
  tmargin=2.5cm, 
  bmargin=2.5cm, 
  lmargin=2.5cm, 
  rmargin=2.5cm}
\usepackage[urlcolor=blue,
  citecolor=red,
  colorlinks=true]{hyperref}
\usepackage{url}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{parskip}
\usepackage[round]{natbib}
\usepackage{morefloats}

% The four packages below are required by latex.fdt function
\usepackage{multirow}
\usepackage{colortbl}
\usepackage{array}
\usepackage{threeparttable}

\setlength\parindent{0pt}
\newcommand{\HRule}{\rule{\linewidth}{0.5mm}}

\begin{document}

\begin{titlepage}
  \begin{center}

  % Title
    \HRule \\[0.4cm]
    {\huge \bfseries \LaTeX~table for fdt objects} \\[0.4cm]

    \HRule \\[1.5cm]

    % Author and user
    \begin{minipage}{0.5\textwidth}
      \begin{flushleft} \large
        \emph{Authors:}\\
        José C. \textsc{Faria} \\
        e Ivan B. \textsc{Allaman}
      \end{flushleft}
    \end{minipage}
    \begin{minipage}{0.4\textwidth}
      \begin{flushright} \large
        \emph{Customization in \LaTeX:} \\
        José C. \textsc{Faria}
      \end{flushright}
    \end{minipage}

    \vfill

    % Bottom of the page
    {\large \today}

  \end{center}
\end{titlepage}
\vspace{3cm}

\newpage

To elaborate a simple table.

<<tab, results=tex>>=
library(fdth)
library(xtable)

t1 <- fdt(rnorm(n=1e3,
                mean=10,
                sd=2),
          x.round=3)

t1x <- xtable(t1)
t1x
@

The default is not good. Let's use the print function.

<<results=tex>>=
print(t1x,
      include.rownames=FALSE,
      sanitize.text.function = function(x){x})
@

It's very good!

Replacing mathematical symbols [ and ) by $\dashv$.

<<results=tex>>=
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')
@

Standardizing the class limits to two decimal places.

<<results=tex>>=
clim <- t1$table[1]
clim1 <- sapply(clim,
                as.character)
right <- t1$breaks[4]
pattern='%05.2f'

clim2 <- 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})
@

To objects of the "fdt.multiple" class.

<<results=tex>>=
t5 <- fdt(iris[, c(1:2, 5)],
          by='Species')
attr(t5, "subheadings") <- paste0("Variable = ",
                                  names(t5))
print(xtable(t5),
     table.placement='H')
@

Is not good! It's necessary to use the longtable begin.

<<results=tex>>=
t51 <- xtable(t5)
print(t51,
      table.placement='H',
      include.rownames=FALSE,
      sanitize.text.function = function(x){x},
      tabular.environment='longtable',
      floating=FALSE)
@

To objects of the "fdt\_cat" class.

<<results=tex>>=
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)

@

Title of the table in portuguese.

<<results=tex>>=
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})
@

\end{document}
