TeX - LaTeX Asked on August 14, 2021
I would like to add automatically numbered highlighted comments in text.
Following this answer I wrote the following
documentclass{article}
usepackage{color}
usepackage{soul}
newcounter{mycounter}
newcommandshowmycounter{stepcounter{mycounter}themycounter}
definecolor{aquamarine}{rgb}{0.5, 1.0, 0.83}
newcommand{my}[1]{sethlcolor{aquamarine}
protecthl{Comment showmycounter: #1} sethlcolor{yellow}}
begin{document}
some text
my{my first comment}
some more text
my{my second comment}
end{document}
which unfortunately for some strange reason increments the counter in steps of five.
Could you please tell me how to get a unit increment on this counter?
PS: I am a bit of a newbie at latex programming. Let me apologise in advance if this question is dumb.
Providing my comment as an answer: You shouldn't put stepcounter
inside of the argument of hl
. Instead increment the counter before it and only put themycounter
inside hl
:
documentclass{article}
usepackage{color}
usepackage{soul}
newcounter{mycounter}
definecolor{aquamarine}{rgb}{0.5, 1.0, 0.83}
DeclareRobustCommand{my}[1]
{%
sethlcolor{aquamarine}stepcounter{mycounter}%
protecthl{Comment themycounter: #1} sethlcolor{yellow}%
}
begin{document}
some text
my{my first comment}
some more text
my{my second comment}
end{document}
Correct answer by Skillmon on August 14, 2021
The processing of hl
requires multiple passes over its argument in order to do measurements. You discovered that there are five passes and each one increments the counter. Note that stepcounter
acts globally.
You can avoid plunging into the innards of soul
with some more work.
documentclass{article}
usepackage{color}
usepackage{soul}
definecolor{aquamarine}{rgb}{0.5, 1.0, 0.83}
newififstep
newcommand{stepcounteronce}[1]{%
ifstep
globalstepfalse
stepcounter{#1}%
fi
}
newcounter{mycounter}
newcommandshowmycounter{stepcounteronce{mycounter}themycounter}
newcommand{my}[1]{{% an additional group to do sethlcolor locally
globalsteptrue
sethlcolor{aquamarine}%
hl{Comment showmycounter: #1}%
}}
begin{document}
some text
my{my first comment}
some more text
my{my second comment}
This is again hl{yellow}
end{document}
By adding globalsteptrue
you start the machinery that allows stepcounteronce
to do stepcounter
only the first time.
Note the additional group, which allows to avoid explicitly redeclaring sethlcolor
.
Answered by egreg on August 14, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP