I would like to define a new environment with an optional parameter. Currently, I have

\newenvironment{propExt}[1][]{\ifx!#1! \begin{prop} 
\else \begin{prop}(Proof in \ref{#1})} 

When I put the optional argument, everything works fine, e.g.


However, with no argument, \begin{propExt}, I have the error message

! Incomplete \ifx; all text was ignored after line 283.

I think the problem comes from the fact that \end{prop} is not executed if there is no argument specified, as it follows \else

How should I do?

migrated from stackoverflow.com May 20 '13 at 17:16

This question came from our site for professional and enthusiast programmers.

  • Welcome to TeX.SX! Your post was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. – Martin Schröder May 20 '13 at 17:33

That's because you have no ending clause for the \ifx statement; it has the structure \ifx...[\else]...\fi. A naive approach to achieve what you're after would be:

  {\begin{prop}% Start prop
   \ifx\empty#1\relax\else(Proof in~\ref{#1})\fi}% Conditionally add (Proof in~\ref{#1})
  {\end{prop}}% End prop

This checks whether the supplied optional argument matches \empty (whether this is defined or not). There are other ways of performing these checks. Most notably would be using xparse:

\usepackage{xparse}% http://ctan.org/pkg/xparse
  {\begin{prop}% Start prop
   \IfNoValueTF{#1}{(Proof in~\ref{#1})}{}}% Conditionally add (Proof in~\ref{#1}

xparse provides the condition \IfNoValueTF{<arg>}{<true>}{<false>} where you condition on whether or not a value was supplied to <arg> (a single optional argument in the above case). An even simpler method (if no false clause is required) is to use \IfValueT{#1}{(Proof in~\ref{#1})}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.