If all you want is two figures side by side, you can put the code in one chunk and set fig.show='hold'
with an appropriate width, e.g.
\documentclass{article}
\begin{document}
<<test, out.width='4cm', out.height='4cm', fig.show='hold'>>=
plot(1:10)
plot(rnorm(100))
@
\end{document}
If you want to center the figures, just add an option fig.align='center'
. If you want to add captions, you can use fig.cap
. See the documentation for all possible options here.
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
Try using the tabularx package, see tabularx documentation for how to use align to further refine your formatting