[Tex/LaTex] Fixed marginpars with gray background in KOMA-Script

backgroundscolorkoma-scriptmarginparpositioning

I am an newbie looking to achieve a sort of two column effect with my LaTeX report – I am using LaTeX KOMA-script as a base.

I want my margin to appear in a different background color, (say) gray, and main text background is (say) blue and I want to put images, text, graphs and equations in this margin. These contents are synched with particular paragraphs, i.e. I don't want them floating around, just fixed with respect to particular paragraphs in the main text.
So, how can I solve these two conditions?

Again,

  1. Different background color for main text and the margin contents. (See Similar questions panel in tex.stackexchange.com website)
  2. Make the contents of the margin par stay fixed with respect to the paragraphs of main text.

I have tried using a background image of two colors and stretching it across the entire page, but the effect is horrific… it does not align well with the main text and margins.

Any suggestions are really welcome.

Best Answer

The color problem may be solved using eso-pic. Figures at the margin but not moving up or down may be done using marginnote. But sometimes moving would be recommended, so you should have a look at marginfix too. Here an example with even some more colors than asked for:

\documentclass{scrbook}% You may use any other KOMA-Script class or
                       % e.g. a standard class.

\usepackage[english]{babel}
\usepackage{blindtext}% for demo only

\usepackage[demo]{graphicx}% remove option `demo' at real life
\usepackage[svgnames]{xcolor}% to have colors (see http://ctan.org/pkg/xcolor)
\usepackage{eso-pic}% put things into background (see http://ctan.org/pkg/eso-pic)
\usepackage{marginnote}% non floating margin notes (see http://ctan.org/pkg/marginnote)

\AddToShipoutPicture{% from package eso-pic: put something to the background
  % 1. Background
  \AtPageLowerLeft{% put it at the left bottom of the page
    \color{LightGrey}\rule{\LenToUnit\paperwidth}{\LenToUnit\paperheight}%
  }%
  % 2. Headline
  \AtTextUpperLeft{% put it at the left top of the text area
    \put(0,\LenToUnit{\dimexpr\headsep-\dp\strutbox\relax}){% move it up
      \color{BurlyWood}\rule{\LenToUnit\textwidth}{\LenToUnit\headheight}%
    }%
  }%
  % 3. Text area
  \AtTextLowerLeft{% put it at the left bottom of the text area
    \color{LightSkyBlue}\rule{\LenToUnit\textwidth}{\LenToUnit\textheight}%
  }%
  % 4. Bottom
  \AtTextLowerLeft{% put it at the left bottom of the text area
    \put(0,\LenToUnit{\dimexpr-\footskip-\dp\strutbox\relax}){% move it down
      % Note: While there is no \footheight, I'm using \headheight here.
      \color{BurlyWood}\rule{\LenToUnit\textwidth}{\LenToUnit\headheight}%
    }%
  }%
}

\setcapindent{0pt}% The margin is too small for hanging captions.

\begin{document}
\chapter{Test}
\blindtext\marginnote{%
  \begin{minipage}{\marginparwidth}
    \includegraphics[width=\linewidth,height=5\baselineskip]{Test}\\
    \captionof{figure}{\hspace{0pt}Example figure}
  \end{minipage}
}

\blindtext

\blinddocument
\end{document}

Maybe using option mpinclude would be a good idea, if you have a lot of material at the margin (see KOMA-Script manual scrguien.pdf) for more information about this option). In this case, increasing \marginparwidth may or may not be a good idea.

If you would like to use text area + margin area for a figure or equation, you may use something like

\begin{addmargin*}[0pt]{\dimexpr \marginparsep+\marginparwidth\relax}
\begin{equation}
  f(x)=x^2
\end{equation}
\end{addmargin*}

addmargin and addmargin* are KOMA-Script environments. You may find them at the KOMA-Script manual.

I've used \dimexpr for some calculations at my example. You may find this at the e-TeX manual. An alternative would be to use package calc. To avoid all the \LenToUnit you may simply use package picture. Package picture extends LaTeX's picture environment and you may use lengths instead of numbers at \put, \makebox, \framebox etc.