You should be OK if only [p]
is specified as the positioning argument, because the contents of page of floats is always centered vertically.
If it's not possible to set up Stata so that it outputs \begin{figure}[p]
for this particular document, then adding the following code to the preamble should force it anyway:
\let\latextable\table
\renewcommand\table[1][]{\latextable[p]}
Note: if you can convince Stata to say
\def\sym#1{\ensuremath{^{#1}}
your document will be more robust. This is one of the few cases when \ensuremath
is really useful.
I think I found an acceptable solution.
Assume that the statistical table is schematically as follows:
Short label Very long description Three letter label
1 1 5 9
2 2 6 10
3 3 7 11
4 4 8 12
LaTeX code to be generated to make every column large as the longest word of its label is:
\documentclass{article}
\usepackage{varwidth, calc}
\begin{document}
%% LaTeX vars
\def\colDef{}
\newlength{\temp}
%% Column 1
\setlength{\temp}{\widthof{\mbox{\begin{varwidth}{\textwidth}
Short\\label
\end{varwidth}}}}
\edef\colDef{\colDef p{\the\temp}}
%% Column 2
\setlength{\temp}{\widthof{\mbox{\begin{varwidth}{\textwidth}
Very\\long\\description
\end{varwidth}}}}
\edef\colDef{\colDef p{\the\temp}}
%% Column 3
\setlength{\temp}{\widthof{\mbox{\begin{varwidth}{\textwidth}
Three\\letter\\label
\end{varwidth}}}}
\edef\colDef{\colDef p{\the\temp}}
\edef\colDef{{\colDef}}
%% Table Macro
\newcommand{\maketab}[2]{%
\begin{tabular}{#1}
#2
\end{tabular}
}
%% Table Body
\newcommand{\tableBody}{
Short label & Very long description & Three letter label \\
\hline
1 & 5 & 9 \\
2 & 6 & 10 \\
3 & 7 & 11 \\
4 & 8 & 12 \\
\hline
}
%% and...
\expandafter\maketab\colDef{\tableBody}
\end{document}
Latexing one gets the intended results for labels:
Short Very long Three
label description letter
label
-----------------------------
1 5 9
2 6 10
3 7 11
4 8 12
-----------------------------
The R/knitr side
As there are both the knitr
and xtable
tags on tex.stackexchange, I assume the R code to generate the above LaTeX might be of interest to someone.
The Rnw document to "knit" is:
\documentclass{article}
\usepackage{varwidth, calc}
\begin{document}
<<label=ididit, results='asis', echo=FALSE>>=
library(xtable)
## Generate sample 4x3 table, with long labels
## -------------------------------------------
tab=data.frame(matrix(1:12, ncol=3))
names(tab) = c("Short label", "Very long description", "Three letter label")
## Generate LaTeX code: every \ needs to be doubled (escaped)
## -----------------------------------------------------------
## Print LaTeX vars
cat("
\\def\\colDef{}
\\newlength{\\temp}
")
## Dynamic text
text1="\\setlength{\\temp}{\\widthof{\\mbox{\\begin{varwidth}{\\textwidth}"
# Labels here separated by //
text2="\\end{varwidth}}}}
\\edef\\colDef{\\colDef p{\\the\\temp}}
"
## Separate labels with \\\\
lab= strsplit(names(tab), " ")
lab= sapply(lab, function(x) paste(x, collapse='\\\\'))
## Replace dynamic text with labels and print it
text=paste(text1, lab, text2, sep="\n", collapse="\n")
cat(text)
cat("\n\\edef\\colDef{{\\colDef}}")
## Setup main table macro
cat("
\\newcommand{\\maketab}[2]{%
\\begin{tabular}{#1}
#2
\\end{tabular}
}
")
## Table body obtained via xtable module
cat("\\newcommand{\\tableBody}{\n")
print.xtable(xtable(tab), only.contents=T, include.rownames=F)
cat("}\n")
### eventually...
cat("\\expandafter\\maketab\\colDef{\\tableBody}")
@
\end{document}
If this document is named table.rnw
, knitting it, that is, executing in R:
knitr("table.rnw")
will generate a table.tex
like the LaTeX code shown in the first listing above (plus some some knitr macro embellishment for graphics) to be compiled in LaTeX in order to get the desired table output.
Comments on solution
Note that the proposed solution requires one step in R + one step in LaTeX, as the generated LaTeX contains both the code to measure lengths and to use them in the tabular environment. xtable
is used only to produce the formatted list of the table inner cells, so one can easily customise the tabular environment with something fancier and/or anyway set more formatting parameters for the table.
Avoiding extra round trip from R to LaTeX means speed, anyway do suggest any path to further improve it.
Best Answer
You don't need to merge
in1.tex
andin2.tex
horizontally intoin3.tex
(say) before setting it in atabular
. Here's how you can do it without that merger:Some things to note here:
Either end of the
tabular
structure has only half the regular\tabcolsep
; that is,.5\tabcolsep
. As such, a horizontal concatenation is equivalent to a regular\tabcolsep
.\input{..}%
ends with a%
. See What is the use of percent signs (%
) at the end of lines?End the first
tabular
using\end{tabular}%
. Motivation similar to above.The two separate
tabular
s are[t]
op aligned. However, in the case where the numbers of rows inin1.tex
andin2.tex
are equal, there is no real need for this forced alignment.