2015-08-24 125 views
0

我在混合knitr,beamer和xelatex製作一些幻燈片演講。爲了讓我的演講更加花哨,我希望看到我的數字會出現陰影。在knitr中投下陰影

我發現this great way從常規數字中刪除陰影,但我無法使其在knitr塊內工作。

你們知道我該如何解決這個問題?

回答

2

使用chunk optionfig.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')}) 

(報價從knitr graphics manualold version;我無法在當前版本中找到它。)

如果您需要塊掛鉤或者設置par(bg = "white")對您而言足夠取決於您生成的圖的數量。如果你不知道如何使用塊鉤子,看看this posting的結尾。

不使用塊鉤和調整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.

+0

謝謝。它像一個魅力一樣工作! –