文字列比較に etoolbox を正しく使用していますか?

文字列比較に etoolbox を正しく使用していますか?

質問文を列挙するための小さな環境があります。ラベルを数字ではなく文字列にしたい場合があります。その場合、カウンターを増加したくありません。

% 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とカウンターの値をどのように比較すればよいでしょうか?最初の 2 つの引数はマクロにする必要があると思ったので\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}

ここに画像の説明を入力してください

関連情報