# Index and fancyhydr

I need to have different (imposed) pagestyles and thus am using fancyhydr. But I do not succeed to do the index correctly : it should begin on a certain pagestyle (say indextitle) and then have another style (say indexstyle). I do not know how to settle that : if I put the style before the command \printindex, it affects the last page before the index, which I do not want. I have tried to use the afterpage package which works fine for the inextitle page but does not solve the problem for the following pages. I have not found a way to settle the style in imakeidx directly.

Here is a MWE

 \documentclass[a4paper,11pt,twoside,openright]{book}
\usepackage{imakeidx}
\usepackage{fancyhdr}
\usepackage{afterpage}
\indexsetup{level=\chapter} %
\makeindex
\usepackage[rule=0.6pt, columnsep=20pt, justific=raggedright,     font=small]{idxlayout}

\fancypagestyle{indextitle}{%
\renewcommand{\footrulewidth}{0pt}
\fancyfoot{}
}
\fancypagestyle{index}{%
\renewcommand{\footrulewidth}{0pt}
\fancyfoot[C]{\thepage}
}

\fancypagestyle{paper}{%
\renewcommand{\footrulewidth}{0pt}
\fancyfoot[C]{\thepage}
}

\begin{document}
\pagestyle{paper}
A text with no sense\index{sense}

Just to make an index\index{index}

\afterpage{\thispagestyle{indextitle}}
\pagestyle{index}

\printindex

\end{document}


One sees that the main page has the wrong header.

I have tried several other things (clearpage first, but then the index begins two pages later). I would like also to change the way the title is done but it may be a different question. Thanks a lot.

• \afterpage{\pagestyle{indexstyle}\thispagestyle{indextitle}} – cfr Feb 28 '17 at 0:55
• Does \indexsetup{firstpagestyle=indextitle} help? – user31729 Feb 28 '17 at 0:56
• @cfr : Thank you, I had tried variants of this (and now these) and it works for the index, but the style of the last page before the index is then changed (as the indexstyle). This defeats me. – Catherine Feb 28 '17 at 17:58
• @Christian Hupfer : idem... – Catherine Feb 28 '17 at 17:58

The culprit is idxlayout that does strange things with the page style for the first page in an index.

You can remedy to the problem by issuing \thispagestyle{indextitle} before starting to typeset the index.

\documentclass[a4paper,11pt,twoside,openright]{book}
\usepackage{imakeidx}
\usepackage{fancyhdr}

\makeindex[columnsep=20pt,columnseprule]

\indexsetup{
othercode={%
\thispagestyle{indextitle}%
\setlength{\columnseprule}{0.6pt}
\small\raggedright
}
}

\fancypagestyle{indextitle}{%
\renewcommand{\footrulewidth}{0pt}%
\fancyhf{}%
}
\fancypagestyle{index}{%
\renewcommand{\footrulewidth}{0pt}%
\fancyhf{}%
\fancyfoot[C]{\thepage}%
}

\fancypagestyle{paper}{%
\renewcommand{\footrulewidth}{0pt}%
\fancyhf{}%
\fancyfoot[C]{\thepage}%
}

\pagestyle{paper}

\begin{document}

A text with no sense\index{sense}

Just to make an index\index{index}

\count255=0
\expandafter\index\expandafter{\romannumeral\count255 }
\repeat

\cleardoublepage
\pagestyle{index}
\printindex

\end{document}


• Argh! Perfect, it works. Thank you so much ! I have to read more carefully idxlayout because the solution defeats me. – Catherine Feb 28 '17 at 18:00
• @Oups, I should have said: it almost works. There is a pagenumber in the middle of the first page of the index and it should not be here in my style. – Catherine Mar 6 '17 at 17:28
• @Catherine As I said, the problem is with idxlayout. Updated avoiding it. – egreg Mar 6 '17 at 18:31
• Perfect! It is sad because idxlayout seemed easy to use. I have found a possible solution in another question, changing plain to empty (\let\ps@plain\ps@empty): it works with idxlayout, but of course may lead to other problems if one needs a real "plain format". Your solution is the best, thanks. – Catherine Mar 7 '17 at 14:40