
У меня есть небольшая среда для перечисления вопросительных утверждений. Иногда я хочу, чтобы метка была не числом, а какой-то строкой. В этом случае я не хочу, чтобы счетчик увеличивался.
% 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}