# Marginal enumeration not in the margins

Based on an answer to one of my previous questions, I implemented the following code which is supposed to enumerate examples discretely in the margins of my document:

\documentclass{memoir}

\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{amsmath,amssymb,geometry,graphicx,amstext}

\usepackage[amsmath]{ntheorem}

% margin theorem
\makeatletter
\newtheoremstyle{mymargin}%
{\item[\theorem@headerfont \llap{##1 ##2}| ##3\theorem@separator\hskip\labelsep]}%
\makeatother

% my example
\theoremstyle{mymargin}
\newtheorem{example}{Example}
\numberwithin{example}{chapter}
\begin{document}
\begin{example}
\centering
\begin{tabular}{cccc}
this & is & a & test\\
table & where  & text & goes
\end{tabular}
\end{example}
\end{document}


This produces the following output:

As you see, because the table is centered, the example number is not actually in the margins, which is my major concern. Furthermore, I would like the number to appear aligned with the top of the table and always on the same page as the table (which is not always the case either). Finally, I would like the number always to appear in the outer margin when the two-page option is on. How may this be fixed?

• if you use \begin{tabular}[t] that resolves one of the issues- switching the margins is quite tricky and intricate- see Equation numbers in outer margins on a two-sided document for example – cmhughes Nov 14 '13 at 20:17
• You're not really typesetting the header in the margin, only at the left of the first word in the statement which means, under normal circumstances, in the margin; if the first word (here the tabular) is not at the left margin, also the header won't. – egreg Nov 14 '13 at 20:59
• cmhughes: Thanks, I'll try that out. egreg: Yes, I see your point. Unfortunately, I don't know enough about the ntheorem syntax to change it. – Gaussler Nov 14 '13 at 21:08

\centering is a bit too much here and won't be appropriate if there is following text. Use \begin{center}...\end{center] escaped with a preceeding \leavevmode:

\documentclass{memoir}

\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{amsmath,amssymb,geometry,graphicx,amstext}

\usepackage[amsmath]{ntheorem}

% margin theorem
\makeatletter
\newtheoremstyle{mymargin}%
{\item[\theorem@headerfont \llap{##1 ##2}| ##3\theorem@separator\hskip\labelsep]}%
\makeatother

% my example
\theoremstyle{mymargin}
\newtheorem{example}{Example}
\numberwithin{example}{chapter}
\begin{document}

\begin{example}
Ordinary example.
\end{example}

\begin{example}
\leavevmode\begin{center}
\begin{tabular}{cccc}
this & is & a & test\\
table & where  & text & goes
\end{tabular}
\end{center}
And now comes the discussion of the rest of the example.
\end{example}

\begin{example}
\begin{equation*}
x=y
\end{equation*}
\end{example}

\end{document}