我想突出显示图像的某个部分并隐藏其余部分。我的最小工作示例如下。任何帮助都将不胜感激。谢谢
\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\node[anchor = south west, inner sep = 0] (A) at (0, 0) {\includegraphics[width = 0.8\paperwidth, height = 10cm, keepaspectratio]{Effile.jpeg}};
\draw[red, ultra thick] (1.05, 5.1) rectangle (2.49, 4.3);
\fill [draw = none, fill = white, fill opacity = 1.0] (1.05, 5.1) -- (2.49, 5.1) -- (2.49, 1.05) -- cycle;
\end{tikzpicture}
\end{frame}
\end{document}
答案1
编辑
使用该
显然不需要该库,因为 Tikz 宏可以使用!overlay-beamer-styles
库可以非常优雅地完成此操作。overlay-beamer-styles
的覆盖语法。beamer
我以相同的方式包含图像,但将节点命名为image
。此外,节点放在哪里并不重要。
\begin{tikzpicture}
\node[anchor = south west, inner sep = 0] (image) {\includegraphics[width = 0.8\paperwidth, height = 10cm, keepaspectratio] {Effile.jpeg}};
然后我可以定义一个范围,其位于(0,0)
左下角image
、(1,0)
右下角、(0,1)
左上角和(1,1)
右上角。
\begin{scope}[shift={(image.south west)},x={(image.south east)},y={(image.north west)}]
现在绘制与图像一起缩放的网格变得非常简单。这样可以轻松识别下一步所需路径的位置。
% Draw a overlaying grid to easily see where the rectangles must be, this can be commented out in the final version
\draw foreach \xy in {0,0.1,...,1.001}{
(\xy,0) -- node[pos=0,below]{\pgfmathprintnumber{\xy}} (\xy,1)
(0,\xy) -- node[pos=0,left]{\pgfmathprintnumber{\xy}} (1,\xy)};
确定了我想要突出显示的片段的路径后,我可以将它们添加到我的\foreach
循环中,该循环会在图像顶部绘制一个大的白色矩形,排除我指定的路径,并在突出显示的片段周围绘制一个额外的红色矩形。此外,该overlay-beamer-styles
库还提供了visible on={<>}
密钥,可与 中的计数器一起使用\foreach
。
\foreach[count=\i] \mypath in {
{(0.1,0.55) rectangle (0.25,0.65)},
{(0.45,0.85) rectangle (0.6,0.95)},
{(0.5,0.5) circle (1cm)},
{(0.75,0.3) rectangle (0.85,0.35) (0.25,0.75) circle (1.5cm)}
}{
\filldraw<\i>[white,opacity=0.75,even odd rule] (0,0) rectangle (1,1) \mypath;
\draw<\i>[red,ultra thick] \mypath;
}
最后关闭scope
和tikzpicture
。
\end{scope}
\end{tikzpicture}
其结果是:
其中网格仍然可见,并且白色矩形是“透明的”,但对于最终版本,可以将其设为不透明,并且可以隐藏网格。
总 MWE(另请注意,我绘制了网格在上面图像和白色矩形,因为否则它不会很有帮助):
\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\node[anchor = south west, inner sep = 0] (image) {\includegraphics[width = 0.8\paperwidth, height = 10cm, keepaspectratio] {Effile.jpeg}};
\begin{scope}[shift={(image.south west)},x={(image.south east)},y={(image.north west)}]
\foreach[count=\i] \mypath in {
{(0.1,0.55) rectangle (0.25,0.65)},
{(0.45,0.85) rectangle (0.6,0.95)},
{(0.5,0.5) circle (1cm)},
{(0.75,0.3) rectangle (0.85,0.35) (0.25,0.75) circle (1.5cm)}
}{
\filldraw<\i>[white,even odd rule] (0,0) rectangle (1,1) \mypath;
\draw<\i>[red,ultra thick] \mypath;
}
% Draw a overlaying grid to easily see where the rectangles must be, this can be commented out in the final version
% \draw foreach \xy in {0,0.1,...,1.001}{
% (\xy,0) -- node[pos=0,below]{\pgfmathprintnumber{\xy}} (\xy,1)
% (0,\xy) -- node[pos=0,left]{\pgfmathprintnumber{\xy}} (1,\xy)};
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}
编辑2
由于@marmot 在他的回答中展示了一个奇特的例子,我必须自己添加一些奇特的东西:)
可以通过以下方式实现:
\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\node[anchor = south west, inner sep = 0] (image) {\includegraphics[width = 0.8\paperwidth, height = 10cm, keepaspectratio] {Effile.jpeg}};
\begin{scope}[shift={(image.south west)},x={(image.south east)},y={(image.north west)}]
\clip (0,0) rectangle (1,1);
\foreach[count=\i] \myDim in {0,5,...,106}{
\begin{scope}[shift={(0.5,0.5)}]
\filldraw<\i>[white,opacity=1,even odd rule,] (-0.5,-0.5) rectangle (0.5,0.5) (0,1.38*\myDim mm)
-- ++(288:\myDim mm) -- ++( 0:\myDim mm)
-- ++(216:\myDim mm) -- ++(288:\myDim mm)
-- ++(144:\myDim mm) -- ++(216:\myDim mm)
-- ++( 72:\myDim mm) -- ++(144:\myDim mm)
-- ++( 0:\myDim mm) -- cycle;
\draw<\i>[red,ultra thick] (0,1.38*\myDim mm)
-- ++(288:\myDim mm) -- ++( 0:\myDim mm)
-- ++(216:\myDim mm) -- ++(288:\myDim mm)
-- ++(144:\myDim mm) -- ++(216:\myDim mm)
-- ++( 72:\myDim mm) -- ++(144:\myDim mm)
-- ++( 0:\myDim mm) -- cycle;
\end{scope}
}
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}
编辑3
这(可能(也许))将是最后一个:
完成:
\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\node[anchor = south west, inner sep = 0,outer sep=0.5\pgflinewidth] (image) {\includegraphics[width = 0.8\paperwidth, height = 10cm, keepaspectratio] {Effile.jpeg}};
\clip (image.south west) rectangle (image.north east);
\foreach[count=\k] \myDim in {0,0.2,...,3.4}{
\ifnum\k>1
\def\mypath{
[scale={\myDim^2}] (image.center)
foreach \myPhi [count=\i,evaluate=\i as \j using {0.5*mod(\i,2)+0.5}] in {0,36,...,359}{
\ifnum\i=1
+({90+50*sqrt(\k)+\myPhi}:\j)
\else
-- +({90+50*sqrt(\k)+\myPhi}:\j)
\fi
} -- cycle
}
\else
\def\mypath{}
\fi
\filldraw<\k>[white]
(image.south west) rectangle (image.north east) \mypath;
\draw<\k>[red,ultra thick] \mypath;
}
\end{tikzpicture}
\end{frame}
\end{document}
编辑4
这确实是最后一个:
使用命令完成\clip
(偷偷从@marmot 的答案中偷来的)。我还定义了一个use node cs
以节点名称为参数的样式,这样更容易在相对坐标系中绘制:
\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{
use node cs/.style={shift={(#1.south west)},x={(#1.south east)},y={(#1.north west)}}
}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\node[anchor = south west, inner sep = 0,opacity=0] (image) {\includegraphics[width = 0.8\paperwidth, height = 10cm, keepaspectratio] {Effile.jpeg}};
\clip (image.south west) rectangle (image.north east);
\foreach \myDim [count=\i] in{0,...,30}{
\clip<\i>[use node cs=image] let
\n1={max(\myDim,0)},
\n2={max(\myDim - 1,0)},
\n3={max(\myDim - 2,0)},
\n4={max(\myDim - 3,0)},
\n5={max(\myDim - 4,0)}
in
(0.1,0.2) circle (\n1 mm)
(0.6,0.4) circle (\n2 mm)
(0.8,0.2) circle (\n5 mm)
(0.5,0.3) circle (\n1 mm)
(0.1,0.8) circle (\n5 mm)
(0.4,0.9) circle (\n3 mm)
(0.7,0.7) circle (\n1 mm)
(0.2,0.5) circle (\n3 mm)
(0.9,0.6) circle (\n2 mm)
(0.9,0.8) circle (\n1 mm);
}
\node[anchor = south west, inner sep = 0] {\includegraphics[width = 0.8\paperwidth, height = 10cm, keepaspectratio] {Effile.jpeg}};
\end{tikzpicture}
\end{frame}
\end{document}
答案2
我认为 Max 已经给了你一个完美的答案。这里只是为了好玩,并展示一种使用 clip 的略有不同的方法。你可以使用 来剪辑所需的区域\only
。
\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\coordinate (O) at (0,0);
\node[anchor = south west, inner sep = 0,opacity=0] (A) at (O) {\includegraphics[width = 0.8\paperwidth, height = 10cm, keepaspectratio]{Effile.jpeg}};
\only<1>{\clip (A.center) circle (1cm);}
\only<2>{\clip (A.center) circle (2cm);}
\only<3>{\clip (1.05, 5.1) rectangle (2.49, 4.3);}
\only<4>{}
\node[anchor = south west, inner sep = 0] at (O) {\includegraphics[width = 0.8\paperwidth, height = 10cm, keepaspectratio]{Effile.jpeg}};
\only<3>{\draw[red, ultra thick] (1.05, 5.1) rectangle (2.49, 4.3);}
\end{tikzpicture}
\end{frame}
\end{document}
当然,人们可以摆脱它\only
,并做其他类似的事情。
\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\coordinate (O) at (0,0);
\node[anchor = south west, inner sep = 0,opacity=0] (A) at (O) {\includegraphics[width = 0.8\paperwidth, height = 10cm, keepaspectratio]{Effile.jpeg}};
\foreach \X [count=\Y] in {0.9,0.8,...,0.1}
{
\path (A.south west) -- (A.north east) coordinate[pos=\X*0.5] (BL)
coordinate[pos=1-0.5*\X] (TR);
\clip<\Y> (BL) rectangle (TR);
}
\node[anchor = south west, inner sep = 0] at (O) {\includegraphics[width = 0.8\paperwidth, height = 10cm, keepaspectratio]{Effile.jpeg}};
\end{tikzpicture}
\end{frame}
\end{document}
编辑:我还没看过 Max 的超级棒的更新。所以我不得不用更短的方式重写他漂亮的星星代码。可以加载shapes.geometric
,其中也有一颗星星(和许多其他形状),然后用它path picture
来根据该形状剪辑图片。;-)
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\path[use as bounding box] (-6,-4) rectangle (6,4);
\foreach \X [count=\Y]in {2,2.4,...,16}
{\node<\Y> [rotate=\X*60,star, star point height=0.25*\X cm, minimum size=\X cm, draw,
path picture={
\node{\includegraphics[width = 0.8\paperwidth, height = 10cm,
keepaspectratio] {Effile.jpeg}};}]
(image) {};}
\end{tikzpicture}
\end{frame}
\end{document}
答案3
也许这会有所帮助。通过正确命名节点,您可以使用fill=<color>
with fill opacity=<num>
。通过命名节点,我们可以轻松访问顶点的坐标,从而我们可以流经我们想要的区域:
\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\begin{document}
\begin{frame}
% yes this is the original pic just for comparing
\begin{tikzpicture}
\node[anchor = south west, inner sep = 0] (A) at (0, 0) {\includegraphics[width = 0.2\paperwidth, height = 10cm, keepaspectratio]{example-image-a}};
\end{tikzpicture}
% the actual stuff, the highlighted picture!
\begin{tikzpicture}
\node[anchor = south west, inner sep = 0] (A) at (0, 0) {\includegraphics[width = 0.2\paperwidth, height = 10cm, keepaspectratio]{example-image-a}};
\draw[red,ultra thick] (0,0) rectangle (0.5,0.5);
\fill [draw=none, fill=white, fill opacity=1]
(0,0.5) --
(A.north west) -- (A.north east) -- (A.south east) --
(0.5,0) -- (0.5,0.5) -- cycle;
\end{tikzpicture}
\end{frame}
\end{document}
这将为您提供: