
情境
我想在文字中新增自動編號的突出顯示註釋。
試圖
按照此回答我寫了以下內容
\documentclass{article}
\usepackage{color}
\usepackage{soul}
\newcounter{mycounter}
\newcommand\showmycounter{\stepcounter{mycounter}\themycounter}
\definecolor{aquamarine}{rgb}{0.5, 1.0, 0.83}
\newcommand{\my}[1]{\sethlcolor{aquamarine}
\protect\hl{Comment \showmycounter: #1} \sethlcolor{yellow}}
\begin{document}
some text
\my{my first comment}\\
some more text
\my{my second comment}
\end{document}
不幸的是,由於某種奇怪的原因,計數器以五為步長遞增。
問題
您能告訴我如何獲得該計數器的單位增量嗎?
PS:我是乳膠程式設計的新手。如果這個問題很愚蠢,請讓我提前道歉。
答案1
提供我的評論作為答案:您不應該\stepcounter
將\hl
.相反,在它之前增加計數器並只將其放入\themycounter
內部\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}%
\protect\hl{Comment \themycounter: #1} \sethlcolor{yellow}%
}
\begin{document}
some text
\my{my first comment}\\
some more text
\my{my second comment}
\end{document}
答案2
的處理\hl
需要對其參數進行多次傳遞才能進行測量。您發現有五次通過,每次都會遞增計數器。請注意,它\stepcounter
在全球範圍內起作用。
soul
您可以避免陷入更多工作的內部。
\documentclass{article}
\usepackage{color}
\usepackage{soul}
\definecolor{aquamarine}{rgb}{0.5, 1.0, 0.83}
\newif\ifstep
\newcommand{\stepcounteronce}[1]{%
\ifstep
\global\stepfalse
\stepcounter{#1}%
\fi
}
\newcounter{mycounter}
\newcommand\showmycounter{\stepcounteronce{mycounter}\themycounter}
\newcommand{\my}[1]{{% an additional group to do \sethlcolor locally
\global\steptrue
\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}
透過添加,您啟動只允許第一次執行\global\steptrue
的機器。\stepcounteronce
\stepcounter
請注意附加組,它可以避免顯式重新聲明\sethlcolor
.