1

Consider the following LaTeX code, which contains an index entry my index.

\documentclass{scrartcl}
\usepackage{showidx}
\begin{document}
Hello, world!\index{myindex}
\end{document}

This typesets as follows, with the index keyword shown in the margin:

Show index

If the line \usepackage{showidx} is deleted or commented out:

\documentclass{scrartcl}
% \usepackage{showidx}
\begin{document}
Hello, world!\index{myindex}
\end{document}

the index keyword disappears from the margin:

Hide index

A similar mechanism exists for labels using the showlabels package:

\documentclass{scrartcl}
\usepackage{showlabels}
\begin{document}
Hello, world!\label{mylabel}
\end{document}

This produces:

Show labels

whereas deleting or commenting out the line \usepackage{showlabels} produces, as before:

Hide labels

Question

Is there a similar mechanism for margin notes? Consider, for example, the following piece of LaTeX code containing a margin note:

\documentclass{scrartcl}
\begin{document}
Hello, world!\marginpar{A margin note.}
\end{document}

This code typesets as follows:

Show margin note

Is it possible to easily hide all margin notes? More intricately, is it possible to assign identifiers to margin notes (in such a way that several notes can share a single identifier), and then to hide only those notes associated with certain identifiers?

A putative example

A putative piece of LaTeX code in which margin notes are assigned identifiers:

\documentclass{scrartcl}
\usepackage{lipsum}
\begin{document}
\lipsum[1][1-4]\marginpar[tempnote]{1st margin note}

\lipsum[2][1-4]\marginpar{2nd margin note}

\lipsum[3][1-4]\marginpar[tempnote]{3nd margin note} \end{document}

This would produce:

Show all notes

Now the addition of the putative line \usepackage{hidemarginpar}[tempnote] to the preamble:

\documentclass{scrartcl}
\usepackage{hidemarginpar}[tempnote]
\usepackage{lipsum}
\begin{document}
\lipsum[1][1-4]\marginpar[tempnote]{1st margin note}

\lipsum[2][1-4]\marginpar{2nd margin note}

\lipsum[3][1-4]\marginpar[tempnote]{3nd margin note} \end{document}

would produce:

Hide selected notes

Evan Aad
  • 11,066
  • @campa: Fine by me. I'm not married to the name of the command. I only care about the functionality and the ease of use. – Evan Aad Jul 09 '21 at 13:21

1 Answers1

1

Your idea of having something like

\usepackage{hidemarginpar}[tempnote]

is difficult to realize, because one should make \marginpar aware of this "label" (with all the risks coming from the redefinition of a kernel macro). It is better to have code to define your new margin notes from scratch.

\documentclass{article}

\makeatletter

\newcommand*{\newmarginstuff}[1]{% \expandafter@ifdefinable\csname @my@marg@#1\endcsname {\expandafter\let\csname @my@marg@#1\endcsname=\iftrue}%\fi }

\newcommand*{\includemarginstuff}[1]{% @bsphack\expandafter\let\csname @my@marg@#1\endcsname=\iftrue@esphack } %\fi

\newcommand*{\excludemarginstuff}[1]{% @bsphack\expandafter\let\csname @my@marg@#1\endcsname=\iffalse@esphack } %\fi

\newcommand*{\marginstuff}[2][]{% \if\relax\detokenize{#1}\relax \expandafter@firstoftwo \else \expandafter@secondoftwo \fi {\marginpar{#2}}% {\csname @my@marg@#1\endcsname\marginpar{#2}\fi}% }

\makeatother

\newmarginstuff{foo} \newmarginstuff{baz}

\begin{document}

\parskip2ex

Text text text text text text text \marginstuff[foo]{1st foo} text text text text text text text text text text text text text text text text text text

Text text text text text text text \marginstuff{generic} text text text text text text text text text text text text text text text text text text

\excludemarginstuff{foo} Text text text text text text text \marginstuff[foo]{2nd foo} text text text text text text text text text text text text text text text text text text

Text text text text text text text \marginstuff[baz]{1st baz} text text text text text text text text text text text text text text text text text text

\end{document}

enter image description here

campa
  • 31,130