[Tex/LaTex] Example environment

environmentsexamples

I want an example environement that put Example 1.2 in the margin (bold) Text of example in the textwidth, Solution in the margin (italics) and the text of solution in the text width.

Best Answer

Solution 1

Here's something to get you going using the ntheorem package.

screenshot

% arara: pdflatex
\documentclass{article}
\usepackage{ntheorem}   % needed for theorems, examples- MUST load AFTER amsmath
\usepackage{lipsum}

\makeatletter
\newtheoremstyle{margincmh}%
{\item[\theorem@headerfont \llap{##1 ##2}]}%
{\item[\theorem@headerfont \llap{##1 ##2} -- ##3\theorem@separator\hskip\labelsep]}%

\newtheoremstyle{margincmhsoln}%
{\item[\theorem@headerfont \llap{##1}]}%
{\item[\theorem@headerfont \llap{##1} (##3): ]}%
\makeatother

% example
\theoremstyle{margincmh}
%\theorembodyfont{\itshape}}
\theorembodyfont{}
\theoremsymbol{}
\theoremprework{\medskip}
\theorempostwork{}
\theoremseparator{:}
\newtheorem{myexample}{example}

% solution
\theoremstyle{margincmhsoln}
\theorembodyfont{}
\theoremheaderfont{\itshape}
\theoremprework{\medskip}
\theoremseparator{}
\newtheorem{mysolution}{Solution}

\begin{document}

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

Solution 2

Alternatively, if you'd prefer to make the environment yourself without a theorem package, you can create it as a simple list- I have used the enumitem package to help with the margins

% arara: pdflatex
\documentclass{article}
\usepackage{lipsum}
\usepackage{enumitem}

\newcounter{myexample}
\newenvironment{myexample}{\refstepcounter{myexample}\itemize[leftmargin=0mm]\item[\bfseries example \themyexample]}{\enditemize}

\newenvironment{mysolution}{\itemize[leftmargin=0mm]\item[\itshape Solution]}{\enditemize}

\begin{document}

\begin{myexample}
\lipsum[1]  
\end{myexample}
\begin{mysolution}
  \lipsum[1]
\end{mysolution}
\end{document}
Related Question