2

I am trying to create a custom "proof" environment that behaves as follows:

\begin{proof}\end{proof} should produce \textit{Proof.}, while \begin{proof}{method}\end{proof} should produce \textit{Proof (method).}. Here's what I have right now:

\documentclass[10pt]{article}
\usepackage{xifthen}
\newenvironment{proof}[1]
    {\par\vspace{\baselineskip}\noindent\textit{Proof
        {\ifthenelse
            {\isempty{#1}}
            {}
            {(#1)}
    }}}

\begin{document}

\begin{proof}{by contradiction}
    1
\end{proof}

\begin{proof}
    2
\end{proof}

\end{document}

For some reason, I cannot get #1 in parentheses as they always disappear upon compiling. Any ideas?

4
  • 1
    Welcome to TeX.se! Please don't post code fragments. Instead put the fragment into a compilable document that people can play with. This will include any packages needed to make the fragment compile.
    – Alan Munn
    Sep 24, 2023 at 16:08
  • @AlanMunn Fixed! Thank you. Sep 24, 2023 at 16:12
  • 1
    Optional arguments are usually introduced with [...]. That would make things a lot easier. Would that be an acceptable syntax?
    – Alan Munn
    Sep 24, 2023 at 16:24
  • @AlanMunn I seem to run into the same issues when I have that. [1][] makes all the arguments disappear, as I'm sure you can see on your end. Sep 24, 2023 at 16:25

2 Answers 2

3

Optional arguments are usually introduced with [...], and since this is an optional argument to the environment, I think it makes sense to use that syntax. So here's a version that does exactly that:

\documentclass[10pt]{article}

\NewDocumentEnvironment{proof}{o}{
    \par\vspace{\baselineskip}\noindent
    \IfNoValueTF{#1}
    {\textit{Proof}}
    {\textit{Proof (#1)}}
}
{}

\begin{document}

\begin{proof}[by contradiction]
    1
\end{proof}

\begin{proof}
    2
\end{proof}

\end{document}

output of code

2

Alan Munn already beat me to it and probably has a nicer solution. But since I learned that myself just now, here is an alternative which uses the environ package.

\documentclass[10pt]{article}
\usepackage{xifthen}

\usepackage{environ}

\NewEnviron{inner}[1][]{%
  \par\vspace{\baselineskip}\noindent
  \itshape Proof.
  \ifthenelse
  {\isempty{#1}}
  {\BODY}
  {(#1) \BODY}
}

\begin{document}

\begin{inner}[by contradiction]
   1
\end{inner}

\begin{inner}
    2
\end{inner}

\end{document}

enter image description here

You must log in to answer this question.

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