7

I'd like the margins in the example environment in the code below to be slightly indented. What is the easiest way of achieving this? It's supposed to apply to all example environments.

In the example below, just the heading is indented, but I'd like the entire environment to be (slightly) indented from both sides.

\documentclass{amsart}

\newtheoremstyle{ugh}
{3pt}% hSpace above
{3pt}% hSpace below
{}% hBody font
{1cm}% hIndent amount
{\bfseries}% hTheorem head font
{:}% hPunctuation after theorem head
{.5em}% hSpace after theorem head
{}% hTheorem head spec (can be left empty, meaning ‘normal’)

\theoremstyle{ugh}
\newtheorem{example}{Example}
\usepackage{lipsum}


\begin{document}

\lipsum[1-2]

\begin{example}
% I want this automatically indented a bit at both sides for any example 
\lipsum[3-4] \qed
\end{example}

\lipsum[5-6]

\end{document}
gsamaras
  • 1,443
JPi
  • 13,595
  • I tried adjusting the margins inside the body argument of the \theoremstyle command using \newgeometry of the geometry package, which adjust the right margin, but not the left margin. – JPi Mar 30 '15 at 14:24
  • adjustwidth of changepages gets closer but doesn't indent the displayed equations. – JPi Mar 30 '15 at 14:42
  • may be you want to add this comment to the question – touhami Apr 01 '15 at 20:24

2 Answers2

7

I'd locally patch the \@thm command in the example environment. Of course you won't be able to state theorems correctly inside the environment, but I don't think you would.

\documentclass{amsart}

\usepackage{etoolbox}
\usepackage{lipsum}

\theoremstyle{definition}
\newtheorem{innerexample}{Example}

\makeatletter
\patchcmd{\endinnerexample}{\endtrivlist}{\endlist}{}{}
\newenvironment{example}
 {\patchcmd{\@thm}{\trivlist}{\list{}{\leftmargin=3em \rightmargin=3em}}{}{}%
  \innerexample\pushQED{\qed}}
 {\popQED\endinnerexample}
\makeatother

\begin{document}

\lipsum*[1]
\[
e^x=\sum_{k\ge0}\frac{x^k}{k!}
\]
\lipsum[2]

\begin{example}
\lipsum*[3]
\[
e^x=\sum_{k\ge0}\frac{x^k}{k!}
\]
\lipsum*[4]
\end{example}

\lipsum[5-6]

\end{document}

The idea is to patch \@thm to do \list instead of \trivlist, so we're able to use \leftmargin and \rightmargin. The patch happens inside the example environment, so it will disappear when the environment ends.

I also added the automatic QED at the end. Use \qedhere if the example ends with a list or a display, as usual.

enter image description here

egreg
  • 1,121,712
5

Here is a solution based on changemargin environment

Edit: This does not answer the question of displayed equations

\documentclass{amsart}

\newtheoremstyle{ugh}
{3pt}% hSpace above
{3pt}% hSpace below
{}% hBody font
{}% hIndent amount
{\bfseries}% hTheorem head font
{:}% hPunctuation after theorem head
{.5em}% hSpace after theorem head
{}% hTheorem head spec (can be left empty, meaning ‘normal’)

\theoremstyle{ugh}
\newtheorem{eexample}{Example}

\newenvironment{changemargin}{%
\begin{list}{}{%
\setlength{\topsep}{0pt}%
\setlength{\leftmargin}{1cm}%    
\setlength{\rightmargin}{1cm}%  
\setlength{\listparindent}{\parindent}%
\setlength{\itemindent}{\parindent}%
\setlength{\parsep}{\parskip}%
}%
\item[]}{\end{list}}

\newenvironment{example}{%
\begin{changemargin}\vskip-\baselineskip
\begin{eexample}}{%
\end{eexample}\end{changemargin}}
\usepackage{lipsum}


\begin{document}

\lipsum[1-2]

\begin{example}
% I want this automatically indented a bit at both sides for any example 
\lipsum[3-4] \qed
\end{example}

\lipsum[5-6]

\end{document}

If

\documentclass{amsart}

replaced by

\documentclass[leqno]{article}
\usepackage{amsmath,amsthm}

it will be correct.

Edit: Complete solution with amsart This not the proper way but it does the job

\documentclass{amsart}

\newtheoremstyle{ugh}
{3pt}% hSpace above
{3pt}% hSpace below
{}% hBody font
{}% hIndent amount
{\bfseries}% hTheorem head font
{:}% hPunctuation after theorem head
{.5em}% hSpace after theorem head
{}% hTheorem head spec (can be left empty, meaning ‘normal’)

\theoremstyle{ugh}
\newtheorem{eexample}{Example}

\newenvironment{changemargin}{%
\begin{list}{}{%
\setlength{\topsep}{0pt}%
\setlength{\leftmargin}{1cm}%    
\setlength{\rightmargin}{1cm}%  
\setlength{\listparindent}{\parindent}%
\setlength{\itemindent}{\parindent}%
\setlength{\parsep}{\parskip}%
}%
\item[]%
}{\end{list}}

\newenvironment{example}{%
\begin{changemargin}%
\eeq\vskip-\baselineskip%
\begin{eexample}}{%
\end{eexample}\end{changemargin}}
\usepackage{lipsum}

\makeatletter\def\eeq{%                                     this may be bad idea
\def\maketag@@@##1{\makebox[1.5cm][r]{\m@th\normalfont##1}}}
\makeatother

\begin{document}


\lipsum[1-2]

\begin{example}
% I want this automatically indented a bit at both sides for any example 
\lipsum[3-4]

\begin{equation}
    x+y
\end{equation}
\qed
\end{example}

\lipsum[5-6]

\end{document}
touhami
  • 19,520