0

I have a command \point that adds a $\bigstar$ as a margin note using the marginnote package. But I want multiple stars next to each other if there are multiple \point in each line. So Hello \point\point world. Generates two $\bigstar$ in the margin note. For now, both stars are generated but I think they overlap each other and look like there was only one.

My idea was to use a counter that resets on each linebreak. But is this possible? I've found commands to execute a command at the beginning of a paragraph using \everypar. Is there a command to add something to every generated output line of the document?

1 Answer 1

2

Here is an alternative suggestion, that adds a test to \marginnote, if the last \marginnote has the same vertical position and in this case move the output by the width of a \bigstar to the right:

\documentclass{article}
\usepackage{amssymb}
\usepackage{marginnote}
\usepackage{xpatch}

\makeatletter
\newcommand*\@mn@lastypos{\relax}
\newcommand*\@mn@lastpage{\relax}
\newlength{\@mn@currmove}
\setlength{\@mn@currmove}{\z@}
\providecommand\@firstofthree[3]{#1}
\providecommand\@secondofthree[3]{#2}
%\providecommand\@thirdofthree[3]{#3}% Already defined in LaTeX from 1998/03/20
\@ifundefined{pdflastypos}{%
  \let\mn@lastypos\lastypos
}{%
  \let\mn@lastypos\pdflastypos
}
\xpatchcmd{\@mn@margintest}{%
  \protected@write\@auxout{\let\themn@abspage\relax}{%
    \string\newmarginnote{note.\@mn@thispage.\@mn@atthispage}{%
      {\themn@abspage}{\noexpand\number\mn@lastxpos sp}}%
  }%
}{% extend \newmarginnote by the y pos
  \protected@write\@auxout{\let\themn@abspage\relax}{%
    \string\newmarginnote{note.\@mn@thispage.\@mn@atthispage}{%
      {\themn@abspage}{\noexpand\number\mn@lastxpos sp}{\noexpand\number\mn@lastypos}}%
  }%
}{}{\undefined}
\xpatchcmd{\@mn@margintest}{%
  \edef\@mn@currxpos{\expandafter\@secondoftwo\@mn@currpage}%
}{% the x pos is now the second of three arguments and the third is the y pos
  \edef\@mn@currxpos{\expandafter\@secondofthree\@mn@currpage}%
  \edef\@mn@currypos{\expandafter\@thirdofthree\@mn@currpage}%
}{}{\undefined}
\xpatchcmd{\@mn@margintest}{%
  \edef\@mn@currpage{\expandafter\@firstoftwo\@mn@currpage}%
}{% the page number is now the first of three arguments
  \edef\@mn@currpage{\expandafter\@firstofthree\@mn@currpage}%
}{}{\undefined}
\xapptocmd{\@mn@margintest}{% now add the y pos testing and the offset
  \settowidth\@tempdima{\ensuremath{\bigstar}}%
  \ifx\@mn@lastpage\@mn@currpage
    \ifx\@mn@lastypos\@mn@currypos
      \global\advance\@mn@currmove\@tempdima
    \else
      \global\@mn@currmove\z@
    \fi
  \else
    \global\@mn@currmove\z@
  \fi
  \@ifundefined{@mn@currxpos}{}{%  
    \edef\@mn@currxpos{\the\dimexpr \@mn@currxpos-\@mn@currmove\relax}%
  }%
  \global\let\@mn@lastpage\@mn@currpage
  \global\let\@mn@lastypos\@mn@currypos
}{}{\undefined}
\makeatother

\newcommand*{\point}{\marginnote{\ensuremath{\bigstar}}}

\begin{document}
Test\point

Test\point Test\point

Test\point Test\point Test\point

Test\point
\end{document}

result with up to three points

5
  • 1
    Very interesting. I suggest \providecommand instead of \def for \@firstofthree and the like. Some packages define them and usually they’re \long.
    – egreg
    Jan 18, 2023 at 14:56
  • @egreg You are right. Changed.
    – cabohah
    Jan 18, 2023 at 15:27
  • Sorry for not having upvoted earlier, but I was on the phone and already typing in the comment was a tough business. 😀
    – egreg
    Jan 18, 2023 at 15:29
  • @egreg Real multitasking!
    – cabohah
    Jan 18, 2023 at 15:30
  • @cabohah Thank you! It works perfectly!
    – Survari
    Jan 19, 2023 at 7:28

You must log in to answer this question.

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