首页 > 解决方案 > 在knitr中插入外部图像

问题描述

问题描述

我在 Overleaf 环境中使用 knitr 并希望包含外部保存的图像。

以前的尝试

编辑:目前所有尝试都给出编译错误和紧急停止:'<*> main.tex ***(作业中止,文件处于不间断模式)'。所以我怀疑代码中存在一般问题,但我无法发现它。

这是我已经尝试过的:

\begin{figure}[htbp]
\includegraphics{image.png}
\end{figure}
<<fig1,fig=TRUE>>=
f='path/to/image.png'
@

\includegraphics{\Sexpr{f}}

下载到图片:RPBU 2011 6 最高峰 1 和 2

最小工作示例(MWE):

请注意,除第一次之外的其他尝试需要先取消注释。

\documentclass{article}

\begin{document}

<<standardsettings,echo=FALSE,results='hide'>>=
%%begin novalidate
library(knitr)
opts_chunk$set(echo=FALSE,fig.width=10,fig.height=5,fig.align='center')
@
%%end novalidate

%First try: Uploading to overleaf and calling outside knitr chunk:
\begin{figure}[htbp]
\includegraphics[width=\linewidth]{RPBU11hp12.png}
\end{figure}

%Second try: Saving image in variable inside knitr chunk and including graphic outside chunk
%<<fig:6highpeaks2018, fig = TRUE>>=
%RPBU11hp1&2 = 'C:/Documents/Uni/5.Jaar 5 (Bachelor)/Kwartiel 3/BEP/Plots/RPBU11hp12.png'
%@

%\begin{figure}[htbp]
%\includegraphics[width=\linewidth]{\Sexpr{RPBU11hp1&2}}
%\end{figure}


%Third try: Directly including the image inside the chunk
% %%begin novalidate
%<<fig:highpeaks2018, fig = TRUE>>=

%knitr::include_graphics(path = %"C:/Documents/Uni/5.Jaar 5 (Bachelor)/Kwartiel 3/BEP/Plots/RPBU11hp12.png")
%@
% %%end novalidate

%Fourth try: Other method of directly including the image inside the chunk
%<<fig:6highpeaks2018, fig = TRUE>>=
%<img src="C:/Documents/Uni/5.Jaar 5 (Bachelor)/Kwartiel 3/BEP/Plots/RPBU11hp12.png" width="50%"/>
%@

\end{document}

标签: imageinsertlatexknitrexternal

解决方案


解决方案很简单:第一个解决方案,将其上传到背页并在默认情况下在 knitr 块之外调用它

\begin{figure}[htbp]
\includegraphics[width=\linewidth]{name_image.png}
\end{figure}

作品。


推荐阅读