在knitr中投下阴影

时间:2015-08-24 20:41:52

标签: knitr beamer xelatex

我正在混合knitr,beamer和xelatex来制作一些幻灯片。我希望看到我的数字下降阴影,以使我的演示更加花哨。

我发现this great way会从常规数字中删除阴影,但我无法在knitr chunk中使其工作。

你们知道我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:2)

使用chunk option fig.show = "hide",您可以阻止knitr将生成的数字包含在您的文档中。这允许您使用您喜欢的任何标记手动包含图形。

请注意,默认情况下,数字存储在目录figure中,文件名遵循模式chunkname-figurenumber,例如来自mychunk的第二个数字的文件名为mychunk-1.pdf。 (请参阅上面链接的块选项列表中的fig.path。)

这给我们留下了最后一个陷阱:你的情节的背景颜色。

  

取决于您使用的图形包和设备,背景   图表可能是透明的(例如基础图形的tikz)或白色   (例如 ggplot2 )默认情况下。如果你绝对想要的话   基本图形的背景颜色始终为白色,您可以进行设置   white选项的钩子,如下所示:

<<white-background,eval=FALSE>>=    
knit_hooks$set(white = function(before, options, envir) {if (before) par(bg = 'white')}) 

(引用来自old versionknitr graphics manual;我在当前版本中找不到它。)

如果您需要块挂钩或设置par(bg = "white")就足够了,取决于您生成的图形数量。如果您不知道如何使用chunk hooks,请查看this posting的结尾。

不使用chunk钩子并调整TEX代码from the answer that was linked in the question,以下代码使用knitr生成带阴影的图形:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{shadows,calc}

% code adapted from https://tex.stackexchange.com/a/11483/3954
% code then adapted from https://tex.stackexchange.com/a/81847/37118
%
% some parameters for customization
\def\shadowshift{3pt,-3pt}
\def\shadowradius{6pt}

\colorlet{innercolor}{black!60}
\colorlet{outercolor}{gray!05}

% this draws a shadow under a rectangle node
\newcommand\drawshadow[1]{
    \begin{pgfonlayer}{shadow}
        \shade[outercolor,inner color=innercolor,outer color=outercolor] ($(#1.south west)+(\shadowshift)+(\shadowradius/2,\shadowradius/2)$) circle (\shadowradius);
        \shade[outercolor,inner color=innercolor,outer color=outercolor] ($(#1.north west)+(\shadowshift)+(\shadowradius/2,-\shadowradius/2)$) circle (\shadowradius);
        \shade[outercolor,inner color=innercolor,outer color=outercolor] ($(#1.south east)+(\shadowshift)+(-\shadowradius/2,\shadowradius/2)$) circle (\shadowradius);
        \shade[outercolor,inner color=innercolor,outer color=outercolor] ($(#1.north east)+(\shadowshift)+(-\shadowradius/2,-\shadowradius/2)$) circle (\shadowradius);
        \shade[top color=innercolor,bottom color=outercolor] ($(#1.south west)+(\shadowshift)+(\shadowradius/2,-\shadowradius/2)$) rectangle ($(#1.south east)+(\shadowshift)+(-\shadowradius/2,\shadowradius/2)$);
        \shade[left color=innercolor,right color=outercolor] ($(#1.south east)+(\shadowshift)+(-\shadowradius/2,\shadowradius/2)$) rectangle ($(#1.north east)+(\shadowshift)+(\shadowradius/2,-\shadowradius/2)$);
        \shade[bottom color=innercolor,top color=outercolor] ($(#1.north west)+(\shadowshift)+(\shadowradius/2,-\shadowradius/2)$) rectangle ($(#1.north east)+(\shadowshift)+(-\shadowradius/2,\shadowradius/2)$);
        \shade[outercolor,right color=innercolor,left color=outercolor] ($(#1.south west)+(\shadowshift)+(-\shadowradius/2,\shadowradius/2)$) rectangle ($(#1.north west)+(\shadowshift)+(\shadowradius/2,-\shadowradius/2)$);
        \filldraw ($(#1.south west)+(\shadowshift)+(\shadowradius/2,\shadowradius/2)$) rectangle ($(#1.north east)+(\shadowshift)-(\shadowradius/2,\shadowradius/2)$);
    \end{pgfonlayer}
}

% create a shadow layer, so that we don't need to worry about overdrawing other things
\pgfdeclarelayer{shadow}
\pgfsetlayers{shadow,main}

\newsavebox\mybox
\newlength\mylen

\newcommand\shadowimage[2][]{%
\setbox0=\hbox{\includegraphics[#1]{#2}}
\setlength\mylen{\wd0}
\ifnum\mylen<\ht0
\setlength\mylen{\ht0}
\fi
\divide \mylen by 120
\def\shadowshift{\mylen,-\mylen}
\def\shadowradius{\the\dimexpr\mylen+\mylen+\mylen\relax}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[#1]{#2}};
\drawshadow{image}
\end{tikzpicture}}

\begin{document}


<<myfigure, echo = FALSE, fig.show = "hide">>=
par(bg = "white")
plot(1)
@

\begin{frame}
\shadowimage[width=7cm]{figure/myfigure-1}
\end{frame}

\end{document}

Figure with shadow.