31

I am using the book document class. I would like to have Chapter name (Interduction, Results, Discussion, ...conclusion...) to appear in every page, in the right side small vertical (color filled) box (box size, let say 3cm). So, instead of chapter name in the header or footer, I want it to keep it in right side color filled box ..... sometime we see the style in Dairy, and also in many books as well.

For the first chapter, this right side box will appear at the upper top right margin....then in next chapter the box will be little lower and will have different color.

Is there anyone who can help me on this?

1
  • 2
    You might want to have a look here in "Anhang-B". The file is called chapterthumb.sty.
    – cgnieder
    Mar 19, 2012 at 23:58

2 Answers 2

42

Here's one possible solution using the background package; the \BoxColor command uses a simple \ifcase to select the colors to be used; use \chapFrame after each \chapter command (this can be automated). The code needs at three runs to stabilize.

Based on the comments, the position of the colored box has to alternate for odd and even pages; here's the necessary modification:

Improved version (background version > 2.0)

This version uses the new syntax for background introduced in version 2.0 and suppresses the need for the ifthen package:

\documentclass{book}
\usepackage[
  scale=1,
  angle=0,
  opacity=1,
  contents={}
]{background}
\usetikzlibrary{calc}
\usepackage{lipsum}

\pagestyle{plain}

% auxiliary counter
\newcounter{chapshift}
\addtocounter{chapshift}{-1}

% the list of colors to be used (add more if needed)
\newcommand\BoxColor{%
  \ifcase\thechapshift blue!30\or red!30\or olive!30\or magenta!30\else yellow!30\fi}

% the main command; the mandatory argument sets the color of the vertical box
\newcommand\ChapFrame{%
  \AddEverypageHook{%
    \ifodd\value{page}
      \backgroundsetup{contents={%
        \begin{tikzpicture}[overlay,remember picture]
          \node[
            fill=\BoxColor,
            inner sep=0pt,
            rectangle,
            text width=2cm,
            text height=4cm,
            align=center,
            anchor=north east
          ] 
          at ($ (current page.north east) + (-0cm,-2*\thechapshift cm) $) 
          {\rotatebox{90}{\hspace*{.3cm}\parbox[c][1.5cm][t]{3.4cm}{%
              \raggedright\textcolor{black}{\scshape\leftmark}}}};
        \end{tikzpicture}%
      }%
    }  
    \else
      \backgroundsetup{contents={%
        \begin{tikzpicture}[overlay,remember picture]
        \node[
          fill=\BoxColor,
          inner sep=0pt,
          rectangle,
          text width=2cm,
          text height=4cm,
          align=center,
          anchor=north west
        ] 
        at ($ (current page.north west) + (-0cm,-2*\thechapshift cm) $) 
        {\rotatebox{90}{\hspace*{.3cm}\parbox[c][1.5cm][t]{3.4cm}{%
            \raggedright\textcolor{black}{\scshape\leftmark}}}};
        \end{tikzpicture}%
      }%
    }  
    \fi
  \BgMaterial}%
  \stepcounter{chapshift}%
}

% redefinition of \chaptermark to contain only the title
\renewcommand\chaptermark[1]{\markboth{\thechapter.~#1}{}} 

\begin{document}

\chapter[intro]{Introduction}
\ChapFrame
\lipsum[1-7]

\chapter{Results}
\ChapFrame
\lipsum[1-7]

\chapter{Discussion}
\ChapFrame
\lipsum[1-7]

\end{document}

The result:

enter image description here

First version (background version < 2.0)

This was the first version of the code; it still can be used with the newest version of the background package:

\documentclass{book}
\usepackage{background}
\usetikzlibrary{calc}
\usepackage{ifthen}
\usepackage{lipsum}

\pagestyle{plain}

% background common settings
\SetBgScale{1}
\SetBgAngle{0}
\SetBgOpacity{1}
\SetBgContents{}

% auxiliary counter
\newcounter{chapshift}
\addtocounter{chapshift}{-1}

% the list of colors to be used (add more if needed)
\newcommand\BoxColor{%
  \ifcase\thechapshift blue!30\or red!30\or olive!30\or magenta!30\else yellow!30\fi}

% the main command; the mandatory argument sets the color of the vertical box
\makeatletter
\newcommand\ChapFrame{%
\AddEverypageHook{%
\ifthenelse{\isodd{\thepage}}
{\SetBgContents{%
  \begin{tikzpicture}[overlay,remember picture]
  \node[fill=\BoxColor,inner sep=0pt,rectangle,text width=2cm,
    text height=4cm,align=center,anchor=north east] 
  at ($ (current page.north east) + (-0cm,-2*\thechapshift cm) $) 
  {\rotatebox{90}{\hspace*{.3cm}\parbox[c][1.5cm][t]{3.4cm}{%
    \raggedright\textcolor{black}{\scshape\leftmark}}}};
  \end{tikzpicture}}%
}
{\SetBgContents{%
  \begin{tikzpicture}[overlay,remember picture]
  \node[fill=\BoxColor,inner sep=0pt,rectangle,text width=2cm,
    text height=4cm,align=center,anchor=north west] 
  at ($ (current page.north west) + (-0cm,-2*\thechapshift cm) $) 
  {\rotatebox{90}{\hspace*{.3cm}\parbox[c][1.5cm][t]{3.4cm}{%
    \raggedright\textcolor{black}{\scshape\leftmark}}}};
  \end{tikzpicture}}
}
\bg@material}%
  \stepcounter{chapshift}
}
\makeatother

% redefinition of \chaptermark to contain only the title
\renewcommand\chaptermark[1]{\markboth{\thechapter.~#1}{}} 

\begin{document}

\chapter[intro]{Introduction}
\ChapFrame
\lipsum[1-7]

\chapter{Results}
\ChapFrame
\lipsum[1-7]

\chapter{Discussion}
\ChapFrame
\lipsum[1-7]

\end{document}

An image of the resulting document showing the first pages:

enter image description here

14
  • Hi Gonzalo Medina, Many Thanks for very prompt reply. I copied the code and run. Page generated without any problem/error, but surprisingly it did not generated the colored right side margin box that actuallt I was looking for. I am using Mac OS X and TexShop. Don't know what is the problem. Thanks again
    – khademul
    Mar 20, 2012 at 0:28
  • @khademul: Hi. You need to run the example three times to see the resulting colored boxes; I'll add this to my answer. Mar 20, 2012 at 0:32
  • @Gonzalez Mediana: Thanks, I can see now. However, there are three more concerns if you can help. (1) Its writing word in red color "draft" in the middle of the preceding pages (eg. tilte page, contents) where I an not using this box to appear. (2) I am using .sty file from university. Probably that is suppressing the box to go to the exact end of the page. It is going to the ~3cm before. (3) Also, When I have bigger chapter name, it is going out of the box. Can box size be adjusted with text size. I am using \chaptermark{} to make title short though. But in some cases text still big. Thanks
    – khademul
    Mar 20, 2012 at 0:53
  • @khademul: for (1), add \SetBgContents{} in the preamble (I've added it to my code). (2) should not be happening; perhaps we can discuss this with more detail in chat? As for (3), perhaps changing the value is not a good idea I think it would be better to use the optional argument of \chapter to place a shorter version of the title into the box; is that a valid option for you? If not, I could adjust my code. Anyway, I think we might continue this discussion in chat if that's OK with you. Mar 20, 2012 at 1:02
  • Mediana. Thanks again. Yes, we better discuss this in Chat. I am not sure if this forum has chat option. I just joined this forum. However, my gmail id is [email protected] I am online.
    – khademul
    Mar 20, 2012 at 1:16
18

This answer is a supplement to Gonzalo's excellent answer just to show another method. Here fancyhdr is used instead of background. I have adopted the color scheme as given by Gonzalo (Thanks to him).

% ----------------------------------------------------------------
% Book Class (This is a LaTeX2e document)  ***********************
% ----------------------------------------------------------------
\documentclass[10pt,x11names,svgnames,twoside]{book}
\usepackage{tikz}
\usetikzlibrary{shapes.misc,calc}
\usepackage{lipsum,fancyhdr}
\usepackage[a4paper,left=1.2in,right=1in,top=1in,bottom=1in,headheight=\baselineskip,
   headsep=9mm,footskip=13mm,showframe,]{geometry}
% ----------------------------------------------------------------
\newcommand\MyColor{%                                                               % This line is  from Gonzalo's answer
  \ifcase\thechapter blue!30\or red!30\or olive!30\or magenta!30\else yellow!30\fi} % This line is  from Gonzalo's answer
%------------------------------------------
\fancypagestyle{plain}{%
  %% Clear all headers and footers
  \fancyhf{}
    %% Right headers on odd pages
  \fancyhead[RO]{%
  \rotatebox{90}{
      \begin{tikzpicture}[overlay,remember picture]
      \node[fill=\MyColor,text=white,
      font=\footnotesize,
      inner ysep=12pt, inner xsep=20pt,
      rounded rectangle,anchor=east,
      xshift=-0mm,yshift=-32mm,text width=3cm, text height=0.4cm]
      at ($ (current page.north east) + (2.9cm,-0cm) + (-4*\thechapter cm,0cm) $)
      {\sffamily\itshape\small\nouppercase{\leftmark}};
    \end{tikzpicture}
        }
        }
        %% Left headers on even pages
\fancyhead[LE]{%
  \rotatebox{90}{
      \begin{tikzpicture}[overlay,remember picture]
      \node[fill=\MyColor,text=white,
      font=\footnotesize,
      inner ysep=12pt, inner xsep=20pt,
      rounded rectangle,anchor=east,
      xshift=41mm,yshift=-2mm,text width=3cm, text height=0.4cm]
      at ($ (current page.north west) + (4cm,0cm) + (-4*\thechapter cm,0cm) $)
      {\sffamily\itshape\small\nouppercase{\leftmark}};
      \end{tikzpicture}
        }
        }
  \renewcommand{\headrulewidth}{0pt}
  \renewcommand{\footrulewidth}{0pt}
  \fancyfoot[R]{\thepage}
}
% ----------------------------------------------------------------
\pagestyle{plain}
\renewcommand\chaptermark[1]{\markboth{\thechapter.~#1}{}} 
\begin{document}
%
\chapter{First chapter}
\lipsum[1-16]
\cleardoublepage
\chapter{Second chapter}
\lipsum[1-16]
\chapter{Third chapter}
\lipsum[1-16]
\chapter{Fourth chapter}
\lipsum[1-16]
%
\end{document} 

enter image description here

enter image description here

enter image description here

1
  • 1
    Thanks, nice example too. Text is in the center of the box and size changes with text length. However, It conflict with 'geometry.sty' I am using. Using b5 page. Commenting that portion of the code leads the 50% box out of the page. I have many chapters, after a certain point all boxes go bottom and out of the page. Also, I have \part before chapter (like \part{Results}; \chapter{Result1} \chapter{Result2} ) in that case in appropriate box color in\part. Box possible for \part ? Box nicely appears only odd pages as desired, but in some cases in even, may be I commented 'geometry'?
    – khademul
    Mar 20, 2012 at 15:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .