Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am writing a paper about phonetics that contains lots of examples, usually organised in tables. What I would like is to have these examples enumerated in the margins. Specifically, I need an environment called example, so that whatever I write

\begin{example}
    some examples
\end{example}

LaTeX prints this text along with a margin note saying, e.g., "Example 2.7". It is supposed to work a bit like in this book.

I did try with the following code:

\newenvironment{example}
{
    \marginpar{\begin{center}Examples\\ \refstepcounter{examples}\theexamples\end{center}}
    \medskip\begin{center}
}
{
    \end{center}\medskip
}

but the numbers are not always aligned correctly with the examples in the margins.

Any good ideas about how to make this? :-)

UPDATE: On request from cmhughes, I now add my entire preamble so that he may see the problem. :-)

\documentclass[a4paper]{memoir}

%Memoir settings
\setlrmarginsandblock{*}{3.5cm}{0.75}
\setulmarginsandblock{3cm}{*}{1.2}
\checkandfixthelayout[nearest]

\usepackage{amsmath,amssymb,geometry,graphicx,amstext,enumerate,multirow,color,multicol,comment,xcolor}
\usepackage[hidelinks]{hyperref}
\usepackage{cleveref}
\usepackage[ansinew]{inputenc}
\usepackage{wasysym}
\usepackage{tipa,vowel,textcomp}


\chapterstyle{veelo}
\pagestyle{headings}

\usepackage[amsmath]{ntheorem}      % for theorem-like environments

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

% my example
\theoremstyle{mymargin}
\theorembodyfont{}      % customize these to suit your tastes
\theoremsymbol{}
\theoremprework{}
\theorempostwork{}
\theoremseparator{}
\newtheorem{example}{example}
\numberwithin{example}{section}
%\usepackage{subcaption}

\begin{document}

However, using this preamble resulted in the example enumeration looking like this:

enter image description here

See, the number appears to the right of the margin.

Do you have an idea what the problem may be? :-)

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

There's a few ways that you can do this. Personally, I favour the ntheorem package

screenshot

I have loaded the geometry package with showframe=true just to highlight the page boundary, and lipsum just for sample text.

\documentclass{report}

\usepackage[showframe=true]{geometry}
\usepackage{lipsum}                 % for sample text
\usepackage{amsmath}
\usepackage[amsmath]{ntheorem}      % for theorem-like environments

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

% my example
\theoremstyle{mymargin}
\theorembodyfont{}      % customize these to suit your tastes
\theoremsymbol{}
\theoremprework{}
\theorempostwork{}
\theoremseparator{}
\newtheorem{myexample}{example}
\numberwithin{myexample}{section}

\begin{document}

\begin{myexample}
\lipsum[2]
\end{myexample}
\end{document}

You could achieve the same output by using a simple list, using the enumitem package, but I think a theorem package is most appropriate here.

share|improve this answer
    
Thanks for the answer :-). There are just two problems, which I'm sure you'll be able to solve. :-) First of all, the number is vertically aligned by the middle of the text within the environment rather than at the top. And second of all, if the text in the environment is centered, then the number appears to the right of the margin border. Is there an easy solution to this? :-) –  Gaussler Mar 5 '13 at 19:07
    
@Gaussler I'm afraid I don't understand- my code aligns example 0.0.1 at the top of the environment. I don't really understand the centering issue- you want the body of the text to be centered? –  cmhughes Mar 5 '13 at 22:34
    
Oh, sorry the description wasn't accurate enough. I have made this screenshot to show the issue. See, the vertical alignment looks strange, and it appears outside the margin. I copy-pasted what you wrote, so I'm not sure what's wrong. –  Gaussler Mar 5 '13 at 23:05
    
@Gaussler are you sure you pasted my code as is? :) For me it gives the output I posted in my answer- if not, please make it into a complete MWE and add it to your questino –  cmhughes Mar 6 '13 at 1:46
    
I have now done just that. :-) –  Gaussler Mar 7 '13 at 21:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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