用编织器将图表彼此相邻对齐

时间:2022-06-07 09:01:52

I've been been using knitr for a couple of days now, it's great! :)

我一直在使用knitr几天,这太棒了! :)

At the moment I'm struggling to align two plots next to each other in the output file (a PDF). From my understanding this should be achieved by setting out.width='.4\\linewidth' or something similar in the chunk-options.

目前,我正努力在输出文件(PDF)中将两个图彼此相邻。根据我的理解,这应该通过在chunk-options中设置out.width ='。4 \\ linewidth'或类似的东西来实现。

The resulting plots are quite small, 2 would quite easily fit next to each other, but somehow, the get all placed beneath each other.

由此产生的图很小,2很容易彼此相邻,但不知何故,所有图都放在彼此之下。

I am also having trouble to align latex-tables (xtable-output with results='asis'-option) to the left of the document. It would be great to write next to it.

我也很难将latex-tables(xtable-output和results ='asis'-option)对齐到文档的左侧。在旁边写它会很棒。

1 个解决方案

#1


18  

Since you haven't provided one, I will do so for you:

既然你没有提供,我会为你做的:

\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}

which results in something that looks roughly like this for me when I run knitr:

当我运行knitr时,这会产生一些看起来像这样的东西:

用编织器将图表彼此相邻对齐

Note the fiddling with the par settings to make sure everything looks nice. You will have to tinker.

请注意摆弄标准杆设置,以确保一切都很好看。你将不得不修补。

This minimal reproducible example was derived from the very detailed examples on the knitr website.

这个可重复性最小的例子来自knitr网站上非常详细的例子。

Edit

编辑

To answer your second question, even though it's more of a pure LaTeX question, here is a minimal example:

要回答你的第二个问题,即使它更像是一个纯粹的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}

Once again, I simply adapted code I found in this question at the amazingly helpful tex.stackexchange.com site.

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

#1


18  

Since you haven't provided one, I will do so for you:

既然你没有提供,我会为你做的:

\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}

which results in something that looks roughly like this for me when I run knitr:

当我运行knitr时,这会产生一些看起来像这样的东西:

用编织器将图表彼此相邻对齐

Note the fiddling with the par settings to make sure everything looks nice. You will have to tinker.

请注意摆弄标准杆设置,以确保一切都很好看。你将不得不修补。

This minimal reproducible example was derived from the very detailed examples on the knitr website.

这个可重复性最小的例子来自knitr网站上非常详细的例子。

Edit

编辑

To answer your second question, even though it's more of a pure LaTeX question, here is a minimal example:

要回答你的第二个问题,即使它更像是一个纯粹的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}

Once again, I simply adapted code I found in this question at the amazingly helpful tex.stackexchange.com site.

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