# Define double environments

TLDR; How can I define a "double" environment like in theorems/proofs (with an optional proof)?

I'm defining a small library that is supposed to behave more or less like theorems/proof environments (I'm using xparse to parse the inputs if it matters). For now, my code is used that way:

\theoremProofEnd[my keys options]{thm}[mytitle]{My theorem}{My optional proof}


(when I don't have proof, I remove the last curly brackets)

But I'd like to make it closer to the usual way people define theorems and proof so that users are not changed too much. So I'd like to be able to use it like:

\begin{proofAtEnd}[my keys options]{thm}[mytitle]
My theorem
\end{proofAtEnd}
\begin{proof} %% <- This environment is optional
My optinal proof
\end{proof}


But I can't find how to do that... Any idea?

MWE:

\documentclass{article}

\usepackage{xparse}
\usepackage{mathtools}
\usepackage{amssymb, amsthm, amsmath, thm-restate}
\usepackage{thmtools} %%

\newtheorem{thm}{Theorem}[section]

\NewDocumentCommand\theoremProofEnd{O{}mO{}+m+g}{
The options are #1''.
\begin{restatable}[#3]{#2}{mymacro}
#4
\end{restatable}
\IfNoValueTF{#5}{}{
\begin{proof}[Custom proof]
#5
\end{proof}
}
}

\begin{document}

\section{My minilib}
% Current syntax
\theoremProofEnd[options A, options B]{thm}[My optional title]{My theorem}{My proof}

\theoremProofEnd[options C]{thm}{My theorem without proof and title}

%% Wanted syntax:
% \begin{proofAtEnd}[options A, options B]{thm}[My optional title]
%   My theorem
% \end{proofAtEnd}
% \begin{proof}
%   My proof
% \end{proof}
%
% \begin{proofAtEnd}[options C]{thm}
%   My theorem without proof and title
% \end{proofAtEnd}

\end{document}

• Maybe I'm missing something here but can't you just use your restatable and proof environments as desired already? – schtandard May 2 '19 at 10:49
• @schtandard Well in practice the code is more complicated than a simple restatable, takes options, like the name of the section that should include the proof, the content of the text to put in the link... See tex.stackexchange.com/questions/484183/… for example. Now, I'd like to see if it's possible to use two environments instead of one command in order to look like the original syntax of theorems/proofs. – tobiasBora May 2 '19 at 11:52
• I guess I just don't see the point of what you are asking. While it should be possible in principle, I would deem it extremely bad practice to have syntax that looks like environments not be environments. If you want to pass some information from proofAtEnd to proof (while keeping them distinct), just make it available outside of proofAtEnd. If you want to have one construct handling both, make the syntax reflect that. You could e.g. have one environment in which you can use an optional command to indicate that everything after belongs to the proof. Or just nest two environments. – schtandard May 2 '19 at 12:12
• @schtandard the method where you define a macro in the first environment and get it from the proof could work, but it won't work it the second environment is facultative. And for the nesting of environments I don't see how to use them, would you have an example? – tobiasBora May 3 '19 at 11:22

Since this is getting a bit extensive for the comments, here's an answer.

Firstly, let me stress that only clear syntax is good syntax. Syntax that looks like one thing but does something else entirely is just confusing. One of the strengths of (good) LaTeX is that syntax always reflects meaning; \emph emphasizes text (no matter what this means typographically), \begin starts an environment, \end ends it.

Thus, while possible in theory, I would strongly discourage building a syntax that looks like two separate environments where the second environment is really an optional argument. Instead, there are some alternatives you could choose from. (I will from now on call the first environment head and the second one tail.)

1. You could really make the environments separate. If you want to pass some information from head to tail, make it available outside of head. This has the disadvantage of "leaking" macros and you have little control over where after head the user puts tail. It will however allow you to use the syntax you desire.

2. You could use the second environment inside the first. This makes sense semantically (tail is an optional part of head) and makes designing the environments rather easy, too (you can make tail and the information needed by it unavailable outside of head naturally by the grouping provided by \begin and end).

3. You could design a macro for use inside of head that marks everything up to \end{head} as part of tail. This is basically the same as the previous option with a slightly different syntax.

I would favor option 2 since it is the most straight forward.

Here's an example of all three options. I didn't dive deep into your link, but it seemed you wanted to store something for later output, so my head just typesets something and my tail collects its content with references to the corresponding head, which is then printed by \printtails.

All of the definitions could of course be improved depending on the use case, but I think the concept becomes clear here.

\documentclass{article}

\usepackage{etoolbox}
\usepackage{calc}
\usepackage{collect}
\usepackage{hyperref}

\makeatletter
\newcounter{tail}
\def\@printtails{}
\def\printtails{\@printtails\def\@printtails{}}
\par\vskip 1em\noindent%
\protected@edef\@currentlabelname{#2}%
\label{#1}%
% make information public
\centering
}{%
\par\vskip 1em%
}
\par\vskip 1em\noindent%
\protected@edef\@currentlabelname{#2}%
\label{#1}%
% make {tail} available
\def\tailB{\@tailB{#1}{#2}}%
\let\endtailB\end@tailB
\centering
}{%
\par\vskip 1em%
}
\par\vskip 1em\noindent%
\protected@edef\@currentlabelname{#2}%
\label{#1}%
% reset the \end{tail} hook
\let\@maybeendtail\relax
% make {tail} available
\def\tailC{%
\let\@maybeendtail\end@tailB
\@tailB{#1}{#2}%
}%
\centering
}{%
% use the \end{tail} hook
\@maybeendtail
\par\vskip 1em%
}
\newenvironment{@printtail}[2]{%
\par\vskip 1em\noindent
\setcounter{tail}{\expandafter\expandafter\expandafter\@firstoffive\csname r@#1\endcsname-1}%
\refstepcounter{tail}%
\label{tailof:#1}%
\begin{tabular}{|p{\textwidth-2\tabcolsep}|}%
}{%
\end{tabular}%
\par\vskip 1em%
}
\newenvironment{tailA}{%
\xappto\@printtails{%
\noexpand\end{@printtail}%
}%
\collectinmacro\@temp@tailcontent{}{}%
}{%
\endcollectinmacro
\expandafter\expandafter\expandafter
\gdef
\expandafter\expandafter
\expandafter
{\@temp@tailcontent}%
}
\newenvironment{@tailB}[2]{%
\xappto\@printtails{%
\noexpand\begin{@printtail}{#1}{#2}%
\expandafter\noexpand\csname @tailcontent@#1\endcsname
\noexpand\end{@printtail}%
}%
\collectinmacro\@temp@tailcontent{}{}%
}{%
\endcollectinmacro
\expandafter\expandafter\expandafter
\gdef
\expandafter\expandafter
\expandafter
{\@temp@tailcontent}%
}
\makeatother

\begin{document}

In vino veritas.

Lorem ipsum dolor sit amet.
\begin{tailA}
Well, it looks like Latin, but it doesn't actually mean anything, does it?
Anyhow, people won't care if we just use it as filler.
\end{tailA}

Haec non sunt verba.
\begin{tailB}
Okay, then.
\end{tailB}