How to Elegantly Annotate PDFs with LaTeX
My professor shared a PDF Beamer file, and I want to add mathematical annotations to it. How can I do this elegantly?
Let me cut to the chase—it’s getting late and I need some sleep!
The inspiration for this solution comes from Stackexchange ↗. I’ve tried several annotation software options before, but I really want to bring only my iPad to class. Using VNC or xrdp to remotely access Linux just feels clunky and has terrible latency. So I’ve settled on using Overleaf + LaTeX with PDF page inclusion for annotations.
The Stackexchange author provided this solution:
\documentclass{article}
%\url{http://tex.stackexchange.com/q/85651/86}
\usepackage[svgnames]{xcolor}
\usepackage{pdfpages}
\usepackage{tikz}
\tikzset{
every node/.style={
anchor=mid west,
}
}
\makeatletter
\pgfkeys{/form field/.code 2 args={\expandafter\global\expandafter\def\csname field@#1\expandafter\endcsname\expandafter{#2}}}
\newcommand{\place}[3][]{\node[#1] at (#2) {\csname field@#3\endcsname};}
\makeatother
\newcommand{\xmark}[1]{\node at (#1) {X};}
\begin{document}
\foreach \mykey/\myvalue in {
ctsfn/{Defined in Week 1},
metsp/{Defined in Week 3},
} {
\pgfkeys{/form field={\mykey}{\myvalue}}
}
\includepdf[
pages=1,
picturecommand={%
\begin{tikzpicture}[remember picture,overlay]
%%% The next lines draw a useful grid - get rid of them (comment them out) on the final version
\draw[gray] (current page.south west) grid (current page.north east);
\foreach \k in {1,...,28} {
\path (current page.south east) ++(-2,\k) node {\k};
}
\foreach \k in {1,...,20} {
\path (current page.south west) ++(\k,2) node {\k};
}
%%% grid code ends here
\tikzset{every node/.append style={fill=Honeydew,font=\large}}
\place[name=ctsfn]{14cm,17cm}{ctsfn}
\place[name=metsp]{11cm,9cm}{metsp}
\draw[ultra thick,blue,->] (ctsfn) to[out=135,in=90] (9cm,17.3cm);
\draw[ultra thick,blue,->] (metsp) to[out=155,in=70] (6cm,9cm);
\end{tikzpicture}
}
]{tikzmark_example.pdf}
\end{document}
plaintextThe original author’s result:
This immediately caught my eye because:
- It has a coordinate grid, making annotation placement super convenient
- It’s highly extensible—you can mix text and graphics, insert TikZ diagrams, mathematical formulas, you name it!
However, there were several issues to address:
- The professor’s Beamer slides are in landscape format, but this code produces portrait output
- The macro definitions are somewhat messy, and I don’t need fancy connecting lines. Plus, the
includepdf
call is too verbose and inelegant for repeated use - The coordinate grid looks pretty ugly
So here’s how I solved these problems:
-
Fix the orientation: Use
\usepackage[paperwidth=12cm, paperheight=16cm, landscape]{geometry}
to make it landscape format. -
Create a clean macro to simplify
includepdf
usage and support multiple annotations:
\newcommand{\includePDFWithAnnotations}[2]{
\includepdf[
pages=#1,
picturecommand={%
\begin{tikzpicture}[remember picture,overlay]
%%% The next lines draw a useful grid - get rid of them (comment them out) on the final version
\draw[very thin, lightgray] (current page.south west) grid (current page.north east);
\foreach \k in {0,...,11} {
\path (current page.south east) ++(-0.55,\k + 0.2) node[font=\tiny] {\k};
}
\foreach \k in {0,...,14} {
\path (current page.south west) ++(\k,0.2) node[font=\tiny] {\k};
}
%%% grid code ends here
\tikzset{every node/.append style={fill=Honeydew,font=\huge}}
% Iterate through annotation list and place annotations
#2
\end{tikzpicture}
}
]{YOUR PDF NAME.pdf}
}
plaintext- Use the macro elegantly to insert multiple annotations:
\includePDFWithAnnotations{1}{
\place{5, 4}{$123avd$}
\place{7, 8}{$456xyz$}
}
\includePDFWithAnnotations{7}{
\place{5, 4}{$123avd$}
\place{7, 8}{$456xyz$}
}
plaintext- Improve the aesthetics: Move the coordinate grid to the page edges, use tiny font size, make the lines thinner and lighter colored. Much more visually appealing!
Isn’t that satisfying?
Here’s the complete TeX example:
\documentclass[UTF8]{ctexart}
\usepackage[svgnames]{xcolor}
\usepackage[paperwidth=12cm, paperheight=16cm, landscape]{geometry}
\usepackage{pdfpages}
\usepackage{tikz}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\tikzset{
every node/.style={
anchor=mid west,
}
}
\makeatletter
\pgfkeys{/form field/.code 2 args={\expandafter\global\expandafter\def\csname field@#1\expandafter\endcsname\expandafter{#2}}}
\newcommand{\place}[2]{\node at (#1) {\large #2};}
\makeatother
\newcommand{\xmark}[1]{\node at (#1) {X};}
\newcommand{\NotePage}[2]{
\includepdf[
pages=#1,
picturecommand={%
\begin{tikzpicture}[remember picture,overlay]
%%% The next lines draw a useful grid - get rid of them (comment them out) on the final version
\draw[very thin, lightgray] (current page.south west) grid (current page.north east);
\foreach \k in {0,...,11} {
\path (current page.south east) ++(-0.45,\k + 0.2) node[font=\tiny] {\k};
}
\foreach \k in {0,...,14} {
\path (current page.south west) ++(\k,0.2) node[font=\tiny] {\k};
}
\place{0,11.25}{Page #1}
%%% grid code ends here
\tikzset{every node/.append style={fill=Honeydew,font=\huge}}
#2
\end{tikzpicture}
}
]{LA14.pdf}
}
\begin{document}
\NotePage{1}{
\place{1,4.5}{That is because $\det{A} = \det{A^\top}$}
}
\NotePage{2}{}
\end{document}
plaintext