4

Background

The following screenshot shows a Problem-Solution environment that is symmetric for twoside papers.

NOTE: The problem heading is on the right side for odd-numbered pages. For even-numbered pages, the heading is on the left side. Orphan-widow issues must also be taken into account such that there is no page break at the heading.

alt text

I already created this environment but it looks so complicated. Is there a package to do this out of the box?

Maximal Code Snippet (I cannot minimize my code anymore)

\documentclass[cmyk,10pt]{article}
\usepackage[a6paper,hmargin=1cm,vmargin=2cm]{geometry}
\usepackage{ifthen}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage{calc}

\newcounter{example}[section]

\renewcommand{\theexample}{\thesection.\arabic{example}}
\newenvironment{Example}[2][]%
{%
    \refstepcounter{example}%
    %------------------- Example Head -------------------
    \par\nointerlineskip\vspace{\baselineskip}\noindent%    
    \label{example:\theexample}\color{blue}%
    \ifthenelse{\isodd{\pageref{example:\theexample}}}%
    {%
        {\color{blue}\hrulefill}%
        \raisebox{\depth}{%
            \colorbox{blue}{%
                \color{white}Example \theexample\ifthenelse{\equal{#1}{}}{}{\ (#1)}%
            }%
        }%
    }
    {%  
        \raisebox{\depth}{%
            \colorbox{blue}{%
                \color{white}Example \theexample\ifthenelse{\equal{#1}{}}{}{\ (#1)}%
            }%
        }%
        {\color{blue}\hrulefill}%
    }%
    %------------------- Example Body -------------------
    \par\nopagebreak\nointerlineskip\vspace{0.5\baselineskip}\noindent{\footnotesize#2}%
    %------------------- Solution Head -------------------
    \par\nointerlineskip\vspace{0.5\baselineskip}\noindent%
    \colorbox{red}{\makebox[\linewidth-2\fboxsep]{\color{white}Solution}}%
    %------------------- Solution Body -------------------
    \par\nopagebreak\nointerlineskip\vspace{0.5\baselineskip}\noindent%
    \color{red}\footnotesize\ignorespaces%
}
{%
    %------------------- Solution Foot -------------------
    \par\nopagebreak\nointerlineskip\vspace{0.5\baselineskip}\noindent{\color{red}\hrulefill}%
    \par\nointerlineskip\vspace{\baselineskip}\noindent\ignorespacesafterend%
}

\usepackage{multido}

\begin{document}
\section{Without Title}

\begin{Example}
{%
    \lipsum[1]
}%
\lipsum[2]
\end{Example}


\section{With Title}
\begin{Example}[With Title]
{%
    \lipsum[1]
}%
\lipsum[2]
\end{Example}

\section{Looping}

\multido{\ix=1+1}{10}{%
\begin{Example}[\ix]
{%
    \lipsum[\ix]
}%
Einstein's formula
\begin{equation}
E\not=mc^2
\end{equation}
\lipsum[\ix]
\end{Example}\lipsum[\ix]}
\end{document}

Or if there is no package, could you give me a suggestion to do it in a better way?

  • should it completely without a pagebreak inside? – user2478 Jan 13 '11 at 12:26
  • @Herbert: Example and Solution headings must be followed by at least one line. – xport Jan 13 '11 at 13:55
3

the same with tabularx and ragged2e and some minor changes:

\vspace{0.5\baselineskip}==> \medskip \vspace{\baselineskip}==> \bigskip

\newenvironment{Example}[2][]%
{%------------------- Example Head -------------------
    \par\bigskip\noindent%    
    \refstepcounter{example}\label{example:\theexample}%%
    \ifthenelse{\isodd{\pageref{example:\theexample}}}
      {\color{blue}
       \begin{tabularx}{\linewidth}{@{} >{\RaggedLeft}X @{}}
       \colorbox{blue}{\color{white}Example \theexample\ifx\relax#1\relax\else~(#1)\fi}\\\hline
       \end{tabularx}}
      {\color{blue}
       \begin{tabularx}{\linewidth}{@{} >{\RaggedRight}X @{}}%
       \colorbox{blue}{\color{white}Example \theexample\ifx\relax#1\relax\else~(#1)\fi}\\\hline
       \end{tabularx}}%
    %------------------- Example Body -------------------
    \par\nopagebreak\nointerlineskip\medskip
       \noindent{\footnotesize#2}%
    %------------------- Solution Head -------------------
    \par\nointerlineskip\medskip\noindent%
    \colorbox{red}{\makebox[\linewidth-2\fboxsep]{\color{white}Solution}}%
    %------------------- Solution Body -------------------
    \par\nopagebreak%\nointerlineskip\vspace{0.5\baselineskip}\noindent%
    \color{red}\footnotesize\ignorespaces%
}
{%------------------- Solution Foot -------------------
    \par\nopagebreak\nointerlineskip\medskip
         \noindent{\color{red}\hrulefill}%
    \par\nointerlineskip\bigskip\noindent\ignorespacesafterend%
}
  • The first \noindent in Solution does not work the way I want. You commented out the line. :-) – xport Jan 13 '11 at 15:13
  • Why did you remove the first \nointerlineskip (after Example Head) ? – xport Jan 13 '11 at 15:30
  • @xport: by mistake ... – user2478 Jan 13 '11 at 15:39
  • Thanks for the modification. I like your modification: \ifx...\fi, \bigskip, and \medskip. I use them. – xport Jan 13 '11 at 15:44
0

The Memoir document class has \ifodd macro for testing whether you're on an odd page. You could do this to have the section headings display differently depending on whether you're on an odd or even page. See p. 338 of the memoir manual

The code is adapted from the changepage package, so I guess you could use that instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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