[Tex/LaTex] Trouble with titlesec

double-sidedheader-footertitlesectwo-column

I've been using titlesec for quite some time now and want to "pimp up my style" 😉

I've read How to change header to list first section on page and not last section and tried to adapt the code for my purposes. I'm writing a twocolumn, twosided layout and would like my header to be formatted such that the header shows the context (i.e. section and subsection number and title) of the current two visible pages.

  • On left pages the header should list:
    • print the section/subsection "enclosing" the first line of that page, if there's no subsection enclosing the first line print only the section
    • section or subsection or section and subsection may be printed on a previous page
  • On right pages the header should list:
    • print the section/subsection "enclosing" the last line of that page, if there's no subsection enclosing the last line print only the section
    • section or subsection or section and subsection may be printed on a previous page

Let's have a look at my MWE:

\documentclass[twoside]{article}
\usepackage[bf,pagestyles,raggedright]{titlesec}% http://ctan.org/pkg/titlesec
\usepackage{lipsum}% http://ctan.org/pkg/lipsum

%% to be used when odd pages are on the RIGHT HAND SIDE
\newpagestyle{main}{%
  \headrule
  \sethead[\thepage]% even-left
    []% even-center
    [\bfseries{\scriptsize \bottitlemarks\thesubsection$\:$\bottitlemarks\subsectiontitle} $\quad$ \bfseries{\bottitlemarks\thesection$\:$\bottitlemarks\sectiontitle}]% even-right
    {\bfseries{\toptitlemarks\thesection$\:$\toptitlemarks\sectiontitle} $\quad$ \bfseries{\scriptsize \toptitlemarks\thesubsection$\:$\toptitlemarks\subsectiontitle}}% odd-left
    {}% odd-center
    {\thepage}% odd-right
\setfoot[]% even-left
    [\thepage]% even-center
    []% even-right
    {}% odd-left
    {\thepage}% odd-center
    {}% odd-right
}

\newpagestyle{TitleMarks}{%
  \headrule
  \sethead[E:Top is~\toptitlemarks\thesection:\toptitlemarks\thesubsection]% even-left
    [E:First is~\firsttitlemarks\thesection:\firsttitlemarks\thesubsection]% even-center
    [E:Bottom is~\bottitlemarks\thesection:\bottitlemarks\thesubsection]% even-right
    {O:Top is~\toptitlemarks\thesection:\toptitlemarks\thesubsection}% odd-left
    {O:First is~\firsttitlemarks\thesection:\firsttitlemarks\thesubsection}% odd-center
    {O:Bottom is~\bottitlemarks\thesection:\bottitlemarks\thesubsection}% odd-right
\setfoot[][\thepage][]{}{\thepage}{}
}


\pagestyle{main}

\twocolumn

\begin{document}
\section{One}
\lipsum[1]
\section{Two}
\lipsum[1]
\subsection{TwoOne}
\lipsum[1-3]
\subsection{TwoTwo}
\lipsum[1-4]
\section{Three}
\lipsum[1]
\end{document}  

And the result (don't mind the strange font for the title/header, that's an artifact of combining two pages in inkscape):

result3

On page one it states "2 Two" instead of "1 One" (section enclosing first line of text) [This depends on the "length" of secion one, once a part of section one continues into the second column the header switches to "1 One". Edit: After some further testing I've come to the conclusion that this a result of using twocolumn style, switching to onecolumn text resolves this part of the problem. However, this is not an option for me.] and on page two it prints "2.2 TwoTwo 2 Two" instead of "2.1 TwoOne 2 Two" (i.e. the section&subsection enclosing the first line of that page).

Edit2: Some further investigation yielded a sort-of-solution for the first problem described above. When using the fixltx2e package the header displays "0 Contents" (when there's a ToC) for \toptitlemarks and a "1 One" for \firsttitlemarks (which is sort-of what I expect yet not the results I'd like to have. Of course it is kind of pointless to tell the reader that on the page just before page one there's the ToC, i.e. this would require either using two pagestyles [one for page one which used \firsttitlemarks instead of \topttitlemarks and another one for all other pages]). However I can not use fixltx2e because I need dblfloatfix which conflicts with the former (well, more precisely, the former doesn't work anymore when both are loaded) -> this is no solution for me!

Is it possible to change that?

Best Answer

Working through the requirements here, including the business about the fixes for the kernel, I think that everything does work if you take things a bit at a time

% Should be part of the kernel
\RequirePackage{fixltx2e}
\documentclass[twoside]{article}
% Required by the OP for 'other things'
\usepackage{dblfloatfix}
% For filler
\usepackage{lipsum}

\usepackage[pagestyles]{titlesec}
\newpagestyle{main}{%
  \sethead
    [\bottitlemarks\thesubsection\ \subsectiontitle]
    []
    [\bottitlemarks\thesection\ \sectiontitle]
    {\toptitlemarks\thesection\ \sectiontitle}
    {}
    {\toptitlemarks\thesubsection\ \subsectiontitle}%
}
\pagestyle{main}


\twocolumn

\begin{document}
\section{One}
\lipsum[1]
\section{Two}
\lipsum[1]
\subsection{TwoOne}
\lipsum[1-3]
\subsection{TwoTwo}
\lipsum[1-4]
\section{Three}
\lipsum[1]
\end{document}  

The result it:

  • Page 1 has a left header '1 One' and no right header (first line of the page is a section, no enclosing subsection)

  • Page 2 has left header '2.2 TwoTwo' and right header '2 Two' (last line of the page is in section two subsection two)

  • Page 3 has left header '2 Two' and right header '2.2 TwoTwo' (first line of the page is in section two subsection two)

That matches up with what I (think) your requirements are ('left' and 'right' in the part before the example don't really agree with page number issues as described after!).

Related Question