Page LayoutFlushleft and flushright in margins (odd an even pages)?

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
imajcena
Posts: 5
Joined: Fri Oct 03, 2008 9:29 am

Flushleft and flushright in margins (odd an even pages)?

Post by imajcena »

I'm writing a book and I am putting some images in the margin. I don't like the default position of the image in the margin. It should be flushed left on the odd pages and flushed right on the right pages, so I want it right next to the text.

There is a problem with the code below, it doesn't work if the environment just starts on the new page (see the attached file, the position of the first \bigoplus on page 2 should be the same as the position of the second!). How could I prevent that? Help is highly appreciated.
Btw, I have no other margin types so this could even be set as a general default for the margins, not only in "dokaz" environment.

Code: Select all

Code, edit and compile here:
\documentclass[b5paper,11pt]{book}
\usepackage[frame,a4,center]{crop}
\usepackage[latin2]{inputenc}
\usepackage{amssymb}
\usepackage{lipsum}
\usepackage{ifthen}%the position depends on whether the page is odd or even
\newenvironment{dokaz}{\noindent{\sc Dokaz.{}} \marginpar{
\ifthenelse{\isodd{\thepage}}
{\flushleft$\bigoplus$}
{\flushright$\bigoplus$}
}}
{\hspace*{\fill}\nolinebreak[1]\hspace*{\fill}$\square$\vskip.3cm}
\begin{document}
\lipsum[1]
\begin{dokaz}
\lipsum[1-2]
\end{dokaz}
\begin{dokaz}
\lipsum[3]
\end{dokaz}
\begin{dokaz}
\lipsum[4]
\end{dokaz}
\end{document}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Attachments
Question.pdf
(19.43 KiB) Downloaded 357 times
Last edited by imajcena on Tue Aug 18, 2009 9:37 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

imajcena
Posts: 5
Joined: Fri Oct 03, 2008 9:29 am

Re: Flushleft and flushright in margins (odd an even pages)?

Post by imajcena »

Doesn't anyone know the answer to this? Or perhaps I wasn't clear about where the problem is?
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Flushleft and flushright in margins (odd an even pages)?

Post by gmedina »

imajcena wrote:Doesn't anyone know the answer to this? Or perhaps I wasn't clear about where the problem is?
Yes, I know the answer, and no, you were clear enough.

The problem comes from the asynchronous way in which LaTeX builds pages; this means that, in your code, the condition of the iftenelse command is evaluated while a page is built, and later on LaTeX's routines decided to move the relevant part of text to the next page, producing undesired results.

This is a typical example to show that it's not a good idea to use simply \thepage when defining commands like yours.

The solution (also typical (see The LaTeX Companion, for example)) is to use a combination of \label and \pageref commands:

Code: Select all

Code, edit and compile here:
\documentclass[b5paper,11pt]{book}
\usepackage[frame,a4,center]{crop}
\usepackage[latin2]{inputenc}
\usepackage{amssymb}
\usepackage{lipsum}
\usepackage{ifthen}%the position depends on whether the page is odd or even
\newcounter{pl}
\newenvironment{dokaz}
{\noindent{\scshape Dokaz.{}}%
\stepcounter{pl}\label{pl-\thepl}%
\marginpar{%
\ifthenelse{\isodd{\pageref{pl-\thepl}}}%
{\flushleft$\bigoplus$}%
{\flushright$\bigoplus$}}}
{\hspace*{\fill}\nolinebreak[1]\hspace*{\fill}$\square$\vskip.3cm}
\begin{document}
\lipsum[1]
\begin{dokaz}
\lipsum[1-2]
\end{dokaz}
\begin{dokaz}
\lipsum[3]
\end{dokaz}
\begin{dokaz}
\lipsum[4]
\end{dokaz}
\lipsum[1]
\begin{dokaz}
\lipsum[1-2]
\end{dokaz}
\begin{dokaz}
\lipsum[3]
\end{dokaz}
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1,1,2,3,5,8,13,21,34,55,89,144,233,...
imajcena
Posts: 5
Joined: Fri Oct 03, 2008 9:29 am

Re: Flushleft and flushright in margins (odd an even pages)?

Post by imajcena »

Thank you! :D
Post Reply