
コンテクスト
テキスト内に自動的に番号が付けられ、強調表示されたコメントを追加したいと思います。
試み
これに続いて答え私は次のように書きました
\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}
残念ながら、何らかの奇妙な理由で、カウンターが 5 ずつ増加します。
質問
このカウンターで単位を増分する方法を教えてください。
追記: 私は LaTeX プログラミングの初心者です。この質問が愚かなものであれば、あらかじめお詫び申し上げます。
答え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
測定を行うために引数を複数回パスする必要があります。 5 つのパスがあり、各パスでカウンターが増加することがわかりました。 は\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
。