3

I'm trying to write this new environment, but produces this error when I use it:

Undefined control sequence.
Missing number, treated as zero.
Undefined control sequence.
Extra \endgroup.
Undefined control sequence.
LaTeX Error: \begin{document} ended by \end{questao}.
LaTeX Error: \begin{questao} on input line 69 ended by \end{enumerate}.
Undefined control sequence.
Undefined control sequence.
Undefined control sequence.
You can't use `\relax' after \the.
Undefined control sequence.
Undefined control sequence. Illegal parameter number in definition of \endquestao.

This is my code:

\documentclass{article}



\newcounter{questao}

\newenvironment{questao}[1][]{

    \ifx\relax#1\relax
        \refstepcounter{questao}%

        \noindent \textbf{Questão \thequestao.}

    \else

        \refstepcounter{questao}%
        \noindent \textbf{Questão \thequestao.} #1
        \hspace*{1em} % Adiciona um espaço antes das alternativas
        \begin{enumerate}[label=\alph*)]

    \fi
}{%

    \ifx\relax#1\relax
    \else

        \end{enumerate}

    \fi} [2]: The second error here

\begin{document}

\begin{questao}
    
....

\end{questao} [1]: The first error here

\end{document}

1: This is the first error Undefined control sequence. Missing number, treated as zero. Undefined control sequence. Extra \endgroup. Undefined control sequence. LaTeX Error: \begin{document} ended by \end{questao}. LaTeX Error: \begin{questao} on input line 69 ended by \end{enumerate}. Undefined control sequence. Undefined control sequence. Undefined control sequence. You can't use `\relax' after \the. Undefined control sequence. Undefined control sequence.

2: This is the second error Illegal parameter number in definition of \endquestao.

4
  • 2
    Welcome. Please copy paste the error as code. Never post images of code or errors. It can be searched, can't be copied and can't be accessed by people using screen-reading software. What is the first error? LaTeX won't say just Undefined control sequence. It will tell you which control sequence is undefined and on which line. That's the information we need to help you. Subsequent errors are irrelevant until you get rid of the first. Almost always any error leads to others.
    – cfr
    Commented May 27 at 3:35
  • 2
    I see you edited your question to invalidate my answer, but not to provide details in an accessible format.
    – cfr
    Commented May 27 at 4:19
  • 1
    Instead of using \ifx\relax#1\relax to test for emptiness you can use the built in \IfBlankTF (which would treat spaces as empty as well, so behave slighty different, but is much more robust).
    – Skillmon
    Commented May 27 at 7:57
  • Sorry, cfr. I'm new here, and I'm learning how it works. I do my best to provide the details in an accessible format. Is this approach acceptable? Commented May 27 at 17:36

2 Answers 2

6

TeX is not free form. Your code has a lot of unwanted spaces and paragraph endings.

The main problem, though, is the usage of #1 in the end part of \newenvironment, which is not allowed.

You can use \NewDocumentEnvironment and abbreviate your code a lot.

\documentclass{article}
\usepackage{enumitem}

\newcounter{questao}
\NewDocumentEnvironment{questao}{o}{%
  % ensure being between paragraphs; add some vertical space
  % and tell LaTeX you prefer a page break here and not at enumerate
  \par\addvspace{\topsep}\pagebreak[2]
  % start the question
  \noindent\refstepcounter{questao}\textbf{Questão \thequestao.}%
  % if the optional argument is present
  \IfValueT{#1}{ #1\begin{enumerate}[label=\alph*)]}%
}{%
  \IfValueT{#1}{\end{enumerate}}%
  \par\addvspace{\topsep}%
}

\begin{document}

\begin{questao}
Something about something else
\end{questao}

\begin{questao}[Title]
\item a
\item b
\item c
\end{questao}

\end{document}

enter image description here

1
  • Thank you, this is a better solution than the first solution. Commented May 27 at 17:02
5

[[1]] is wrong. You need [1].

EDIT

Please don't edit questions to invalidate existing answers.

You cannot use #1 in the definition of the end of an environment created with \newenvironment because the way the environment is implemented is really as two commands and the second doesn't have access to the arguments. So questao is \questao and \endquestao. The first of these does the tests for any optional argument and absorbs the relevant number of optional and/or mandatory arguments, but \endquestao is just \endquestao. So, if you want to reuse an argument in the definition of the end, you would need to save it first.

\NewDocumentEnvironment is cleverer and ensures the second command also has access to the arguments. As Skillman points out, this also allows you to use \IfValueTF{#1}{}{} to test for the existence of an optional argument o.

\documentclass{article}
\newcounter{questao}

\NewDocumentEnvironment {questao} {o}
{% begin environment
  \refstepcounter{questao}% do this in all cases
  \noindent \textbf{Questão \thequestao.} 
  \IfValueT{#1}{% only if there is an optional argument
    #1
    \hspace*{1em} % Adiciona um espaço antes das alternativas
    \begin{enumerate}[label=\alph*)] 
    }%
  }{% end environment: nothing to do unless optional argument
    \IfValueT{#1}{\end{enumerate}}%
}

\begin{document}
\begin{questao}
    
....

\end{questao} (The error is not here)

\end{document}

See usrguide.pdf for documentation.

4
  • Thank you, it's work! Commented May 27 at 4:42
  • 1
    Instead of testing for emptiness and using O{} I'd use o and \IfNoValueTF when using \NewDocumentEnvironment.
    – Skillmon
    Commented May 27 at 7:57
  • @Skillmon Thanks.
    – cfr
    Commented May 27 at 13:18
  • 1
    @LenindeCastilhos Please see edit. This is better. I'm not sure the \hspace* is doing anything given you're starting a list - unless that is redefined?
    – cfr
    Commented May 27 at 13:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .