
질문 진술을 열거할 수 있는 약간의 환경이 있습니다. 때로는 레이블이 숫자가 아닌 문자열이길 원할 때도 있습니다. 이 경우 카운터가 증가하는 것을 원하지 않습니다.
% question environment
\newcounter{QuestionCounter}
\stepcounter{QuestionCounter}
\newenvironment{question}[1][\arabic{QuestionCounter}] {
\vspace*{0.5\baselineskip}
\noindent\textbf{Question #1. }\ignorespaces
\ifdefstrequal{#1}{\value{QuestionCounter}}
{\stepcounter{QuestionCounter}}
{}}{}
여기에 관한 것은 if-statement
,
\ifdefstrequal{#1}{\value{QuestionCounter}}
{\stepcounter{QuestionCounter}}
{}
인수 값(확장?) #1
과 카운터 값을 어떻게 비교할 수 있나요 \value{QuestionCounter}
? 나는 처음 두 인수가 매크로여야 한다고 \ifdefstrequal{\value{#1}}{\value{QuestionCounter}}
생각했기 때문에 시도해 보았습니다 .\ifdefstrequal
감사해요!
답변1
아니요, 잘못된 사용법입니다. 그리고 전체 확장으로 테스트를 수행해야 합니다.
나는 다른 방식으로 할 것입니다 :만약에선택적 인수가 누락되었거나 비어 있는 경우 카운터가 단계적으로 진행되어 질문에 번호를 매기는 데 사용됩니다.
\documentclass{article}
\newcounter{QuestionCounter}
\newenvironment{question}[1][]
{%
\par\addvspace{0.5\baselineskip}%
\if\relax\detokenize{#1}\relax
\stepcounter{QuestionCounter}%
\thisquestion{\arabic{QuestionCounter}}%
\else
\thisquestion{#1}%
\fi
}{%
\par\addvspace{0.5\baselineskip}%
}
\newcommand{\thisquestion}[1]{%
\noindent\textbf{Question #1. }\ignorespaces
}
\begin{document}
\begin{question}
Is this a numbered question?
\end{question}
\begin{question}[foo]
Is this a numbered question?
\end{question}
\begin{question}
Is this a numbered question?
\end{question}
\end{document}