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?

Rabarberski
  • 6,459
khademul
  • 627

2 Answers2

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

sdaau
  • 17,079
Gonzalo Medina
  • 505,128
  • 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 '12 at 00:28
  • @khademul: Hi. You need to run the example three times to see the resulting colored boxes; I'll add this to my answer. – Gonzalo Medina Mar 20 '12 at 00: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 '12 at 00: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. – Gonzalo Medina Mar 20 '12 at 01: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 abmmki@gmail.com I am online. – khademul Mar 20 '12 at 01:16
  • @khademul: yes, this site has chat rooms. Please wait and I will create a chat room for this discussion. – Gonzalo Medina Mar 20 '12 at 01:18
  • @Mediana: Let me know when Chat is created. Also, I realize now, in odd pages box should be in right side, but fro even pages it should be in Left side....as margin is different. # Also, as you said to create optional argument to place short text for the box would be great ... \boxText ?? thanks, – khademul Mar 20 '12 at 01:39
  • @khademul: my updated answer contains the code for alternating the position of the colored boxes. We'll continue in chat. – Gonzalo Medina Mar 20 '12 at 02:05
  • Many thanks Gonzalo Medina. That solved my problem and helped me a lot. Great work – khademul Mar 20 '12 at 17:58
  • @Gonzalo Medina: Great answer. It works really well when I use it with report document. When I insert your code in the preamble of my document it compiles smoothly, without any errors. However, when I insert the \ChapFrame command in one of my chapters (each chapter is in a different .tex file that is called by the main document using \include{} I get the following error message: ! Package pdgfkeys Error: I do not know the key '/tikz/align' and I am going to ignore it If I ignore the error message I can compile the final document. Also, is it possible to give particular names to each thu – EduardoSAS Apr 25 '12 at 23:54
  • @EduardoSAS: thank you for your kind words. I did some tests but I couln't reproduce the problem you mentioned with \include. I suggest you to open a fresh new follow-up question (linking to this one) and including a MWE example of your code allowing us to reproduce the problem mentioned. For the other issue, if you don't use the optional argument for \chapter, then the mandatory argument will be used both for the ToC and the thumb. – Gonzalo Medina Apr 28 '12 at 00:08
  • Is there a way to tweak this (or another way altogether) to get a \Large block of running text in the right margin that is rotated as shown here? – JohnD May 03 '12 at 02:32
  • @texasAUtiger Welcome to TeX.sx! Your question won't be seen by many people here, so it would be best to repost it as a fresh question. Follow-up questions like this are more than welcome! Please use the "Ask Question" link for your new question; there you can link to this question to provide the background. – Gonzalo Medina May 03 '12 at 02:36
  • Thank you for posting this solution! For some more details in a document with lots of chapters/sections; is there any way to automate colors in the \newcommand\boxcolor{}? Say I want to get colors from red to orange in steps for each chapter? – GaRyu Nov 11 '16 at 08:10
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
    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 '12 at 15:49