0

Using \newpagestyle I wanted to put the section title in the page header. However on the 1st page with the first section I have the header of the second section, as the example shows:

\documentclass[a5paper,12pt]{article}

\usepackage[sf,bf,pagestyles]{titlesec}
\usepackage{lipsum}

\newcommand*\pagina{}

\newpagestyle{main}{\headrule%
    \sethead{}{\footnotesize\it\pagina}{\thepage}
}
\pagestyle{main}

\newcommand\sectionX[1]{%
    \section*{#1}%
    \renewcommand\pagina{#1}%
    \addcontentsline{toc}{section}{#1}%
}

\begin{document}

\sectionX{Uno}

\framebox{We have here header ``Due''. WHY?}

\bigskip

\lipsum[1]

\sectionX{Due}

\lipsum[2]

\sectionX{Tre}

\lipsum[3]

\clearpage

\tableofcontents

\end{document}
  • The header is updated before LaTeX decides that the section needs to be put on a new page. – Johannes_B Dec 18 '16 at 13:27
  • 1
    Is your only purpose to get unnumbered section headings? – Johannes_B Dec 18 '16 at 13:27
  • How to correct this situation? The hard-page seems to be rather strange solution. – zeta0 Dec 18 '16 at 15:10
  • 1
    The second comment of me was much more important. If you are only after unnumbered headings, the solutions is amazingly simple. – Johannes_B Dec 18 '16 at 15:18
  • I want to have more complex header than in the example code. This is the minimum which makes problems. – zeta0 Dec 18 '16 at 20:33
1

That's why there are marks. These pick up information at the time the page is built.

Here is a solution with fanchdr. [Actually I would suggest not to redefine your own section command, but I just kept it.]

\documentclass[a5paper,12pt]{article}

\usepackage{lipsum}

\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}
\chead{\footnotesize\textit{\rightmark}}
\rhead{\thepage}

\newcommand\sectionX[1]{%
    \section*{#1}%
    \markright{#1}%
    \addcontentsline{toc}{section}{#1}%
}

\begin{document}

The rest is the same.

| improve this answer | |
  • Thank you. However the fancyhdr not always work fine. \markright with titlesec is strange. Is there any simple solution for the titlesec? – zeta0 Dec 18 '16 at 20:30
  • I don't know. Do you have an example where it is strange? – Pieter van Oostrum Dec 18 '16 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.