0

I would like to create a custom environment, let's say foo, with an optional and a mandatory argument. This could be an approach:

\NewDocumentEnvironment{foo}{O{default} m}{start #1 #2}{end}

This environment could be used like that:

\begin{foo}[optional value]{mandatory value}
    text
\end{foo}

But I prefer the following method, which I could not implement:

\begin{foo}[optional value]
    text
\end{foo}{mandatory value}

Is it possible to create such an environment, where the mandatory argument follows the end-clause?

  • I’d advise you against this idea, but a use case might possibly clear up the matter. – egreg Feb 20 '22 at 09:28
  • @egreg In my case text is a latin text and the mandatory value is its translation, the optional contains pgf keys. I have to combine the two texts (latin and translation) in one command/environment, because the formatting of both texts depends on the same pgf keys. (I could use a command \foo[key]{text}{translation}, which I find ugly when having long texts and which is inconsistent to my other environments.) To keep the order text+translation, I prefer this last position. I know, it's a little bit special... – Andreas Knobloch Feb 20 '22 at 09:59
  • Wouldn't something like \begin{foo}[<options>] Text \BREAK Translation \end{foo} be better? With a better name than \BREAK, of course. – egreg Feb 20 '22 at 10:16
  • I think it looks very strange to have one text in an enviornment body and one in an argument, either use two environments or two arguments or as egreg suggests a single enviornment with a separator. – David Carlisle Feb 20 '22 at 11:04

1 Answers1

0

For the use case as clarified in comments I would use a more symmetric markup using an environment form for both texts.

enter image description here

\documentclass{article}

\NewDocumentEnvironment{foo}{O{}} {\gdef\lastsettings{#1}\section{with settings: #1}\quote} {\endquote} \NewDocumentEnvironment{foo2}{} {\section{with reused settings: \lastsettings}\quote} {\endquote}

\begin{document}

\begin{foo}[optional value] text \end{foo} \begin{foo2} translated text \end{foo2}

\end{document}

David Carlisle
  • 757,742
  • Both solutions (egreg's and David Carlisle's) are great. Thank you! The separator solution is nicely clean. The solution of two environments with transfer of options has the advantage that I can easily separately check, if one body is empty, e.g. if there is one translation or not (i.e. if there has to be a tcolorbox around the translation or not.) I will think about both. – Andreas Knobloch Feb 20 '22 at 11:41