[Tex/LaTex] Renew environment commands

environmentsmacros

I'm trying to renew my environment command, specifically the \example command in the amsthm (or something-like-that package).

Specifically, my

\example

command always leave the rest of my text italic (because the environment itself is italic font).

But when I use

\renewcommand{\example}[1]{\begin{example} #1 \end{example}}

it always gives me compiler error.

Best Answer

I don't really recommend this because there is not much benefit to get from the approach as desired by the O.P.

The command \example is defined already as soon the example environment has been declared with \newtheorem, as is \endexample in order to mark the end of the example and doing some 'clean-up' for the end of the environment.

\renewcommand{\example} is basically no problem as long as \example does not appear again inside the redefined \example environment, otherwise it will end in an recursive definition, finally exceeding TeX's memory.

A solution is to save the old definition of \example and \endexamples and use those in an redefinition of \example, together with an extra\begingroup...\endgroup pair in order to prevent the leaking of font etc. changes outside, which has been the case by using \example only as reported by the O.P.

The \foo count stuff is included only in order to show that the group feature is maintained and does not leak outside.

\documentclass{article}

\usepackage{amsthm}

\newtheorem{example}{Example}
\newtheorem{exampleorig}{Example}

\let\origexample\example
\let\origendexample\endexample

\newcount\foo


\renewcommand{\example}[1]{\begingroup\origexample#1\origendexample\endgroup}


\begin{document}

The value of foo is \the\foo

\example{%
Here is an example for the Pythagorean theorem:
\foo=18%
\[ c^{2} = a^{2} + b^{2} \]
}

The new value of foo is \the\foo
\begin{exampleorig}
Here is an example for the Pythagorean theorem:

\[ c^{2} = a^{2} + b^{2} \]
\end{exampleorig}


\end{document}

enter image description here

Related Question