[Tex/LaTex] Command \example already defined

amsthmlyxtheorems

I cannot use \example from any of the Theorems modules because LaTeX says "Command \example already defined" to the line \newtheorem{example}[thm]{\protect\examplename} generated by Lyx. I am using Lyx 2.2.2 on Mac. Any ideas how to fix it?

Best Answer

If you're using the Paper (Standard Class) document class together with Theorems (AMS) module, the latter defines the example environment which conflicts with the document class (since it's already defined).

Unfortunately it does not help placing any corrections in the Document > Settings... > LaTeX Preamble since the User specified LaTeX commands section (this contains whatever you place in the preamble) is only inserted after

\newtheorem{example}[thm]{\protect\examplename}

To fix this, you have to address the module-specific definition via a Local Layout:

Format 60
Style Example
    CopyStyle             Definition
    LatexName             example
    LabelString           "Example \thetheorem."
    Preamble
      \theoremstyle{definition}
      \let\example\relax% Remove any existing example functionality
      \newtheorem{example}[thm]{\protect\examplename}
    EndPreamble
    LangPreamble
      \providecommand{\examplename}{_(Example)}
    EndLangPreamble
    BabelPreamble
      \addto\captions$$lang{\renewcommand{\examplename}{_(Example)}}
    EndBabelPreamble
End

The only addition to this local layout (fix) is the insertion of

\let\example\relax

which removes any existing example environment definitions. This allows \newtheorem{example} to function as expected, without having to change environments or mess with menu items.

Related Question