54

Is it possible to typeset handwritten comments and annotations in the margins around a paragraph like in the example below?

What would be the closest approximation of this done in LaTeX?

enter image description here

3kstc
  • 931
  • 6
  • 22
  • 4
    For basic margin notes you can use \marginpar; the package marginnote gives improvement. – Willie Wong Jun 29 '16 at 13:44
  • 2
    Related, perhaps, on the subject of handwriting: http://tex.stackexchange.com/questions/29402/how-do-i-make-my-document-look-like-it-was-written-by-a-cthulhu-worshipping-madm. Also, these: http://tex.stackexchange.com/questions/195798/using-handwriting-font-in-proofs, http://tex.stackexchange.com/questions/211588/fraction-written-in-a-handwriting-font – Steven B. Segletes Jun 29 '16 at 13:45
  • It may help if you can be more precise on what you hope to achieve: the hand written look? The two layers of annotations? The arrows pointing between annotation and text? Allowing text to flow into the header/footer space (and not just the left/right margins)? General annotation with underlining/highlighting? The vertically written text? – Willie Wong Jun 29 '16 at 13:49
  • 1
    As to how to place things arbitrarily on the page, irrespective of specified margins, there is this question: http://tex.stackexchange.com/questions/169808/what-are-the-ways-to-position-things-absolutely-on-the-page – Steven B. Segletes Jun 29 '16 at 13:49
  • @WillieWong I want to achieve a page that looks like the example provided or as similar to it as possible. – Bartholomaios Jun 29 '16 at 14:05

1 Answers1

87

Combining results from the questions cited in my comments... namely, doncherry's use of the fts "Teen Spirit" font (http://www.tug.dk/FontCatalogue/teenspirit/) referenced at How do I make my document look like it was written by a Cthulhu-worshipping madman?. I also used my answer at What are the ways to position things absolutely on the page? (adapted for new hook system)

Below, I introduce

\handnote[<angle>]{<x from left>}{<y from top>}{<width>}{<color>}{<content>}

EDITED to introduce the following supporting emphasis markings:

\handline[<thickness-scale>]{<width>}
\handrightarrow[<thickness-scale>]{<width>}
\handleftarrow[<thickness-scale>]{<width>}
\handhookrightarrow[<thickness-scale>]{<width>}
\handhookleftarrow[<thickness-scale>]{<width>}
\handstealthrightarrow[<thickness-scale>]{<width>}
\handstealthleftarrow[<thickness-scale>]{<width>}
\handuparrow[<thickness-scale>]{<height>}
\handdownarrow[<thickness-scale>]{<height>}

These emphasis markings appear as macros within the context of a \handnote's <content>. Thus, when they constitute the complete content of a given \handnote, they may be freely rotated using the <angle> parameter of \handnote. The use of varying <thickness-scale>s and <width>s allows for repetition of emphasis markings to be avoided.

Also EDITED so that the <content> of \handnote is now top-aligned to the specified vertical coordinate given by <y from top>.

Here is the MWE. The emphasis markings are summarized on page 2 (EDITED to account for LaTeX kernel changes on 2018.12.1):

\documentclass{book}
\usepackage{lipsum,stackengine,scalerel,xcolor,emerald,rotating}
\usepackage[T1]{fontenc}
\def\PageTopMargin{1in}
\def\PageLeftMargin{1in}
\newcommand\atxy[3]{%
 \AddToHookNext{shipout/background}{\put(#1,-\dimexpr#2 ){#3}}}
\newcommand\handnote[6][0]{\atxy{#2}{#3}{\rotatebox{#1}{\parbox[t]{#4}{%
  \raggedright\fontfamily{fts}\selectfont\color{#5}#6}}}}
\newcommand\handxform[7]{% SCALING IS EMPLOYED TO EXPAND THE USEFUL RANGE OF #7
  \raisebox{#1}{%
    \scalebox{4}[#6]{% THE 4X SCALING IS LATER COUNTERED BY 0.25
      \raisebox{#2}{%
        \rotatebox{#3}{%
          \stretchto{%
            \rotatebox{#4}{%
              \char#5}%
          }{0.25\dimexpr#7\relax}% THE 0.25 IS COUNTERED BY THE EARLIER 4X SCALING
        }%
      }%
    }%
  }%
}
\newcommand\handline             [2][1]{\handxform{.20ex}{0.30ex}{-90}{ 12}{47}{#1}{#2}}
\newcommand\handrightarrow       [2][1]{\handxform{.55ex}{0.45ex}{-90}{ 90}{62}{#1}{#2}}
\newcommand\handleftarrow        [2][1]{\handxform{.55ex}{-0.5ex}{ 90}{ 90}{62}{#1}{#2}}
\newcommand\handhookrightarrow   [2][1]{\handxform{.70ex}{0.60ex}{-90}{-30}{35}{#1}{#2}}
\newcommand\handhookleftarrow    [2][1]{\handxform{.50ex}{0.60ex}{-90}{ 30}{36}{#1}{#2}}
\newcommand\handstealthrightarrow[2][1]{\handxform{.50ex}{-0.2ex}{ 90}{ 80}{60}{#1}{#2}}
\newcommand\handstealthleftarrow [2][1]{\handxform{.50ex}{0.40ex}{-90}{ 80}{60}{#1}{#2}}

\newcommand\handyform[7]{\raisebox{#1}{\scalebox{#6}[1]{% \rotatebox{#3}{\stretchto{\rotatebox{#4}{\raisebox{#2}{\char#5}}}{#7}}}}} \newcommand\handuparrow [2][1]{\handyform{.10ex}{-.08ex}{ 0}{ -0}{91}{#1}{#2}} \newcommand\handdownarrow [2][1]{\handyform{.05ex}{-.00ex}{ 0}{ -0}{93}{#1}{#2}} \begin{document} \pagecolor{yellow!10!orange!10} \handnote[1]{6.3in}{1.5in}{2in}{red}{\lipsum[4]} \handnote[4]{0.3in}{4.85in}{1.3in}{blue}{I have a really short comment} \handnote[4]{1.3in}{4.88in}{1.3in}{blue}{\handrightarrow[1.7]{4ex}} \handnote[0]{3.8in}{4.82in}{3in}{blue}{% \handline[2]{7ex} \handline[2.5]{6ex} \handline[2]{5ex}}% \handnote[10]{6.2in}{4.8in}{3in}{blue}{\handleftarrow[2]{10ex}}% \handnote[-75]{5.95in}{9.2in}{1.3in}{red}{\handhookleftarrow[2.1]{6ex}} \handnote[20]{6.3in}{9.55in}{1.3in}{red}{What is the meaning of life?
I really want to know. Does the teacher even read this?} \lipsum[1-6]

\lipsum[5-6] \handnote[89]{.5in}{7in}{7in}{red}{Here is a test of a long vertical note! I am thinking it is working just great!} \handnote[10]{6.5in}{5.8in}{2in}{blue}{\Huge The end!!!!} \handnote[2]{2.38in}{4.43in}{3in}{blue}{\handline[2]{12ex}} % \handnote[0]{2in}{6.0in}{4.9in}{red}{\LARGE{\Huge ACCENTS:}\% handlines: \handline{6ex}\handline[3]{12ex} handrightarrows: \handrightarrow{4ex}\handrightarrow[2]{5ex} handleftarrows: \handleftarrow{4ex}\handleftarrow[2]{5ex} handhookrightarrows: \handhookrightarrow[1]{4ex}\handhookrightarrow[2.1]{7ex} handhookleftarrows:\handhookleftarrow[1]{5ex}\handhookleftarrow[2.1]{9ex} handstealthleftarrows: \handstealthleftarrow{5ex}\handstealthleftarrow[2]{5ex} handstealthrightarrows: \handstealthrightarrow{5ex}\handstealthrightarrow[2]{5ex} handuparrows \handuparrow{2ex}\handuparrow[1.2]{4ex} handdownarrows \handdownarrow{2ex}\handdownarrow[1.2]{4ex}} \end{document}

enter image description here

enter image description here

FYI: the Teen Spirit font table is available at http://www.tug.dk/FontCatalogue/teenspirit/teenspirit.pdf, on page 2

EDIT Note: The \handxform macro has been modified to introduce scaling/counterscaling so as to extend the range of sizes over which the \handline mandatory argument (length) may be successfully applied. The \handline macro can now be employed to make lines as long as \LARGE 47ex in length without arithmetic overflow.

  • 1
    Great answer, thanks! Is there any way to draw lines and arrows from the text to the comments or between comments? – Bartholomaios Jun 29 '16 at 14:10
  • 1
    @Bartholomaios Not directly, but there is no reason the <content> of this call can't be a \tikzpicture that does just that. – Steven B. Segletes Jun 29 '16 at 14:13
  • 1
    This is so cool. It would be perfect if each handnote could be an anchor for a tikzpicture node...

    But thank you, this is great!

    – Bartholomaios Jun 29 '16 at 14:21
  • 2
    @Bartholomaios Unfortunately, tikz is not my speciality. – Steven B. Segletes Jun 29 '16 at 14:22
  • 10
    "handnote" should be its own package on ctan! – Bartholomaios Jun 29 '16 at 14:32
  • @StevenB.Segletes What is the fts font family? – NVaughan Jun 29 '16 at 15:10
  • 1
    @NVaughan After more investigation, fts is the "code" for the TeenSpirit font at http://www.tug.dk/FontCatalogue/teenspirit/ – Steven B. Segletes Jun 29 '16 at 15:15
  • @StevenB.Segletes Cheers! – NVaughan Jun 29 '16 at 15:16
  • @StevenB.Segletes could you combine handnotes with handlatex to get a randomized script? – Bartholomaios Jun 29 '16 at 15:20
  • @Bartholomaios Are you sure you provided the right link? It seems to go to something with hyperref... – Steven B. Segletes Jun 29 '16 at 15:32
  • @StevenB.Segletes you are right. Here the correct link – Bartholomaios Jun 29 '16 at 15:34
  • 1
    @Bartholomaios --- for arrows, you can use tikzmark as in this question: http://tex.stackexchange.com/questions/295903/refer-to-a-node-in-tikz-that-will-be-defined-in-the-future-two-passes, combined with http://tex.stackexchange.com/questions/39296/simulating-hand-drawn-lines – Rmano Jun 29 '16 at 15:38
  • @Bartholomaios While I can find hand.dtx at https://github.com/markwgirard/handlatex/blob/master/hand.dtx, I am unable to find the ins file to extract the sty file from it. – Steven B. Segletes Jun 29 '16 at 15:46
  • @StevenB.Segletes it's in this tarball: https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/handlatex/handlatex-1.0.0.tar.bz2 – Bartholomaios Jun 29 '16 at 15:50
  • @Bartholomaios I have revised my answer to provide easier use of realistic hand-drawn emphasis markings like lines and arrows. – Steven B. Segletes Jul 06 '16 at 19:05
  • How would one modify this to use XeLatex and any font using the fontspec package? – ramonskovitch Jul 10 '16 at 07:48
  • @ramonskovitch Removing the \fontfamily{fts}\selectfont from the \handnote macro allows it to be compiled in xelatex (but using the base font in the comments). I'd have to think further on the xelatex syntax for changing the font at that point in the code. – Steven B. Segletes Jul 10 '16 at 18:41
  • @ramonskovitch In the preamble, \usepackage{fontspec}, and in place of \fontfamily{fts}\selectfont replace it with, for example, \setmainfont{Calibri}. Note that all the annotation markings, will have to be revised, or also set in the Teen Spirit font. – Steven B. Segletes Jul 10 '16 at 18:54
  • @Bartholomaios See also my answer here: http://tex.stackexchange.com/questions/320033/how-i-can-customize-the-fill-and-line-in-tikz/320113#320113. Note that while the macros have the same names, they have been revised a bit. Also double-stealth arrow and box function added. – Steven B. Segletes Jul 22 '16 at 13:55