# newgeometry makes content run to next page

Thank you all from the beginning for your help in this. I'm completely new to LaTeX as I'm writing my bachelor thesis, so I'm sure I've made lots of mistakes...

I'm using the geometry package because I need to modify the text width at a certain point in my thesis, and to do so I wanted to use \newgeometry inside a custom environment.

The problem is that the part of the text that should be influenced by the new geometry runs all the way down to the next page.

I have read this Using \restoregeometry in environment, next page runs off the page bottom question and, as you can see, I also used the \aftergroup comman, but still the issue persists.

My custom environment is:

\newenvironment{esempio}[1]%
{
\vspace{1.5ex}
\noindent
\underline{#1}
\nopagebreak
\newgeometry{textwidth=\textwidth}
%\leftskip=1cm
%\rightskip\leftskip
}
{
\par
\aftergroup\restoregeometry
}


And i use it like:

\begin{esempio}{$k=2$}
Il Poligono di Controllo \e formato da $\{P_0, P_1, P_2\}$.
La curva di Bezier \e definita come $$P_0^2(t) = (1-t)P_0^1(t) + tP_1^1(t)$$
\end{esempio}


I get the " K=2 " underlined in the correct position, but the following text is all down to the next page.

My aim would be to have an environment that lets me format a body of text like this:

bla bla bla bla bla bla bla bla bla bla bla bla
K=2
bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla


As a sidenote, you can see I also tried with the \leftskip.

It gets the job done very well with the normal text, but most of the times I'll also include a picture on the side and I want the text to wrap around it, and with the \leftskip it leaves the space on the left of the image and so the text does not wrap nicely.

@ egreg: thank you for the warm welcome and the tips :)

@ Gonzalo: thanks, your code works like a charm for the text! However, as i said in the original question, a very common situation would be to insert a figure inside that text and i'd like the text to be wrapped around the picture.

However, this causes a couple of warnings and bad boxes in the compilation process and the figure gets moved down and placed not after the "esempio" box, but after even the next paragraph, and it also creates huge blank spaces... quite weird.

Below the MWE (rettaBezier is a picture 4cmx4cm square)

\documentclass[a4paper,11pt,italian]{book}

\usepackage[italian]{babel}
\usepackage{graphicx, wrapfig, subfig}
\usepackage{changepage}
\usepackage{calc}
\usepackage{amsmath, amsthm, amssymb, mathrsfs, setspace}
\usepackage{mycommands}
\usepackage{indentfirst}
\usepackage[format=hang,font=footnotesize,labelfont=bf]{caption}
\graphicspath{{./images/}{./matlab/}}
\usepackage{lipsum}

\begin{document}
\lipsum[2]
\begin{esempio}{$k=1$}
\begin{wrapfigure}{o}[1cm]{0cm}
\centering
\includegraphics{rettaBezier}
\caption{Retta ottenuta con $P_0 = [0, 0] , P_1 = [1, 1]$}
\label{rettaBezier}
\end{wrapfigure}
\lipsum[2]
\end{esempio}

\lipsum[2]

\end{document}

-
Welcome to TeX.sx! It's not necessary to sign your questions (as there is already a box with your username below it) or to begin them with a greeting. –  egreg Apr 26 '12 at 21:29
\newgeometry does its job only on whole pages. What you probably need is the quotation environment or something similar. But this conflicts with wrapfigure, I'm afraid. –  egreg Apr 26 '12 at 21:31
@Filo: I've updated my answer with a possible solution using wrapfigure. –  Gonzalo Medina Apr 27 '12 at 13:14

As egreg mentions in a comment, \newgeometry affects whole pages. To temporarily change the text area width, you can use the adjustwidth command from the changepage package:

\documentclass{article}
\usepackage{changepage}
\usepackage{lipsum}

\begin{document}

\lipsum[2]
\lipsum[2]
\lipsum[2]

\end{document}


Your esempio environment then could look like in the following example (change the lengths according to your needs):

\documentclass{article}
\usepackage{changepage}
\usepackage{soul}
\usepackage{lipsum}

\newenvironment{esempio}[1]%
{\vspace{1.5ex}
\rlap{\ul{#1}}\par\nobreak
}

\begin{document}

\begin{esempio}{$k=2$}
\lipsum[2]
\end{esempio}
\lipsum[2]

\end{document}


Without additional packages, you can use a list:

\documentclass{article}
\usepackage{soul}
\usepackage{lipsum}

\newenvironment{esempio}[1]%
{\vspace{1.5ex}
\list{}{\setlength\leftmargin{1cm}\setlength\rightmargin{1cm}}\item\relax
\rlap{\ul{#1}}\nobreak
}
{\endlist}

\begin{document}

\lipsum[2]
\begin{esempio}{$k=2$}
\lipsum[2]
\end{esempio}
\lipsum[2]

\end{document}


To be able to use wrapfigure inside the esempio environment, one option is to use a minipage to enclose the figure and its wrapping text (of course, now the material inside the minipage won't admit page breaks):

\documentclass{article}
\usepackage{soul}
\usepackage{wrapfig}
\usepackage{lipsum}

\newenvironment{esempio}[1]%
{\vspace{1.5ex}
\list{}{\setlength\leftmargin{1cm}\setlength\rightmargin{1cm}}\item\relax
\rlap{\ul{#1}}\nobreak
}
{\endlist}

\begin{document}

\lipsum[2]
\begin{esempio}{$k=2$}
\begin{minipage}[t]{\linewidth}
\begin{wrapfigure}{r}{5cm}
\centering
\rule{4cm}{3cm}
\caption{a test figure}
\label{fig:test}
\end{wrapfigure}
\lipsum[2]
\end{minipage}
\end{esempio}
\lipsum[2]

\end{document}


And the result:

-

Thank you Gonzalo for your suggestion. However, i hate the minipage and avoid any use of it as much as i can, therefore i came up with this solution

\newenvironment{esempio}[3]%
{
\vspace{1.5ex}
\rlap{\underline{#1}}
\par
\setlength{\parindent}{0cm}
\nopagebreak
\leftskip=#2cm
\rightskip=#3cm
}
{
\par
}


It might not be the most elegant code ever, but it gets the job done and fits well in my project expecially because it allows pagebreaks in the middle of the "esempio".

-