TheUnknownThing的技术分享站

Stay Hungry, Stay Foolish

如何优雅地使用LaTeX做批注

默认分类 0 评 25 阅读

先甩结论,太晚了,想睡觉。
思路来源于 Stackexchange。我之前也尝试过几个软件,但我还是想上课只带iPad,然后远程Linux使用VNC或者xrdp实在是太不优雅,而且延迟颇高。最终还是准备用Overleaf+LaTeX插入PDF Pages来做批注。

Stackexchange上作者给出的解答是:

\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}

原作者的效果图:
原作者的效果图

这个我一看就很心水,因为:

  • 有坐标格子,插入annotation很方便;
  • 可扩展性强,图文混排,插TiKZ的图,公式,都可以做到。

但是, 有几个问题:

  • 老师下发的Beamer文件是横屏的,这个编译出来是竖版;
  • 这里宏定义有点乱,而且我不需要飞线啊之类的东西;而且每次include page都太长,不优雅。
  • 这里坐标太丑了。

所以我来解决一下:

  1. \usepackage[paperwidth=12cm, paperheight=16cm, landscape]{geometry} 使用Geometry让他变为横板。
  2. 定义一个宏来简化 includepdf 的使用,并支持多个批注

    \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}}
     % 遍历批注列表并放置批注
     #2
     \end{tikzpicture}
     }
    ]{YOUR PDF NAME.pdf}
    }
  3. 优雅地使用宏,插入多个批注:

    \includePDFWithAnnotations{1}{
    \place{5, 4}{$123avd$}
    \place{7, 8}{$456xyz$}
    }
    
    \includePDFWithAnnotations{7}{
    \place{5, 4}{$123avd$}
    \place{7, 8}{$456xyz$}
    }
  4. 把坐标的grid移动到页面边缘,字体变成tiny,线条变细、颜色变淡。美学效果好。

最终效果图

是不是很爽!

最后贴上完整的TeX示例:

\documentclass[UTF8]{ctexart}
\usepackage[svgnames]{xcolor}
\usepackage[paperwidth=12cm, paperheight=16cm, landscape]{geometry}
\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}[2]{\node at (#1) {#2};}
\makeatother

\newcommand{\xmark}[1]{\node at (#1) {X};}

\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}}
      % 遍历批注列表并放置批注
      #2
      \end{tikzpicture}
    }
  ]{LA1-4.pdf}
}

\begin{document}

\includePDFWithAnnotations{1}{
  \place{5, 4}{$123avd$}
  \place{7, 8}{$456xyz$}
}

\includePDFWithAnnotations{7}{
  \place{5, 4}{$123avd$}
  \place{7, 8}{$456xyz$}
}

\end{document}
OpenWRT编译二三事
快来做第一个评论的人吧~

:D 获取中...