[Tex/LaTex] Is there method for removing \\ newlines – problem with fancyhdr and multiline chapter name

chaptersfancyhdrheader-footerline-breaking

Some of my chapter has very long names, and they have different break line in ToC and on Chapter page, according to a solution to How to make (force) line-break in Chapter Title (in ToC, Chapter, Header) – if possible?

The problem is that when I have such "multi-line" chapter name, it is broken in fancyhdr.

(For clarification : new lines are problem in fancydhr. In ToC and chapter names they are intentional and have to stay)

Is there a method for removing newlines \\, like \MakeUppercase for making letters uppercase ?

\documentclass[pdftex,a4paper,12pt,twoside,openany]{report}
\usepackage{lipsum}
\usepackage{fancyhdr}
\setlength{\headheight}{15pt}
\setlength{\headsep}{8pt}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ #1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\fancyhf{}
\fancyhead[RO]{\rightmark}
\fancyhead[LE]{\leftmark}
\fancyfoot[C]{\thepage}
\begin{document}
\tableofcontents
\newcommand{\term}{\textit}
%\cleardoublepage \phantomsection
\chapter
[FIRST LONG LINE\\SECOND \textit{LONG} LINE\\THIRD LONG LINE\\FOURTH LONG LINE]
{FIRST LONG LINE\\SECOND \textit{LONG} LINE\\THIRD LONG LINE\\FOURTH LONG LINE}
\lipsum[1-60]
\end{document}

What I am looking for is a kind of macro \MakeNoNewlines that would be used in similar fashion as \MakeUppercase: is used :

\renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ \MakeNoNewlines{#1}}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ \MakeNoNewlines{#1}}}

Best Answer

Your \MakeNoNewlines command can be defined as in the following. We can locally redefine \\ to insert only a <space>. It doesn't help for the Table of contents though.

\documentclass[a4paper,12pt,twoside,openany]{report}
\usepackage{lipsum}
\usepackage{fancyhdr}
\setlength{\headheight}{15pt}
\setlength{\headsep}{8pt}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ \MakeNoNewlines{#1}}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ \MakeNoNewlines{#1}}}

\def\MakeNoNewlines#1{\begingroup\def\\{ }#1\endgroup} % <-- here

\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\fancyhf{}
\fancyhead[RO]{\rightmark}
\fancyhead[LE]{\leftmark}
\fancyfoot[C]{\thepage}
\begin{document}
\tableofcontents
\newcommand{\term}{\textit}
%\cleardoublepage \phantomsection
\chapter
[FIRST LONG LINE\\SECOND LONG LINE\\THIRD LONG LINE\\FOURTH LONG LINE]
{FIRST LONG LINE\\SECOND LONG LINE\\THIRD LONG LINE\\FOURTH LONG LINE}
\lipsum[1-60]
\end{document}