图*twocolumn knit/Sweave 文档中的环境

发布于 2024-12-28 16:46:54 字数 499 浏览 1 评论 0原文

听起来这应该是一个常见问题,但我没有找到明显的技巧。 考虑下面的 knit Rnw 文件,

\documentclass[twocolumn, 12pt]{article}
\usepackage{graphicx}
\begin{document}
%\SweaveOpts{dev=pdf, fig.align=center}
\begin{figure*}
<<aaa, fig.width=8, fig.height=5, fig.show=hold>>=
plot(1,1)
@
\end{figure*}
\end{document}

我希望这个宽图跨越两列,使用 {figure*} LaTeX 环境。有一个钩子吗?

编辑:将块包装​​在 figure* 中给出以下输出。

在此处输入图像描述

Sounds like it should be a common problem, but I didn't find an obvious trick.
Consider the knitr Rnw file below,

\documentclass[twocolumn, 12pt]{article}
\usepackage{graphicx}
\begin{document}
%\SweaveOpts{dev=pdf, fig.align=center}
\begin{figure*}
<<aaa, fig.width=8, fig.height=5, fig.show=hold>>=
plot(1,1)
@
\end{figure*}
\end{document}

I would like this wide figure to span two columns, using a {figure*} LaTeX environment. Is there a hook for that?

EDIT: wrapping the chunk in figure* gives the following output.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弥枳 2025-01-04 16:46:54

两个事实:

  1. knitr 让您可以轻松访问所有内容,因此 LaTeX 技巧通常是不必要的;
  2. 有一个 chunk 钩子,您可以用它包装块结果;

一个简单的解决方案是:

knit_hooks$set(chunk = function(x, options) {
                       sprintf('\\begin{figure*}\n%s\n\\end{figure*}', x)
})

我将剩下的工作留给您来处理 options 中的更多细节(例如,当 options$fig.keep == 'none' ,您不应将输出包装在 figure* 中)。您可能想了解默认的chunk钩子 for LaTeX 在 knitr 中定义,以便更好地了解 chunk 钩子的工作原理。

然而,在这种情况下,我倾向于自己在文档中编写LaTeX代码,而不是自动创建它。获得 figure* 后,您可能会开始考虑 \caption{}\label{} (不难,但我仍然想在 LaTeX 中看到它们)。

Two facts:

  1. knitr makes everything accessible for you, so LaTeX tricks are often unnecessary;
  2. there is a chunk hook with which you can wrap your chunk results;

A simple-minded solutions is:

knit_hooks$set(chunk = function(x, options) {
                       sprintf('\\begin{figure*}\n%s\n\\end{figure*}', x)
})

I leave the rest of work to you to take care of more details in options (e.g. when options$fig.keep == 'none', you should not wrap the output in figure*). You may want to see how the default chunk hook for LaTeX is defined in knitr to know better how the chunk hook works.

However, in this case, I tend to write the LaTeX code by myself in the document instead of automatically creating it. After you have got figure*, you may start to think about \caption{} and \label{} (not hard, but I still want to see them in LaTeX).

内心激荡 2025-01-04 16:46:54

不确定如何knitr,但对于Sweave(和基本乳胶)来说,实际上有一个技巧:让R代码生成一个pdf文件,然后使用标准的\includegraphics将其拉入。

所以用这个:

\documentclass[twocolumn, 12pt]{article}
\usepackage{graphicx}
\begin{document}
%\SweaveOpts{dev=pdf}

<<aaa,fig=FALSE,print=FALSE,echo=FALSE>>=
pdf("mychart.pdf", width=6, height=3)
set.seed(42)
plot(cumsum(rnorm(100)), type='l', main="yet another random walk")
invisible(dev.off())
@

\begin{figure*}
  \includegraphics{mychart.pdf}
\end{figure*}

\end{document}

我得到了下面的文档(然后我将其从 pdf 转换为 png):

在此处输入图像描述

Not sure about how knitr but for Sweave (and basic latex) there is in fact a trick: have the R code produce a pdf file, and then use standard \includegraphics to pull it in.

So with this:

\documentclass[twocolumn, 12pt]{article}
\usepackage{graphicx}
\begin{document}
%\SweaveOpts{dev=pdf}

<<aaa,fig=FALSE,print=FALSE,echo=FALSE>>=
pdf("mychart.pdf", width=6, height=3)
set.seed(42)
plot(cumsum(rnorm(100)), type='l', main="yet another random walk")
invisible(dev.off())
@

\begin{figure*}
  \includegraphics{mychart.pdf}
\end{figure*}

\end{document}

I got the document below (which I then converted from pdf to png):

enter image description here

江城子 2025-01-04 16:46:54

在准备 IEEE 两栏会议论文中应跨越两栏的图表时,我也遇到了类似的问题。

设置块钩子导致我的设置出现一些奇怪的错误。
即使是这个简单的钩子: knit_hooks$set(chunk = function(x, options) x)

但在研究 knitr::opts_chunk$get() 之后,我意识到这很简单设置 fig.env="figure*" 以优雅的方式解决了这个问题。

这是我的块在 Rnw 文件中的样子:

<<fig1, fig.width=18, fig.height=6, fig.env="figure*">>=
@

I also had a similar problem while preparing a figure that should span two columns in a IEEE two-column conference paper.

Setting the chunk hook caused some strange error in my setup.
Even this simple hook: knit_hooks$set(chunk = function(x, options) x)

But after looking into knitr::opts_chunk$get(), I realized that simply setting fig.env="figure*" solves the problem in an elegant way.

Here is how my chunk looks like in an Rnw file:

<<fig1, fig.width=18, fig.height=6, fig.env="figure*">>=
@
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文