การเพิ่มเคาน์เตอร์ผิด?
บริบท
ฉันต้องการเพิ่มความคิดเห็นที่เน้นหมายเลขโดยอัตโนมัติในข้อความ
พยายาม
ตามคำตอบนี้ฉันเขียนต่อไปนี้
\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: ฉันเป็นมือใหม่ในการเขียนโปรแกรมน้ำยาง ขออภัยล่วงหน้าหากคำถามนี้ไม่เหมาะสม
คำตอบ
ให้ความคิดเห็นของฉันเป็นคำตอบ: คุณไม่ควรใส่ไว้\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}
การประมวลผลของ\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ครั้งแรกเท่านั้น
หมายเหตุ: กลุ่มเพิ่มเติมซึ่งจะช่วยให้หลีกเลี่ยงอย่างชัดเจน \sethlcolorredeclaring