1

Is there a way to make the TeX xtable source code results well formatted and easier to read. Example:

library(xtable)
## Load example dataset
data(tli)
print(tli.table)

gives

\begin{table}[ht]
\centering
\begin{tabular}{rrlllr}
  \hline
 & grade & sex & disadvg & ethnicty & tlimth \\ 
  \hline
1 & 6 & M & YES & HISPANIC & 43 \\ 
  2 & 7.0 & M & NO & BLACK & 88.0 \\ 
  3 & 5.00 & F & YES & HISPANIC & 34.00 \\ 
  4 & 3.000 & M & YES & HISPANIC & 65.000 \\ 
  5 & 8.0000 & M & YES & WHITE & 75.0000 \\ 
  6 & 5 & M & NO & BLACK & 74 \\ 
  7 & 8.0 & F & YES & HISPANIC & 72.0 \\ 
  8 & 4.00 & M & YES & BLACK & 79.00 \\ 
  9 & 6.000 & M & NO & WHITE & 88.000 \\ 
  10 & 7.0000 & M & YES & HISPANIC & 87.0000 \\ 
  11 & 3 & M & NO & WHITE & 79 \\ 
  12 & 6.0 & F & NO & WHITE & 84.0 \\ 
  13 & 8.00 & M & NO & WHITE & 90.00 \\ 
  14 & 5.000 & M & NO & WHITE & 73.000 \\ 
  15 & 8.0000 & F & NO & WHITE & 72.0000 \\ 
  16 & 6 & F & NO & BLACK & 82 \\ 
  17 & 4.0 & M & NO & WHITE & 69.0 \\ 
  18 & 3.00 & F & YES & HISPANIC & 17.00 \\ 
  19 & 3.000 & M & NO & HISPANIC & 37.000 \\ 
  20 & 5.0000 & M & NO & WHITE & 70.0000 \\ 
   \hline
\end{tabular}
\end{table}

If I use something like http://www.tablesgenerator.com/ it gives the following (much equally spaced columns and hence more readable in TeX)

\begin{table}[]
\centering
    \begin{tabular}{rrlllr}
    \hline
           & grade  & sex & disadvg & ethnicty & tlimth  \\ \hline
        1  & 6      & M   & YES     & HISPANIC & 43      \\
        2  & 7.0    & M   & NO      & BLACK    & 88.0    \\
        3  & 5.00   & F   & YES     & HISPANIC & 34.00   \\
        4  & 3.000  & M   & YES     & HISPANIC & 65.000  \\
        5  & 8.0000 & M   & YES     & WHITE    & 75.0000 \\
        6  & 5      & M   & NO      & BLACK    & 74      \\
        7  & 8.0    & F   & YES     & HISPANIC & 72.0    \\
        8  & 4.00   & M   & YES     & BLACK    & 79.00   \\
        9  & 6.000  & M   & NO      & WHITE    & 88.000  \\
        10 & 7.0000 & M   & YES     & HISPANIC & 87.0000 \\
        11 & 3      & M   & NO      & WHITE    & 79      \\
        12 & 6.0    & F   & NO      & WHITE    & 84.0    \\
        13 & 8.00   & M   & NO      & WHITE    & 90.00   \\
        14 & 5.000  & M   & NO      & WHITE    & 73.000  \\
        15 & 8.0000 & F   & NO      & WHITE    & 72.0000 \\
        16 & 6      & F   & NO      & BLACK    & 82      \\
        17 & 4.0    & M   & NO      & WHITE    & 69.0    \\
        18 & 3.00   & F   & YES     & HISPANIC & 17.00   \\
        19 & 3.000  & M   & NO      & HISPANIC & 37.000  \\
        20 & 5.0000 & M   & NO      & WHITE    & 70.0000 \\ \hline
    \end{tabular}
\end{table}

thanks

2
  • 1
    I gather you are referring to legibility of source code and not that of typeset output? Welcome to the site. Mar 31 '16 at 11:48
  • @Courvoisier - Your text editor should provide such a feature. If you use Emacs, try M-x align-current RET. Mar 31 '16 at 19:36
1

As long as the input material in the tabular environment isn't too illegible, I would put in next to no effort at all to pretty-print the input material. In contrast, I would exert considerable effort to make the typeset output more visually appealing to your readers, say along the following lines:

enter image description here

\documentclass{memoir}
\usepackage{siunitx,array,booktabs}
\newcolumntype{T}[1]{S[table-format=#1]}
\newcolumntype{L}{>{\scshape}l}

\begin{document}
\begin{table}
\centering
  \begin{tabular}{@{} T{2.0} T{1.4} LLL T{2.4} @{}}
  \toprule
  & {\scshape grade} & sex & disadvg & ethnicty & {\scshape tlimth} \\
  \midrule
  1 & 6 & m & yes & hispanic & 43 \\
  2 & 7.0 & m & no & black & 88.0 \\
  3 & 5.00 & f & yes & hispanic & 34.00 \\
  4 & 3.000 & m & yes & hispanic & 65.000 \\
  5 & 8.0000 & m & yes & white & 75.0000 \\
  6 & 5 & m & no & black & 74 \\
  7 & 8.0 & f & yes & hispanic & 72.0 \\
  8 & 4.00 & m & yes & black & 79.00 \\
  9 & 6.000 & m & no & white & 88.000 \\
  10 & 7.0000 & m & yes & hispanic & 87.0000 \\
  11 & 3 & m & no & white & 79 \\
  12 & 6.0 & f & no & white & 84.0 \\
  13 & 8.00 & m & no & white & 90.00 \\
  14 & 5.000 & m & no & white & 73.000 \\
  15 & 8.0000 & f & no & white & 72.0000 \\
  16 & 6 & f & no & black & 82 \\
  17 & 4.0 & m & no & white & 69.0 \\
  18 & 3.00 & f & yes & hispanic & 17.00 \\
  19 & 3.000 & m & no & hispanic & 37.000 \\
  20 & 5.0000 & m & no & white & 70.0000 \\
  \bottomrule
  \end{tabular}
\end{table}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.