如何让fig.width和out.width与knitr保持一致?
How to make fig.width and out.width consistent with knitr?
我最近发现了 knitr,我主要用它来制作带有 dev=tikz
的绘图,以便轻松获得 Latex 排版。但是我不明白如何以一致性设置块选项 fig.width
和 out.width
。
我的意思是,我知道第一个是 R 选项,而第二个是 \includegraphics
附带的乳胶选项,但似乎 fig.width
与 [= 相比太大了15=] 然后线条很细,因为乳胶缩小了图片。另一方面,如果它太小,那么乳胶会拉伸它,一切都太大了。
基本上我也想有一个设置,我只选择 out.width
然后线条和文本的粗细与文档的文本大小一致。
我附上了一个 MWE 来说明我的问题。另外我在第一个例子中设置了fig.height
否则图片比页面大,我也不太明白。
热烈欢迎任何帮助!
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
options(formatR.arrow=TRUE,width=50)
opts_chunk$set(fig.path='figure/graphics-',
cache.path='cache/graphics-',
fig.align='center',
dev='tikz')
@
\begin{document}
\begin{figure}[!ht]
<<fig_1,fig.width=2, fig.height = 3,out.width = '\textwidth',echo=FALSE>>=
x = seq(1,3,l=100)
plot(x,cos(x), type ="l", xlab = "x", ylab = "y")
@
\end{figure}
\begin{figure}[!ht]
<<fig_2,fig.width=10, out.width = '\textwidth',echo=FALSE>>=
x = seq(1,3,l=100)
plot(x,cos(x), type ="l", xlab = "x", ylab = "y")
@
\end{figure}
\end{document}
你可能不应该 \begin{figure}
因为 knitr 会为你包含它们。此外,我发现当 LaTeX 根本不调整图形大小时效果最好,即它们已经是正确的大小。这是一个示例,我将文本宽度设置为 6 英寸,将图形宽度设置为相同的值。
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
options(formatR.arrow=TRUE,width=50)
opts_chunk$set(fig.path='figure/graphics-',
cache.path='cache/graphics-',
fig.align='center',
dev='tikz')
@
\usepackage[text={6in,9in},centering]{geometry}
\begin{document}
<<fig_1, fig.width=6, echo=FALSE>>=
x = seq(1,3,l=100)
plot(x,cos(x), type ="l", xlab = "x", ylab = "y")
@
<<fig_2, fig.width=6, out.width = '\textwidth',echo=FALSE>>=
x = seq(1,3,l=100)
plot(x,cos(x), type ="l", xlab = "x", ylab = "y")
@
\end{document}
创建tikzpicture
后基本上不应该调整大小,因为它会失去与文本样式的一致性。事实上,当设置块选项 cache=FALSE
时,out.width
没有效果,因为没有创建 pdf 输出。因此,必须为每个块的 fig.width
和 fig.height
指定以英寸为单位的精确尺寸。
在对 Whosebug 和 Knitr 网站进行一些研究后,我发现了一个技巧来获得几乎相同的用户体验,即我不想关心真实的大小,但只考虑相对于 \textwidth
和其他 Latex 变量:
- 在setup chunk中,定义R变量并明确命名(如
paperwidth
,textwidth
对应Latex单位英寸,例如一张A4纸是(w,h) = ( 8.3, 11.7).
- 在每个带有情节的块中,您可以使用
fig.width = 0.5*paperwidth
例如
这是一个 MWE:
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{tikz}
<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
options(formatR.arrow=TRUE,width=50)
opts_chunk$set(fig.path='figure/graphics-',
cache.path='cache/graphics-',
fig.align='center',
dev='tikz',
external=TRUE,
echo=FALSE
)
a4width<- 8.3
a4height<- 11.7
@
\begin{document}
\begin{figure}[!ht]
\centering
<<fig_1, fig.width = 0.4*a4width, fig.height = 0.2*a4height>>=
x = seq(1,3,l=100)
plot(x,cos(x), type ="l", xlab = "x", ylab = "y")
@
<<fig_2, fig.width = 0.2*a4width, fig.height = 0.5*a4height>>=
x = seq(1,3,l=100)
plot(x,cos(x), type ="l", xlab = "x", ylab = "y")
@
\caption{Test figure}
\end{figure}
\end{document}
我最近发现了 knitr,我主要用它来制作带有 dev=tikz
的绘图,以便轻松获得 Latex 排版。但是我不明白如何以一致性设置块选项 fig.width
和 out.width
。
我的意思是,我知道第一个是 R 选项,而第二个是 \includegraphics
附带的乳胶选项,但似乎 fig.width
与 [= 相比太大了15=] 然后线条很细,因为乳胶缩小了图片。另一方面,如果它太小,那么乳胶会拉伸它,一切都太大了。
基本上我也想有一个设置,我只选择 out.width
然后线条和文本的粗细与文档的文本大小一致。
我附上了一个 MWE 来说明我的问题。另外我在第一个例子中设置了fig.height
否则图片比页面大,我也不太明白。
热烈欢迎任何帮助!
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
options(formatR.arrow=TRUE,width=50)
opts_chunk$set(fig.path='figure/graphics-',
cache.path='cache/graphics-',
fig.align='center',
dev='tikz')
@
\begin{document}
\begin{figure}[!ht]
<<fig_1,fig.width=2, fig.height = 3,out.width = '\textwidth',echo=FALSE>>=
x = seq(1,3,l=100)
plot(x,cos(x), type ="l", xlab = "x", ylab = "y")
@
\end{figure}
\begin{figure}[!ht]
<<fig_2,fig.width=10, out.width = '\textwidth',echo=FALSE>>=
x = seq(1,3,l=100)
plot(x,cos(x), type ="l", xlab = "x", ylab = "y")
@
\end{figure}
\end{document}
你可能不应该 \begin{figure}
因为 knitr 会为你包含它们。此外,我发现当 LaTeX 根本不调整图形大小时效果最好,即它们已经是正确的大小。这是一个示例,我将文本宽度设置为 6 英寸,将图形宽度设置为相同的值。
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
options(formatR.arrow=TRUE,width=50)
opts_chunk$set(fig.path='figure/graphics-',
cache.path='cache/graphics-',
fig.align='center',
dev='tikz')
@
\usepackage[text={6in,9in},centering]{geometry}
\begin{document}
<<fig_1, fig.width=6, echo=FALSE>>=
x = seq(1,3,l=100)
plot(x,cos(x), type ="l", xlab = "x", ylab = "y")
@
<<fig_2, fig.width=6, out.width = '\textwidth',echo=FALSE>>=
x = seq(1,3,l=100)
plot(x,cos(x), type ="l", xlab = "x", ylab = "y")
@
\end{document}
创建tikzpicture
后基本上不应该调整大小,因为它会失去与文本样式的一致性。事实上,当设置块选项 cache=FALSE
时,out.width
没有效果,因为没有创建 pdf 输出。因此,必须为每个块的 fig.width
和 fig.height
指定以英寸为单位的精确尺寸。
在对 Whosebug 和 Knitr 网站进行一些研究后,我发现了一个技巧来获得几乎相同的用户体验,即我不想关心真实的大小,但只考虑相对于 \textwidth
和其他 Latex 变量:
- 在setup chunk中,定义R变量并明确命名(如
paperwidth
,textwidth
对应Latex单位英寸,例如一张A4纸是(w,h) = ( 8.3, 11.7). - 在每个带有情节的块中,您可以使用
fig.width = 0.5*paperwidth
例如
这是一个 MWE:
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{tikz}
<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
options(formatR.arrow=TRUE,width=50)
opts_chunk$set(fig.path='figure/graphics-',
cache.path='cache/graphics-',
fig.align='center',
dev='tikz',
external=TRUE,
echo=FALSE
)
a4width<- 8.3
a4height<- 11.7
@
\begin{document}
\begin{figure}[!ht]
\centering
<<fig_1, fig.width = 0.4*a4width, fig.height = 0.2*a4height>>=
x = seq(1,3,l=100)
plot(x,cos(x), type ="l", xlab = "x", ylab = "y")
@
<<fig_2, fig.width = 0.2*a4width, fig.height = 0.5*a4height>>=
x = seq(1,3,l=100)
plot(x,cos(x), type ="l", xlab = "x", ylab = "y")
@
\caption{Test figure}
\end{figure}
\end{document}