
문맥
텍스트에 자동으로 번호가 매겨진 강조 표시된 댓글을 추가하고 싶습니다.
시도
이에 따라답변나는 다음을 썼다
\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단계씩 증가합니다.
질문
이 카운터에서 단위 증분을 얻는 방법을 알려주시겠습니까?
추신: 저는 라텍스 프로그래밍의 초보자입니다. 이 질문이 바보 같다면 미리 사과드리겠습니다.
답변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
.