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{}

}
\pagestyle{main}

\newcommand\sectionX[1]{%
\section*{#1}%
\renewcommand\pagina{#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. Dec 18 '16 at 13:27
• Is your only purpose to get unnumbered section headings? Dec 18 '16 at 13:27
• How to correct this situation? The hard-page seems to be rather strange solution. Dec 18 '16 at 15:10
• The second comment of me was much more important. If you are only after unnumbered headings, the solutions is amazingly simple. 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. Dec 18 '16 at 20:33

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}

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

• Thank you. However the fancyhdr not always work fine. \markright with titlesec is strange. Is there any simple solution for the titlesec? Dec 18 '16 at 20:30