[Tex/LaTex] How to make a custom theorem for a definition

formattingoptional argumentstheorems

Looking at the wikibooks article on theorems has me wondering… I'd like to do something, if possible, similar to the default "proof" theorem style, whereby I can add a custom name to each theorem as I go along..

Eample: In the wikibooks here, it says that if I use the following code, I get the opportunity to name the proof:

\begin{proof}[Proof of important theorem]
    Here is my important proof
\end{proof}

If I were to leave out the [proof of important theorem], it would just say "Proof:" instead.

I'd like to make it possible to define a definition theorem that always says "Definition #", but I can optionally add a name to it so it may say something like "Definition 3.1 – The answer of the universe and everything", followed by the definition in question.

Right now, this is all I have. I don't know much about TeX yet as I'm just learning it now.

\newtheoremstyle{definitionstyle} % name of the style to be used
  {10mm}        % measure of space to leave above the theorem. E.g.: 3pt
  {10mm}        % measure of space to leave below the theorem. E.g.: 3pt
  {}            % name of font to use in the body of the theorem
  {}            % measure of space to indent
  {\bfseries}   % name of head font
  {\newline}    % punctuation between head and body
  {10mm}        % space after theorem head
  {}            % Manually specify head
\theoremstyle{definitionstyle}
\newtheorem{mydef}{Definition}[section]

Best Answer

All theorem environments support an optional argument for a theorem's name or a note. In the following example, I use the thmtools package to format the note. (The amsthm package still provides some underlying functionality, but I prefer thmtools' key-value-syntax.)

\documentclass{article}

\usepackage{amsthm}
\usepackage{thmtools}

\declaretheoremstyle[%
  within=section,%
  spaceabove=10mm,%
  spacebelow=10mm%,
  headfont=\bfseries,%
  headpunct={},%
  postheadspace=\newline,%
  notefont=\normalfont\itshape,%
  notebraces={--~}{},% punctuation before and after the note
]{definitionstyle}
\declaretheorem[style=definitionstyle,name=Definition]{mydef}

\usepackage{lipsum}

\begin{document}

\section{foo}

\begin{mydef}[The answer of the universe and everything]
\lipsum[1]
\end{mydef}

\end{document}

enter image description here