Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

\theoremstyle{plain}
\newtheorem{prop}{Proposition}
\newenvironment{propExt}[1][]{\ifx!#1! \begin{prop} 
\else \begin{prop}(Proof in \ref{#1})} 
{\end{prop}}

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

\begin{propExt}[label_name]

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?

share|improve this question
    
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
add comment

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

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

1 Answer

up vote 2 down vote accepted

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:

\theoremstyle{plain}
\newtheorem{prop}{Proposition}
\newenvironment{propExt}[1][\empty]
  {\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
\theoremstyle{plain}
\newtheorem{prop}{Proposition}
\NewDocumentEnvironment{propExt}{o}
  {\begin{prop}% Start prop
   \IfNoValueTF{#1}{(Proof in~\ref{#1})}{}}% Conditionally add (Proof in~\ref{#1}
  {\end{prop}}

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})}.

share|improve this answer
    
This is perfect. Many thanks! –  Emmanuel Leclercq May 20 '13 at 15:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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