1

I have the next environnement:

\usepackage{fancyhdr}
\pagestyle{fancy}

My problem is the next one. I have a section and a subsection with a too long name and they are overlapping on the page header. I know this solution

\section[section with\\ long title]{section with long title}

But this solution will modify the section name in the table of contents.

I also try the next solution:

\fancyhead[L]{
\begin{minipage}[t]{0.48\textwidth}
\begin{flushleft}
\rightmark
\end{flushleft}
\end{minipage}
}

\fancyhead[R]{
\begin{minipage}[t]{0.48\textwidth}
\begin{flushright}
\leftmark
\end{flushright}
\end{minipage}
}

It is working but the header overlap the lines at the top of the page.

So the big question is: how to say to fancy page style to automatically break the section and/or subsection name in the header of the page without modify the toc and also replace the hline under the header to not cover a part of the text on the page?

3
  • Welcome to TeX SX! If you use titlesec, the [toctitles] option makes the optional (shorter) argument for \section be used only in the header, not in the table of contents. Also, note titleps is an easier-to-use alternative to fancyhdr.
    – Bernard
    Oct 20, 2017 at 13:30
  • Is it a book? an article? which documentclass do you use?
    – koleygr
    Oct 20, 2017 at 13:58
  • It is the article documentclass.
    – AlexDC
    Oct 20, 2017 at 14:35

2 Answers 2

2

You can use \sectionmark.

\section{long version%
          \sectionmark{header version}}
\sectionmark{header version}

You need two \sectionmark-commands to get the correct header both for the current page and the following ones. The KOMA-classes have additional options to handle this.

(Untested as you didn't provide a complete minimal example).

0

You can redefine section to let you use a starred section with optional argument.

This is the code:

\documentclass[a4paper, 11pt]{article}
\usepackage{lipsum}
\usepackage{fancyhdr}


\fancyhead[L]{
\begin{minipage}[t]{0.48\textwidth}
\begin{flushleft}
\rightmark
\end{flushleft}
\end{minipage}
}

\fancyhead[R]{
\begin{minipage}[t]{0.48\textwidth}
\begin{flushright}
\leftmark
\end{flushright}
\end{minipage}
}

    \let\oldleftmark=\leftmark

    \let\oldsection\section
    \makeatletter
    \def\section{%
    \renewcommand{\leftmark}{\oldleftmark}%
    \@ifstar{\@Starred}{\@nonStarred}%
    }
    \def\@Starred{%
    \@ifnextchar[%
    {\GenericWarning{}{Warning: A starred section can not have optional parameters. I am going use it for changing header only!}\@StarredWith}%
    {\@StarredWithout}%
    }      
    \def\@StarredWith[#1]#2{%
    \renewcommand{\leftmark}{#1}% Or \rightmark
    \oldsection{#2}%
    }
    \def\@StarredWithout#1{
    \oldsection*{#1}%
    }
    \def\@nonStarred{%
    \@ifnextchar[%
    {\@nonStarredWith}%
    {\@nonStarredWithout}%
    }
    \def\@nonStarredWith[#1]#2{%
    \oldsection[#1]{#2}%
    }
    \def\@nonStarredWithout#1{%
    \oldsection{#1}%
    }
    \makeatother 

\title{My title}
\author{me}

\begin{document}
\pagestyle{fancy}

\maketitle

\tableofcontents

\section{section}
\lipsum[1-10]
\section*{starred section}
\lipsum[1-4]
\section[Toc-header title]{other title in toc and header}
\lipsum[1-15]
\section*[header title]{a raelly long long long title that will be as is in toc but have just "header title" in header}
\lipsum[1-10]
\section{Regular section}
\lipsum[1-10]
\end{document}

The double indented part is the addition you have to give.

PS: Upvote here: https://tex.stackexchange.com/a/380116/120578 :P

You must log in to answer this question.

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