3

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.

  • 1
    Please provide code we can compile to reproduce the error and tell us the exact text of the error message. But it looks as if you are trying to redefine a command in terms of itself. An environment's contents being italic will not cause text outside that environment to be italic unless you are doing something-wrong-we-know-not-what. But it is hard to help without a minimal example. – cfr Mar 25 '17 at 2:15
  • 1
    If you have an environment called example, you cannot also have a command \example because an environment already defines a command by the same name. But the redefinition you show makes no sense anyway. Why would you not use \begin{example} whatever \end{example}? There is nothing to be gained from the proposed command that would in any way affect italicisation. – cfr Mar 25 '17 at 2:17
  • Just simply because typing the whole thing is kind of slow...and not conforming to my other environment format. – nekodesu Mar 25 '17 at 2:42
  • What does that have to do with italics? If you want a shortcut - not really to be recommended especially, but sometimes useful - just pick a different name. – cfr Mar 25 '17 at 2:44
  • 1
    @Shiyue: This lazyness will bite you in the end. Rather keep the usual \begin{example}...\end{example}. – user31729 Mar 25 '17 at 13:00
5

The problem is that your redefinition is recursive, since \example contains \begin{example} (which is similar to \begingroup\example).

amsmath provides the possibility of producing upright text in regularly-defined theorem-like environments. The default style is plain, which sets the theorem body using \itshape. Using the definition theorem style sets the body in upright font:

enter image description here

\documentclass{article}

\usepackage{amsthm}

%\theoremstyle{plain}% Default style
\newtheorem{exampleA}{Example}
\theoremstyle{definition}
\newtheorem{exampleB}{Example}

\begin{document}

\begin{exampleA}
This is an example.
\end{exampleA}

\begin{exampleB}
This is an example.
\end{exampleB}

\end{document}
5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.