22

我已经使用 knitr 几天了,非常棒!:)

目前,我正在努力在输出文件(PDF)中对齐两个相邻的图。据我了解,这应该通过out.width='.4\\linewidth'在块选项中设置或类似的东西来实现。

生成的地块非常小,2 很容易彼此相邻,但不知何故,所有地块都放在彼此下方。

我也无法将乳胶表(xtable-output 与results='asis'-option)对齐到文档的左侧。写在旁边就好了。

4

1 回答 1

18

由于您还没有提供,我会为您提供:

\documentclass{article}
\begin{document}

Side by side images:

\begin{figure}[htpb]
<<myChunk, fig.width=3, fig.height=2.5, out.width='.49\\linewidth', fig.show='hold'>>=
par(mar=c(4,4,.1,.1),cex.lab=.95,cex.axis=.9,mgp=c(2,.7,0),tcl=-.3)
plot(cars)
boxplot(cars$dist,xlab='dist')
@
\end{figure}

Ta da!

\end{document}

当我运行knitr时,这导致我看起来大致像这样:

在此处输入图像描述

请注意摆弄par设置以确保一切看起来都不错。你不得不修补。

这个最小的可重现示例来自knitr网站上的非常详细的示例。

编辑

要回答你的第二个问题,即使它更像是一个纯 LaTeX 问题,这里是一个最小的例子:

\documentclass{article}
\usepackage{wrapfig,lipsum}
%------------------------------------------
\begin{document}
This is where the table goes with text wrapping around it. You may 
embed tabular environment inside wraptable environment and customize as you like.
%------------------------------------------
\begin{wraptable}{l}{5.5cm}
\caption{A wrapped table going nicely inside the text.}\label{wrap-tab:1}
<<mychunk,results = asis,echo = FALSE>>=
library(xtable)
print(xtable(head(cars)),floating = FALSE)
@
\end{wraptable} 
%------------------------------------------
\lipsum[2] 
\par
Table~\ref{wrap-tab:1} is a wrapped table.
%------------------------------------------
\end{document}

再一次,我简单地修改了我在这个问题中找到的代码,这个问题在非常有用的 tex.stackexchange.com 站点上。

于 2012-05-13T20:59:23.010 回答