Defining an Environment that Takes an Argument

I want to define a new environment that will take a text argument, and use it as a title.

\documentclass{article}
\usepackage{amsthm}

\newenvironment{titleproof}
{\begin{proof} \textbf{Title of the Proof} \newline}
{\vspace*{0.5\baselineskip} \end{proof}}

\begin{document}
\begin{titleproof}

Here is my proof.

I want to be able to write something like begin\{titleproof\}\{New Title\}''
to customize the title of each different proof.

\end{titleproof}
\end{document}


CLARIFICATION I want to be able to do this in general, not just for the proof environment. Here is a more clear MWE:

\documentclass{article}

\newenvironment{titleenv}
{\textbf{Title of the Environment} \newline}
{\vspace*{0.5\baselineskip}}

\begin{document}
\begin{titleenv}

Here is my environment.

I want to be able to write something like begin\{titleenv\}[New Title]''
to customize the title of each different proof.

\end{titleenv}
\end{document}

-

You don't need anything special: the proof environment provided by amsthm allows for an optional argument

\begin{proof}[Title of this proof]
<the proof>
\end{proof}


I suggest you to follow this path, so that removing the optional argument will leave the standard \proofname.

Just to expand on the topic, one can define environments with argument just like for commands:

\newenvironment{foo}[2]
{something with #1 and #2 to be performed at the beginning}
{something to be performed at the end}


Notice that the placeholders for the arguments can be used only in the "begin" part.

You can find examples in every guide to LaTeX, for example "The not so short guide" (texdoc lshort).

-
Just to clarify--I want to know how to do this in general, and I just used the proof as an example... I guess it was a bad choice! Please see the clarification in my question. – jamaicanworm Mar 10 '12 at 22:09
When I try \newenvironment{foo}[2]{\textbf{#1} \newline}{} and then do \begin{foo}[This is my title], for some reason it bolds the [ then does a new line. (In other words, it is treating [ as the argument instead of This is my title.) – jamaicanworm Mar 10 '12 at 22:31
The argument should be in braces; just think to \begin{foo} to be the name of a command; the [2] means that you're requiring two arguments in braces. – egreg Mar 10 '12 at 22:34
@jamaicanworm Please, show what you're trying to do, but not in comments. – egreg Mar 10 '12 at 22:43
It works perfectly now, thanks! – jamaicanworm Mar 10 '12 at 22:45

Just for completeness sake, I'd like to provide another answer. This one uses the amazing xparse. The command we will use for declaring a new environment is:

\DeclareDocumentEnvironment{<environment>}{<arg spec>}{<start code>}{<end code>}


The cool part is the <arg spec>. Here we specify the type of the arguments our new environment will use. Let me show an example:

\DeclareDocumentEnvironment{enva}{O {cake} }{\textbf{I like #1.}}{}
\DeclareDocumentEnvironment{envb}{O {cake} m }{\textbf{I like #1 and #2.}}{}


I declared two environments, enva and envb. The first one has O {cake} as argument specification. According to the xparse documentation, page 2:

• o: A standard LaTeX optional argument, surrounded with square brackets, which will supply the special \NoValue token if not given.

• O: As for o, but returns <default> if no value is given. Should be given as O{<default>}.

The second environment uses m:

• m: A standard mandatory argument, which can either be a single token alone or multiple tokens surrounded by curly braces. Regardless of the input, the argument will be passed to the internal code surrounded by a brace pair. This is the xparse type specifier for a normal TeX argument.

My full example:

\documentclass{article}

\usepackage{xparse}

\DeclareDocumentEnvironment{enva}{O {cake} }{\textbf{I like #1.}}{}
\DeclareDocumentEnvironment{envb}{O {cake} m }{\textbf{I like #1 and #2.}}{}

\begin{document}

\begin{enva}
Hello!
\end{enva}

\begin{enva}[ducks]
Hello!
\end{enva}

\begin{envb}{apples}
Hello!
\end{envb}

\begin{envb}[ducks]{apples}
Hello!
\end{envb}

\end{document}


The output:

There we go. :)

-

You can use a theorem-like structure; in the following example I used the thmtools package as a front-end for amsthm to customize a theorem-like environment:

\documentclass{article}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{lipsum}

\declaretheoremstyle[
spaceabove=\topsep, spacebelow=\topsep,
notefont=\normalfont\bfseries, notebraces={\hspace*{-4pt}}{},
bodyfont=\normalfont,
numbered=no,
]{mystyle}
\declaretheorem[style=mystyle,name={}]{myenv}

\begin{document}

\begin{myenv}[Title one]
\lipsum[2]
\end{myenv}

\begin{myenv}[Title two]
\lipsum[2]
\end{myenv}

\end{document}


-
Would this be so different from \newenvironment{myproof}[1]{\begin{proof}[#1]}{\end{proof}}? – egreg Mar 10 '12 at 21:39
@egreg: yes. With my definition the body of the proof starts in a new line and the proof "name" appears as a note in addition to the usual "Proof"; those were (as I understood them) the requirements of the OP and the use of the optional argument of proof is not enough to fulfill them. – Gonzalo Medina Mar 10 '12 at 21:42
Please see my comment on egreg's answer, and the clarification in my question. – jamaicanworm Mar 10 '12 at 22:10
@jamaicanworm: I updated my answer with a little variation; is it something like this what you are looking for? – Gonzalo Medina Mar 10 '12 at 22:45