Sto usando correttamente etoolbox per il confronto delle stringhe?

Aug 31 2020

Ho un piccolo ambiente per enumerare le dichiarazioni di domande. A volte, voglio che l'etichetta non sia un numero ma una stringa. In tal caso, non voglio che il contatore venga incrementato.

% 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}}
  {}}{}

L' if-statementessere qui preoccupante ,

\ifdefstrequal{#1}{\value{QuestionCounter}}
{\stepcounter{QuestionCounter}}
{}

Come posso confrontare il valore (espansione?) Dell'argomento #1e il valore del contatore \value{QuestionCounter}? Ho provato \ifdefstrequal{\value{#1}}{\value{QuestionCounter}}perché pensavo che i \ifdefstrequalprimi due argomenti dovessero essere macro.

Grazie!

Risposte

2 egreg Sep 01 2020 at 07:57

No, è un utilizzo errato. E dovresti fare un test con la piena espansione.

Lo farei in un modo diverso: se l'argomento facoltativo è mancante (o vuoto), il contatore viene spostato e utilizzato per numerare la domanda.

\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}