0

I would like to have a macro which pass the book title both to the title page and to the page headlines. Unfortunately if I pass the title containing new line command (to break line on title page) then the line is broken also in page headline.

For resolving conflict between titling and titlesec packages I am using @frabjous solution. Probably I have to combine it with some method of removing new line command, given here. I don't know how to do this.

Code:

\documentclass{article}
\usepackage[paperwidth=12cm,paperheight=11cm,twoside]{geometry}
\usepackage{fancyhdr}
\usepackage{titling}
\usepackage{titlesec}

\fancyhead[LE]{\mythetitle}
\pagestyle{fancy}
\let\oldtitle\title
\renewcommand{\title}[1]{\oldtitle{#1}\newcommand{\mythetitle}{#1}}

\author{Cookiemonster}
\title{Title\\consisting of \\ four \\lines}

\begin{document}
\begin{titlingpage}
    \maketitle
\end{titlingpage}
\section{My section}
AAA
\end{document}

enter image description here

enter image description here

BTW: In accordance to the documentation of titling package (top of the page 5) after title page the page number should be 1, not 2. What I am doing wrong in this code?

4
  • \fancyhead[LE]{\renewcommand\\{\space\ignorespaces}\mythetitle} Commented Dec 5, 2020 at 10:07
  • 1
    The pagecounter of the following page is only set to 1 for a onesided document, for some reason, titling doesn't set it in a twoside document (well, even/odd detection wouldn't work correctly without an additional \cleardoublepage). So you have to do it yourself, just put \setcounter{page}{1} after \end{titlingpage}.
    – Skillmon
    Commented Dec 5, 2020 at 10:11
  • @DavidCarlisle this will give two spaces if there is a space before \\ .
    – Skillmon
    Commented Dec 5, 2020 at 10:20
  • @Skillmon you shouldn't have a space before:-) but changing it to \ifhmode\unskip\fi\space\ignorespace Commented Dec 5, 2020 at 10:22

2 Answers 2

3

You can use

\fancyhead[LE]{\renewcommand\\{\unskip\space\ignorespaces}\mythetitle}

so that within the head \\ acts as a space, while ignoring space before and after so you do not get doubled spaces.

1
  • Good minimalistic solution!
    – Skillmon
    Commented Dec 5, 2020 at 10:59
2

The following removes the \\ from your title using l3regex (because there might be spaces before and after the \\ which have to be normalised to one space in total, and doing this with a regular expression is pretty convenient).

Additionally I fixed the page numbering to start with 1, but to get correct odd/even page formatting one has to introduce a blank page after the title in that case (done so with \thispagestyle{empty}\cleardoublepage).

\documentclass{article}
\usepackage[paperwidth=12cm,paperheight=11cm,twoside]{geometry}
\usepackage{fancyhdr}
\usepackage{titling}
\usepackage{titlesec}

\fancyhead[LE]{\mythetitle}
\pagestyle{fancy}
\let\oldtitle\title
\ExplSyntaxOn
\cs_new_protected:Npn \SXforrest_store_title:Nn #1#2
  {
    \tl_set:Nn \l_tmpa_tl {#2}
    \regex_replace_all:nnN { \s* \c{\\} \s* } { \  } \l_tmpa_tl
    \tl_gset_eq:NN #1 \l_tmpa_tl
  }
\cs_set_eq:NN \storemytitle \SXforrest_store_title:Nn
\ExplSyntaxOff
\renewcommand{\title}[1]{\oldtitle{#1}\storemytitle\mythetitle{#1}}

\author{Cookiemonster}
\title{Title\\consisting of \\ four \\lines}

\begin{document}
\begin{titlingpage}
    \maketitle
\end{titlingpage}

\thispagestyle{empty}
\cleardoublepage
\setcounter{page}{1}

\section{My section}
AAA
\newpage
BBB
\end{document}

enter image description here

enter image description here

enter image description here

You must log in to answer this question.

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