1

I am following up further on the questions in: Inserting predefined paragraph numbers in page header I was not allowed to comment on this issue so had to create a new question.

@Andrew. @Sujato has gone offline for a few months and asked me to take over this issue as I have the main document.

I still have some issues left. One of them is that your hack on point 6 created a new page break in between the chapter-heading and the first section, which was not there before. Any ideas on how to overcome this issue?

EDIT: New MWE - hope this is OK now.

\documentclass[11pt,openany]{book}
\usepackage{lipsum}
\setlipsumdefault{1}

\usepackage{sectsty}
\usepackage[explicit,raggedright]{titlesec}
\usepackage[tocflat]{tocstyle}
\usepackage{tocloft}
\usepackage{soulutf8}
\usepackage{hanging}

%TABLE OF CONTENTS%
\settocstylefeature[]{leaders}{\hfill}
\settocstylefeature[]{entryhook}{}
\settocstylefeature[]{raggedhook}{\raggedright}
\settocstylefeature[]{entryvskip}{0.2em}
\settocstylefeature[0]{entryvskip}{1em}
\settocstylefeature[0]{entryhook}{\bfseries}
\settocstylefeature[0]{pagenumberbox}{\csname @gobble\endcsname}
\renewcommand*{\cfttoctitlefont}{\Large}
%TABLE OF CONTENTS%

\titleformat{\chapter}[display]
{\filcenter}
{}
{1ex}
{\Huge {#1}
\addcontentsline{toc}{chapter}{#1}
\markboth{#1}{#1}}


\titleformat{\section}
  {\centering\Large\mdseries}{\thesection}{1em}{\caps{#1}}

  \titleformat{\subsection}
  {\mdseries\large\raggedright}{}{}{#1}

  \titleformat{\subsubsection}
  {\mdseries\raggedright}{}{}{#1}


%HEADER and FOOTER% 
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} % clear all header and footer fields
\fancyfoot[LE,RO]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\fancyhead[CO]{\headcaps{\MakeUppercase{\rightmark}}}
\renewcommand{\sectionmark}[1]{%
   \markright{#1}%
   \def\firstpara{0}\def\midpara{0}\def\lastpara{0}
}
\fancyhead[CE]{\headcaps{\MakeUppercase{\leftmark}}}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\fancyfoot[LO,RE]{\ifnum\firstpara>0\relax\footnotesize MN \thesection.\theparagraph\fi}

\newcommand\headcaps[1]{{ #1}}
\let\realSection\section
\renewcommand\section[2][\relax]{\clearpage%
  \thispagestyle{plain}%
  \ifx#1\relax\relax\realSection{#2}%
  \else\realSection[#1]{#2}%
  \fi
}
\renewcommand\thesection{\arabic{section}}

\def\firstpara{0}%
\def\midpara{0}%
\def\lastpara{0}%
\def\setparagraph#1-#2-#3|{\def\midpara{#1}%
   % if \firstpara=0 => new page => set \firstpara
   \ifnum\firstpara=0\relax\def\firstpara{#1}\fi%
   % if #2=0 => only #1 is meaningful to set \lastpara to this
   % otherwise use #2
   \ifnum#2=0\relax\def\lastpara{#1}%
   \else\def\lastpara{#2}%
   \fi%
}
\def\paragraph#1{\textbf{#1}\space%
  \setparagraph#1-0-|%
}
\def\theparagraph{
  % if \firstpara=\lastpara print only \firstpara otherwise print a range
  \ifnum\firstpara=\lastpara\relax\firstpara\else\firstpara-\lastpara\fi
  % reset firstpara and lastpara
  \xdef\firstpara{\midpara}%
  %\let\lastpara\relax%
}
% need to reset \firstpara on pages without headers
\fancypagestyle{plain}{ 
  \fancyhf{} 
  \fancyfoot[LE,RO]{\thepage}
  \fancyfoot[LO,RE]{\ifnum\firstpara>0\relax\footnotesize MN \thesection.\theparagraph\fi}
  \renewcommand{\headrulewidth}{0pt}
  \renewcommand{\footrulewidth}{0pt}
}
%HEADER & FOOTER%

\setcounter{section}{0} %This can be a higher number f.i. in volume 2

%WIDOWS & ORPHANS% 
\widowpenalty=1000
\clubpenalty=1000
%WIDOWS & ORPHANS%


\date{}

\title{Document Title}
\author{Author Name}

\begin{document}

\frontmatter
\pagestyle{empty}

\maketitle

\newpage

\newpage

\tableofcontents

\newpage
\pagestyle{fancy}

\chapter*{Preface}

\lipsum

\mainmatter

\chapter*{The First chapter}

\section{First section}

\paragraph{1}\lipsum

\paragraph{2}\lipsum

\paragraph{3-5}\lipsum

\paragraph{6}\lipsum

\paragraph{7-17}\lipsum

\paragraph{18}\lipsum

Paragraph without number

\paragraph{19-20}\lipsum

\paragraph{21-28}\lipsum

\paragraph{29}\lipsum

\paragraph{30}\lipsum

\section{Second Section}

\paragraph{1}\lipsum

\paragraph{2}\lipsum

\paragraph{3-5}\lipsum

\paragraph{6}\lipsum

\subsection*{Some subsection}

\paragraph{7-17}\lipsum

\paragraph{18}\lipsum

\paragraph{19-20}\lipsum

\paragraph{21-28}\lipsum

\paragraph{29}\lipsum

\paragraph{30}\lipsum

\chapter*{Second Chapter}

\section{Third Section}

\paragraph{1}\lipsum

\paragraph{2}\lipsum

\paragraph{3}\lipsum

\subsection*{Some subsection}

\paragraph{4}\lipsum

\paragraph{5}\lipsum

\subsubsection*{Some subsubsection}

\paragraph{6}\lipsum

\paragraph{7}\lipsum

\paragraph{8}\lipsum

\paragraph{9}\lipsum

\paragraph{10}\lipsum

\paragraph{11}\lipsum

\paragraph{12}\lipsum

\paragraph{13}\lipsum

\paragraph{14}\lipsum

\paragraph{15}\lipsum

One more line

\paragraph{16}\lipsum

\paragraph{17}\lipsum

\paragraph{18}\lipsum

\paragraph{19}\lipsum

\paragraph{20}\lipsum

\end{document}

There are two issues in this MWE as we had discussed below:

  1. The chapter headings are on a separate page now instead of on the same page as the section heading following the chapter.
  2. The paragraph numbers don't always correctly reflect the numbers on that page - in the screenshot on the bottom of page 4 the numbers are 7-20 and should be 7-18 see screenshot
13
  • Welcome to TeX.SX! Can you concretize your question with an example? Aug 12, 2015 at 7:15
  • @Andrew suggested to replace the following code: \newcommand{\sectionbreak}{\newpage\thispagestyle{plain}} with \let\realSection\section \renewcommand\section[2][\relax]{\clearpage% \thispagestyle{plain}% \ifx#1\relax\relax\realSection{#2}% \else\realSection[#1]{#2}% \fi } This fixed the issue as described in point 6 in the aforementioned post but it also created a new issue, namely a new page break in between the chapter heading and the first section.
    – Vimala
    Aug 12, 2015 at 7:34
  • OK, perhaps I misunderstood. I thought that you wanted every section to start on a new page. Is it true that you want every section EXCEPT for the first section to start on the first page? If so then replacing the \clearpage with \ifnum\arabic{section}>1\clearpage\fi will do what you want.
    – user30471
    Aug 12, 2015 at 8:19
  • @Andrew - That would also not quite work because the sectionnumber after a chapter is not always 1. The main document has 152 sections numbered 1-152, divided into 15 chapters and there is no clear logic as to where the chapter headings go (it's an ancient text).
    – Vimala
    Aug 12, 2015 at 8:34
  • The section number is reset at the start of each chapter. In fact, it above it should be \ifnum\arabic{section}>1\clearpage\fi because the section number is reset to 0 rather than 1. But I guess that you are saying that the chapters and section numbers are not related in a standard way. Returning to my question, is it true that you want a page break before every section EXCEPT for the first section in a chapter. If this is what you want then I can post a way of doing this without using the section numbers.
    – user30471
    Aug 12, 2015 at 8:45

1 Answer 1

2

Here is a modified version of the code from Inserting predefined paragraph numbers in page header. I think that this does what you want.

What I have one is made \chapter set a flag \IsNewChaptertrue which is then used by the \section command to determine whether or not it should start a new page using \clearpage.

I think that I tested this when I was answering the previous question and determined that the issue with the incorrect section numbers in the heading is caused by the \sectionbreak command from the titlesec package, but perhaps I am misunderstanding something.

Anyway, here is the full code:

\documentclass{book}
\usepackage{lipsum}
\setlipsumdefault{1}
\usepackage{titlesec}
%\usepackage[hmargin=63mm,vmargin=68mm]{geometry}

% fancy headings as defined by the OP except we need to be more careful
% with plain headings
\usepackage{fancyhdr}
\fancypagestyle{empty}{
  \fancyhf{}
  \renewcommand{\headrulewidth}{0pt}
}
\fancypagestyle{plain}{
  \fancyhf{}% clear all header and footer fields
  \renewcommand{\headrulewidth}{0pt}
  \renewcommand{\footrulewidth}{0pt}
  \fancyfoot[C]{\bfseries \thepage}%
  % need to reset \firstpara on pages without headers
  \xdef\firstpara{\midpara}%
}
\fancypagestyle{paragraphs}{
  \fancyhead[LE,RO]{\thepage}
  \fancyhead[CO]{\rightmark}
  \fancyhead[CE]{\leftmark}
  \fancyhead[LO,RE]{\ifnum\firstpara>0\relax\thesection.\theparagraph\fi}
}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{%
   \markright{#1}%
   % reset paragraph counters in each section
   \def\firstpara{0}\def\midpara{0}\def\lastpara{0}
}
\newif\ifIsNewChapter\IsNewChapterfalse
\let\realChapter\chapter
\renewcommand\chapter[2][\relax]{%
  \IsNewChaptertrue%
  \ifx#1\relax\relax\realChapter{#2}%
  \else\realChapter[#1]{#2}%
  \fi
}

\let\realSection\section
\renewcommand\section[2][\relax]{\ifIsNewChapter\relax\else\clearpage\fi
  \IsNewChapterfalse%
  \thispagestyle{plain}%
  \ifx#1\relax\relax\realSection{#2}%
  \else\realSection[#1]{#2}%
  \fi
}
\renewcommand\thesection{\arabic{section}}

% now the macros for tracking the paragraph numbers
\def\firstpara{0}%    set to 1 by default
\def\midpara{0}%    set to 1 by default
\def\lastpara{0}% set to 1 by default
\def\setparagraph#1-#2-#3|{\def\midpara{#1}%
   % if \firstpara=0 => new page => set \firstpara
   \ifnum\firstpara=0\relax\def\firstpara{#1}\fi%
   % if #2=0 => only #1 is meaningful to set \lastpara to this
   % otherwise use #2
   \ifnum#2=0\relax\def\lastpara{#1}%
   \else\def\lastpara{#2}%
   \fi%
}
\def\paragraph#1{\textbf{#1}\space
  \setparagraph#1-0-|%
}
\def\theparagraph{%
  % if \firstpara=\lastpara print only \firstpara otherwise print a range
  \ifnum\firstpara=\lastpara\relax\firstpara\else\firstpara--\lastpara\fi
  % reset firstpara and lastpara
  \xdef\firstpara{\midpara}%
  %\let\lastpara\relax%
}

% back to the MWe
\begin{document}

\null
\pagestyle{empty}

\newpage

\newpage

\pagestyle{paragraphs}

\chapter{A Chapter}

\section{First Section}

\paragraph{1}\lipsum

\paragraph{2}\lipsum

\paragraph{3-5}\lipsum

\paragraph{6}\lipsum

\paragraph{7-17}\lipsum

\paragraph{18}\lipsum

\paragraph{19-20}\lipsum

\paragraph{21-28}\lipsum

\paragraph{29}\lipsum

\paragraph{30}\lipsum

\section{Second Section}

\paragraph{1}\lipsum

\paragraph{2}\lipsum

\paragraph{3-5}\lipsum

\paragraph{6}\lipsum

\paragraph{7-17}\lipsum

\paragraph{18}\lipsum

\paragraph{19-20}\lipsum

\paragraph{21-28}\lipsum

\paragraph{29}\lipsum

\paragraph{30}\lipsum

\section{Third Section}

\paragraph{1}\lipsum

\paragraph{2}\lipsum

\paragraph{3}\lipsum

\paragraph{4}\lipsum

\paragraph{5}\lipsum

\paragraph{6}\lipsum

\paragraph{7}\lipsum

\paragraph{8}\lipsum

\paragraph{9}\lipsum

\paragraph{10}\lipsum

\paragraph{11}\lipsum

\paragraph{12}\lipsum

\paragraph{13}\lipsum

\paragraph{14}\lipsum

\paragraph{15}\lipsum

\paragraph{16}\lipsum

\paragraph{17}\lipsum

\paragraph{18}\lipsum

\paragraph{19}\lipsum

\paragraph{20}\lipsum

\end{document}
8
  • Thanks Andrew - I see where the confusion lies now. @Sujato has a lot more code in the main document than the MWE. The sectionnumber is not reset at the next chapter. Your solution works well in both the MWE and the real doc but in the real doc it creates a problem with the toc ... I have to look at this better and provide you with a good new MWE rather than to keep patching up things.
    – Vimala
    Aug 13, 2015 at 11:50
  • I have provided a full MWE now, without your latest change in it. There is a font in it that might need to be changed.
    – Vimala
    Aug 14, 2015 at 6:26
  • OK. Will take a look. I have a few days work that has to be finished first so I can't promise to get to you sooner.
    – user30471
    Aug 14, 2015 at 8:18
  • Thank you @Andrew. Take your time - your help is very much appreciated! I'm very happy when this is done in 2 months time :-)
    – Vimala
    Aug 14, 2015 at 9:20
  • @Vimala A minimal working example is the smallest amount of code needed to exhibit your problem. It seems that your MWE wants me to use XeTeX or LuaTeX and I have not succeeded in getting it to compile, in part because I don't have the skolar fonts...I"m sure that none of this is really necessary.
    – user30471
    Aug 18, 2015 at 11:58

You must log in to answer this question.

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