2

Is there any way to use parameters of the structure \newenvironment{example}[]{}{} after the text between the \begin{example} and the \end{example}. For example, how do I write a new environment that takes as a parameter the author of a text and write it AFTER the text written in the environment?

4

1 Answer 1

4

EDIT: changed the formatting to match specification from comment.

There are two ways to do this, either, with the command \newenvironment you can store the argument inside a temporary macro (using \def or \renewcommand, but initialising it beforehand with \newcommand in either case). Or you use \NewDocumentEnvironment, in which you can refer to the parameters in the \end-part:

\documentclass[]{article}

\newcommand*\exampleARG{}% to assert the name is free
\newenvironment{example}[1]
  {%
    \par\medskip
    \def\exampleARG{#1}%
    \begingroup
      \centering
      \itshape
      \footnotesize
  }
  {%
      \par
    \endgroup
    \leavevmode\hfill\mbox{\textsc{\exampleARG}}\par
    \medskip
  }

\NewDocumentEnvironment{Example}{m}
  {%
    \par\medskip
    \begingroup
      \centering
      \itshape
      \footnotesize
  }
  {%
      \par
    \endgroup
    \leavevmode\hfill\mbox{\textsc{#1}}\par
    \medskip
  }

\usepackage[]{duckuments}

\begin{document}
\begin{example}{a wise duck}
  \blindduck
\end{example}

\begin{Example}{a wise duck}
  \blindduck
\end{Example}
\end{document}
2
  • thank you very much, this really solved my problem. I'm really new to LaTeX so I had some trouble understanding everything but I somehow figured it out. However it seems overly complicated to do such thing.
    – cento18
    Mar 17, 2021 at 15:34
  • @cento18 not really, imho. The complication is just for formatting, the key point of using the parameter in the end part is just the \def\exampleARG{#1} thingy (or in the \NewDocumentEnvironment case straight forward #1). The rest in the begin-part is only formatting (with \begingroup so it doesn't affect the author), the end part could be done differently (\begingroup\raggedleft\scshape\exampleARG\par\endgroup), but that's no big difference, imho (though that would also work for authors needing more than a single line...).
    – Skillmon
    Mar 17, 2021 at 15:40

You must log in to answer this question.

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