2

Suppose I have some Latex document. What I would like to do is have an automatic way of highlighting a word in the text and, in the margin of the same line, have a box with some tip about the highlighted word. Here is an example:

enter image description here

For the above document, the way I would want the automation to work is:

\makemargintip{form}{this is a word} loops that move water first away...

The tip would automatically be created in the margin of the same line (or, if the tip is too long, in the margin of the same line and the following lines). Note that unlike marginnote, I want to have a box or some other stylistic element that delimits the tip.

Thank you for helping!

2
  • There is the built-in \marginpar macro, though some variation will be needed if you want color and boxes as you have shown. Aug 24, 2016 at 12:26
  • The package todonotes does all that. It is based on margin notes, but it is automated and easy to customize, and is already styled
    – Elad Den
    Aug 24, 2016 at 19:30

2 Answers 2

3

With \marginpar, xcolor and \fbox.

\documentclass{article}
\usepackage[nopar]{lipsum}
\usepackage{xcolor}
\newcommand\makemargintip[2]{\textcolor{red}{\fbox{\color{black}#1}}%
  \marginpar{\textcolor{red}{\raisebox{7pt}{%
  \fbox{\parbox[t]{\marginparwidth}{\color{black}#2}}}}}}
\begin{document}
\lipsum[1]
\makemargintip{form}{this is a word} loops that move water first away...
\lipsum[2]
\makemargintip{form}{this is a word that goes on} loops that move water first away...
\lipsum[3]
\end{document}

enter image description here

If you want one that goes to the edge of the paper, I merely changed \marginparwidth to some suitable value, in this case 1.64in.

Also, if one wishes the in-text word to be underlined, rather than boxed, merely change the \fbox in the first line of the \makemargintip definition to \underline.

\documentclass{article}
\usepackage[nopar]{lipsum}
\usepackage{xcolor}
\newcommand\makemargintip[2]{\textcolor{red}{\underline{\color{black}#1}}%
  \marginpar{\textcolor{red}{\raisebox{7pt}{%
  \fbox{\parbox[t]{1.64in}{\color{black}#2}}}}}}
\begin{document}
\lipsum[1]
\makemargintip{form}{this is a word} loops that move water first away...
\lipsum[2]
\makemargintip{form}{this is a word that goes on and on} loops that move water first away...
\lipsum[3]
\end{document}

enter image description here

In response to the OP's question about the method not working on even numbered pages, I can surmise that the issue concerns the 2nd of my approaches when using a document class with asymmetric margins where the asymmetry is flipped on every page.

Here is a way to deal with it semi-automatically, except when the tip occurs in a carry-over paragraph from the prior page. If manual intervention is required, one should provide the optional argument [r] on even pages ([l] for odd pages):

\documentclass{book}
\usepackage[nopar]{lipsum}
\usepackage{xcolor,everypage}
\def\pagesense{o}
\AddEverypageHook{\if o\pagesense\gdef\pagesense{e}\else\gdef\pagesense{o}\fi}
\newcommand\makemargintip[3][x]{%
  \textcolor{red}{\underline{\color{black}#2}}%
  \ifx x#1%
    \if o\pagesense\def\tmp{l}\else\def\tmp{r}\fi%
    \marginpar{\makebox[\marginparwidth][\tmp]{\textcolor{red}{\raisebox{7pt}{%
    \fbox{\hspace{20pt}\parbox[t]{1.64in}{\color{black}#3}\hspace{20pt}}}}}}%
  \else%
    \marginpar{\makebox[\marginparwidth][#1]{\textcolor{red}{\raisebox{7pt}{%
    \fbox{\hspace{20pt}\parbox[t]{1.64in}{\color{black}#3}\hspace{20pt}}}}}}%
  \fi%
}
\begin{document}
\lipsum[1]
\makemargintip{form}{this is a word} loops that move water first away...
\lipsum[2]
\makemargintip{form}{this is a word that goes on and on} loops that move water first away...
\lipsum[3-7]
\makemargintip[r]{form}{for carry over paragraph on a page, manual intervention
  is needed, because it is actually ``set'' on the prior page} loops that move water first away...
  \lipsum[8]

But after \makemargintip{that}{but not after that}, no optional argument is needed.
\end{document}

enter image description here

enter image description here

3
  • This works great when the tip is on the right, but it fails when it is on the left (which happens for me on the even-numbered pages). What to do then? Aug 24, 2016 at 17:12
  • @space_voyager, Unfortunately, you haven't really provided enough information yet to address the issue. For example, the \documentclass you are using as well as any packages that may mess with the margins. From what you describe, it sounds as if you have nonsymmetric left/right margins. In general, it is best to post with your question a minimum working example that demonstrates the issue at hand. That way, people don't have to guess your situation. Aug 24, 2016 at 18:01
  • @space_voyager See my addendum Aug 24, 2016 at 18:16
2

The todonotes package uses the same macros as shown in the previous answer, but I believe it is easier to manipulate. Here is my suggestion

\documentclass{article}
\usepackage{todonotes}
\usepackage{xcolor}
\usepackage{lipsum}

\newcommand{\tipword}[1]{\colorlet{currentcolor}{.}{\color{red}\fbox{\color{currentcolor}#1}}}
\newcommand{\tipitself}[1]{\todo[backgroundcolor=white,bordercolor=red,fancyline]{#1}}
\newcommand{\makemargintip}[2]{\tipword{#1}\tipitself{#2}}

\begin{document}
    \lipsum[1-2]


    Now this is where the tip word comes in, it's \makemargintip{form}{this is the tip} can be shaped with the todo package

    \lipsum[3-4]
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .