答案1
不久前我写了一些代码用于注释图像,同样适用于在 Beamer 中注释 PDF 文件。我的宏定义了一个AnnotatedImage
环境,其中坐标被缩放,以便矩形图像适合适当缩放的单位正方形,这使得在图像上绘图非常容易。例如,您可以生成以下“Beamer 幻灯片”
使用本质上的代码:
\begin{AnnotatedImage}[width=0.8]{abacus.pdf}
\draw[red, rounded corners, thick](0.2,0.85) rectangle(0.5,0.71);
\draw[<-,red](0.2,0.78)--++(-0.1,0);
\end{AnnotatedImage};
[这个 MWE 无法编译,因为我abacus.pdf
在电脑上使用了一个随机 PDF 文件,而你不会看到这个……不幸的是,像这样的好包姆韦不提供 PDF 文件。] 环境的可选参数允许您设置图像尺寸,并且在环境中,您可以使用任意蒂克兹命令。此外,还有一个\annotate
命令。请参阅带有一些叠加文本的图像更多细节。
对于多页 PDF 文件,这几乎肯定行不通,但如果您提取要注释的页面,它就会起作用。另一个选择是使用pdf页面仅提取您需要的页面。如果您需要这个,请告诉我,因为修改我的代码以使用应该不难pdf页面直接地。
完整代码如下:
\documentclass{beamer}
\usepackage{tikz}
\usepackage{xparse}
\tikzset{% styling for the overlay
annotatedImage/x/.initial = 0.7,% default overlay position
annotatedImage/y/.initial = 0.7,
annotatedImage/width/.initial = 1,% default image width
annotatedImage/.unknown/.code = {% unknown keys are appended to style
% to append to a style we need to expand everything first
\edef\tikzappend{\noexpand\tikzset{annotatedImage/.append style =
{\pgfkeyscurrentname=\pgfkeyscurrentvalue}}}
\tikzappend
},
annotatedImage/.style = {% default overlay style
draw=red, ultra thick, rounded corners, rectangle,
}
}
\newsavebox\annotatedImageBox% need to calculate the width and height
% internal helper functions
\newcommand\AnnotatedImageVal[1]{\pgfkeysvalueof{/tikz/annotatedImage/#1}}
\newcommand\SetUpAnnotatedImage[2]{% set keys and dimensions to scale image
\tikzset{annotatedImage/.cd, #1}%
\sbox\annotatedImageBox{\includegraphics[width=\AnnotatedImageVal{width}\textwidth,
keepaspectratio]{#2}}%
\pgfmathsetmacro\annotatedHeight{\ht\annotatedImageBox/28.453}% convert from px to a tikz length
\pgfmathsetmacro\annotatedWidth{\wd\annotatedImageBox/28.453}%
}
% usage: \annotatedImage[node options]{image}{text}
\NewDocumentCommand\annotatedImage{ O{} m m}{%
\bgroup% start a group to keep tikz changes local
\SetUpAnnotatedImage{#1}{#2}%
\begin{tikzpicture}[xscale=\annotatedWidth, yscale=\annotatedHeight]%
\node[inner sep=0, anchor=south west] (image) at (0,0) {\usebox{\annotatedImageBox}};
\node[annotatedImage] at (\AnnotatedImageVal{x},\AnnotatedImageVal{y}) {#3};
\end{tikzpicture}%y
\egroup%
}
% usage: \annotate <insert favourite node commands>;
\newcommand\annotate[1][]{\node[annotatedImage,#1]}
% usage: \begin{AnnotatedImage][width(default 1)]{image} tikz commands....\end{AnnotatedImage}
\newenvironment{AnnotatedImage}[2][1]{%
\SetUpAnnotatedImage{#1}{#2}%
\tikzpicture[xscale=\annotatedWidth, yscale=\annotatedHeight]
\node[inner sep=0, anchor=south west] at (0,0) {\usebox{\annotatedImageBox}};
}{\endtikzpicture}
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{PDF example}
\begin{AnnotatedImage}[width=0.8]{abacus.pdf}
\draw[red, rounded corners, thick](0.2,0.85) rectangle(0.5,0.71);
\draw[<-,red](0.2,0.78)--++(-0.1,0);
\end{AnnotatedImage};
\end{frame}
\end{document}