Last Edit Answer
With this answer you can select the depth of sectioning that appear
in fancyhead using \MaxLevel variable. (For example if you use number 2,
only sections will be shown on fancy header)
Also, you have the extra option to use a deferent fancy head content from the name of the chapter or section etc by using starred sectioning with optional argument (something that doesn't allowed in ordinary latex)
An extra feature that may not be desired is that labels are added automaticaly
to every chapter, section or subsection as {c:1} for chapter 1 (real first)
{s:2} for second section {sss:3} for third subsubsection etc.
\documentclass[11pt,a4paper,twoside,titlepage,openany]{book}
\usepackage{lipsum}
\usepackage[T1]{fontenc}
\usepackage{amsfonts}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{lmodern}
\usepackage{sectsty}
\usepackage{bophook}
\usepackage{refcount}
\usepackage{geometry}
\geometry{
left=35mm,
right=30mm,
top=27mm,
bottom=30mm
}
\usepackage{fancyhdr}
\newcounter{realchapter}
\newcounter{realsection}
\newcounter{realsubsection}
\newcounter{realsubsubsection}
\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}
\newcount\Level
\def\MaxLevel{3} % Change this to show only desired depth on fancy header
\def\PrevSec{}
\makeatletter
\let\oldchapter=\chapter
\def\chapter{%
\xdef\ind{c:0}
\stepcounter{realchapter}
\xdef\PrevSec{\curfmark}
\global\Level=1%
\@ifstar{\@Starred}{\@nonStarred}%
}
\def\@Starred{%
\@ifnextchar[%
{\@StarredWith}%
{\@StarredWithout}%
}
\def\@StarredWith[#1]#2{%
\xdef\ind{c:\therealchapter}
\oldchapter*{#2}\index{\ind}\label{\ind}\def\Chaptername{#1}%
}
\def\@StarredWithout#1{%
\xdef\ind{c:\therealchapter}
\oldchapter*{#1}\index{\ind}\label{\ind}\def\Chaptername{#1}%
}
\def\@nonStarred{%
\@ifnextchar[%
{\@nonStarredWith}%
{\@nonStarredWithout}%
}
\def\@nonStarredWith[#1]#2{%
\xdef\ind{c:\therealchapter}
\oldchapter[#1]{#2}\index{\ind}\label{\ind}\def\Chaptername{#1}%
}
\def\@nonStarredWithout#1{%
\xdef\ind{c:\therealchapter}
\oldchapter{#1}\index{\ind}\label{\ind}\def\Chaptername{#1}%
}
\let\oldsection=\section
\def\section{%
\stepcounter{realsection}
\xdef\PrevSec{\curfmark}
\global\Level=2%
\@ifstar{\@StarredS}{\@nonStarredS}%
}
\def\@StarredS{%
\@ifnextchar[%
{\@StarredWithS}%
{\@StarredWithoutS}%
}
\def\@StarredWithS[#1]#2{%
\xdef\ind{s:\therealsection}
\oldsection*{#2}\index{\ind}\label{\ind}\def\Sectionname{#1}% Here you can use starred chapter, section etc with optional argument for chaptermark or sectionmark etc
}
\def\@StarredWithoutS#1{%
\xdef\ind{s:\therealsection}
\oldsection*{#1}\index{\ind}\label{\ind}\def\Sectionname{#1}%
}
\def\@nonStarredS{%
\@ifnextchar[%
{\@nonStarredWithS}%
{\@nonStarredWithoutS}%
}
\def\@nonStarredWithS[#1]#2{%
\xdef\ind{s:\therealsection}
\oldsection[#1]{#2}\index{\ind}\label{\ind}\def\Sectionname{#2}%
}
\def\@nonStarredWithoutS#1{%
\xdef\ind{s:\therealsection}
\oldsection{#1}\index{\ind}\label{\ind}\def\Sectionname{#1}%
}
\let\oldsubsection=\subsection
\def\subsection{%
\stepcounter{realsubsection}
\xdef\PrevSec{\curfmark}
\global\Level=3%
\@ifstar{\@StarredSS}{\@nonStarredSS}%
}
\def\@StarredSS{%
\@ifnextchar[%
{\@StarredWithSS}%
{\@StarredWithoutSS}%
}
\def\@StarredWithSS[#1]#2{%
\xdef\ind{ss:\therealsubsection}
\oldsubsection*{#2}\index{\ind}\label{\ind}\def\Subsectionname{#1}% Here you can use starred chapter, section etc with optional argument for chaptermark or sectionmark etc
}
\def\@StarredWithoutSS#1{%
\xdef\ind{ss:\therealsubsection}
\oldsubsection*{#1}\index{\ind}\label{\ind}\def\Subsectionname{#1}%
}
\def\@nonStarredSS{%
\@ifnextchar[%
{\@nonStarredWithSS}%
{\@nonStarredWithoutSS}%
}
\def\@nonStarredWithSS[#1]#2{%
\xdef\ind{ss:\therealsubsection}
\oldsubsection[#1]{#2}\index{\ind}\label{\ind}\def\Subsectionname{#1}%
}
\def\@nonStarredWithoutSS#1{%
\xdef\ind{ss:\therealsubsection}
\oldsubsection{#1}\index{\ind}\label{\ind}\def\Subsectionname{#1}%
}
\let\oldsubsubsection=\subsubsection
\def\subsubsection{%
\stepcounter{realsubsubsection}
\xdef\PrevSec{\curfmark}
\global\Level=4%
\@ifstar{\@StarredSS}{\@nonStarredSS}%
}
\def\@StarredSSS{%
\@ifnextchar[%
{\@StarredWithSSS}%
{\@StarredWithoutSSS}%
}
\def\@StarredWithSSS[#1]#2{%
\xdef\ind{sss:\therealsubsubsection}
\oldsubsubsection*{#2}\index{\ind}\label{\ind}\def\Subsubectionname{#1}% Here you can use starred chapter, section etc with optional argument for chaptermark or sectionmark etc
}
\def\@StarredWithoutSSS#1{%
\xdef\ind{sss:\therealsubsubsection}
\oldsubsubsection*{#1}\index{\ind}\label{\ind}\def\Subsubsectionname{#1}%
}
\def\@nonStarredSSS{%
\@ifnextchar[%
{\@nonStarredWithSSS}%
{\@nonStarredWithoutSSS}%
}
\def\@nonStarredWithSSS[#1]#2{%
\xdef\ind{sss:\therealsubsubsection}
\oldsubsubsection[#1]{#2}\index{\ind}\label{\ind}\def\Subsubsectionname{#2}%
}
\def\@nonStarredWithoutSSS#1{%
\xdef\ind{sss:\therealsubsubsection}
\oldsubsubsection{#1}\index{\ind}\label{\ind}\def\Subsubsectionname{#1}%
}
\makeatother
\def\mysecdepth#1{\ifcase#1
Part
\or \empty%
\or Sectionname%
\or Subsectionname%
\or Subsubsectionname%
\fi}
\fancyhead[LE,RO]{\fmark}
\fancyhead[LO,RE]{\smark}
\def\curfmark{\ifnum\thepage<\getpagerefnumber{\ind}\PrevSec\else\ifnum\Level>\MaxLevel\csname\mysecdepth{\MaxLevel}\endcsname\else\csname\mysecdepth{\the\Level}\endcsname\fi\fi}
\newcommand{\fmark}{\curfmark}
\newcommand{\smark}{\Chaptername}
\fancyfoot[LE,RO]{\thepage} % Left side on Even pages; Right side on Odd pages
\begin{document}
\chapter{Chapter Intro}
\lipsum
\lipsum
\chapter*{Chapter 1 with subsections}
\section{section foo 1}
\lipsum[2-13]
\subsection{subsection bar 1}
\lipsum
\chapter{Chapter 2 with subsections}
\section{section foo 2}
\lipsum[2-13]
\subsection{subsection bar 2}
\lipsum
\chapter*[Different title in fancyheader]{Chapter 3 with subsections}
\section{section foo 3}
\lipsum
\subsection{subsection bar 3}
\lipsum
the last sebsection was \ref{ss:3} with starting on page \pageref{ss:3}
\end{document}
(Used Detecting if inside \section) and from this my answer
Old answer
Test this to see if it is ok
\documentclass[11pt,a4paper,twoside,titlepage,openany]{book}
\usepackage{lipsum}
\usepackage[T1]{fontenc}
\usepackage{amsfonts}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{lmodern}
\usepackage{sectsty}
\usepackage{geometry}
\geometry{
left=35mm,
right=30mm,
top=27mm,
bottom=30mm
}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}
\fancyhead[LE,RO]{\fmark}
\fancyhead[LO,RE]{\smark}
\let\Chaptermark\chaptermark
\def\chaptermark#1{\def\Chaptername{#1}\Chaptermark{#1}}
\newcommand{\fmark}{\rightmark}
\newcommand{\smark}{\Chaptername}
\fancyfoot[LE,RO]{\thepage} % Left side on Even pages; Right side on Odd pages
\begin{document}
\chapter{Chapter Intro}
\lipsum
\lipsum
\chapter{Chapter with subsections}
\section{section foo}
\lipsum
\subsection{subsection bar}
\lipsum
\end{document}
Used answer here: how to get the current chapter name, section name, subsection name, etc?
\fancyhf{}
to see something, but this make double numbering page in the foot.