3

I am using R to produce my tables via xtable, but I am not using Sweave to write my document. Instead I am using TexStudio and if possible would like to stick with it.

Therefore I am using \input to import my tables. Ideally, I would like to have all tables be as wide as \textwidth. It seems like there is no way in xtable yet to apply modify \tabcolsep directly within the function. Thus it has to be done in Latex, which means I have to do it manually for every single table.

The idea was to create a macro, which applies \renewcommand{\tabcolsep}{VALUE} at the right position within the table environment.

Here is a MWE table in the format of all my tables produced by R (imagine this as an external .tex file)

\begin{table}[htbp]
\centering
\caption[a]{bbb} 
\label{x}
{\normalsize
\begin{tabular}{lccc}
\toprule 
\multicolumn{1}{l}{Delta} & \multicolumn{1}{c}{Gamma} & \multicolumn{1} {c}{Omega} & \multicolumn{1}{c}{Theta} \\
\midrule 
\multicolumn{1}{l}{$a+x$} & 0.1231 & 0.1122  & 7124 \\ 
\multicolumn{1}{l}{$a+x$} & 0.3333 & 0.1122 & 7343 \\ 
\multicolumn{1}{l}{$a-x$} & 0.3333 & 0.1231 & 9344 \\ 
\multicolumn{1}{l}{$a-x$} & 0.2222 & 0.1231 & 9342 \\ 
\bottomrule 
\end{tabular}
}
\end{table}

So I believe the command \renewcommand{\tabcolsep}{VALUE} would have to be included after \normalsize. Again, the objective is to create a tabular with width \textwidth.

Is this possible? Thanks.

  • 1
    I've taken the liberty of editing your posting to clarify the reason for modifying \tabcolsep: The need to create a tabular that's as wide as \textwidth. – Mico Dec 7 '13 at 22:16
  • \tabcolsep is a length command, so you would need \setlength\tabcolsep{VALUE}. If each file has only one table, you could just start a group, set this length, input the file, then close the group. – Dan Dec 7 '13 at 22:34
7

\tabcolsep is a length not a macro so it should be set using

\setlength\tabcolsep{10pt}

Not \renewcommand.

You can set it anywhere before the table, if you set it in the document preamble it will apply to all tables in the document.

If you want to affect just one table that you are inputting then you can do

{\setlength\tabcolsep{15pt}\input{tablefile}}

However I just noticed

Again, the objective is to create a tabular with width \textwidth.

Setting \tabcolsep is a tricky way to affect the table size using tabular, if you want to affect the total table width you really need an environment that takes a width argument such as tabular*. You would then need to adjust \extracolsep rather than \tabcolsep.


In order to specify a total table width you do not want \begin{tabular} you want

\begin{tabular*}{\textwidth}{@{\extracolumnsep{\fill}....

The xtable manual suggests xtable has width argument to specify \textwidth and tabular.environment argument where you can specify tabular* instead of tabular.

  • I've just finished editing the OP's text to clarify the objective: to create a tabular that's automatically as wide as \textwidth, but without going the route of creating a tabular* environment with @{\extracolsep{\fill}} instructions... – Mico Dec 7 '13 at 22:18
  • @Mico Yes your comment overlapped my answer edit. I'll leave the answer as is though as it asks the question asked, even if it probably isn't what is required for the actual problem. – David Carlisle Dec 7 '13 at 22:21
  • Ah, I may be to blame for the OP's use of \renewcommand in lieu of \setlength! See tex.stackexchange.com/q/148845/5001 and my answer... I don't how long I've been doing it this way -- for some reason it works, at least for the ways I use it. – Mico Dec 7 '13 at 22:25
  • @Mico I added some info on using tabular* instead of tabular from R. – David Carlisle Dec 7 '13 at 22:27
  • 2
    @TomM the xtable manual explictly says you can generate tabular* and specify the width. width is described as: If not NULL , the specified value is included in parenthesis between the tabular environment begin tag and the alignment specification. This allows specification of the table width when using tabular environments such as tabular* and tabularx. Note that table width specification is not supported with the tabular or longtable environments. – David Carlisle Dec 7 '13 at 23:04
0

Thank you Mico for your May 2017 updates in your answer to Optimizing size (font AND width) of tables.

I was also unable to find a way to integrate @{\extracolsep{\fill}} in the xtable function, unless I added it in the intermediary tex file inside the {lrrrrrrr} alignment brackets.

In the hope to save some time to the beginner-to-intermediary xtable user (which I am), I just found that \setlength{\tabcolsep}{VALUE pt} can be specified in the size argument of xtable, as shown in the xtable Gallery vignette by Jonathan Swinton under "5.7 Table Level Commands". I found out after I searched for the tablecolsep argument instead of width theme. "Table Level Commands" is not very explicit about actions like fitting the table to page width. The dedicated section "5.13 Table Width" did not specify how to expand columns automatically so they fit the whole page width. In my case, only the last one was expanded (i.e. lines were expanded until aligned to textwidth, with no option to tell xtable to divide that expansion between columns (I may be mistaken about that).

As the table (and lines) expands automatically with longer tabcolseps when tabular.environment = "tabular*" and width = "\\textwidth" are in use, the user can fiddle with the scaleboxargument to resize the table approximately the width of the text (visually). tabcolsep can also be used in conjunction with those two. Nevertheless, this solution still requires to manually adjust tabcolsep for each table or document.

  • Update: I was still unable to integrate @{\extracolsep{\fill}} in the xtablefunction, and was unsuccessful in defining a new column with a custom \tabcolsep using @{\extracolsep{ }} (systematically ignored and treated like @{} ). However, I was able to bypass the issue by inserting a \hspace declaration, which I believe is put the space between column width and the intercolumn separator. \newcolumntype{R}{r<{\hspace{5.5pt}}} – Pier-Eric Chamberland Dec 11 '17 at 22:54

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.