# Problems with line-break in Chapter Title using a custom Command for Chapter

If you have to force a line break in a chapter title, this would be possible doing so:

\chapter[][A Long title that has to be broken,\\ how sad is this!]


But in my document I had to define my own chapter command, in order to have a chapter author and a subtitle:

\newcommand\Chapter[3][]{%
% Last char of the title
\StrRight{#2}{1}[\lastchar]%
% Put to \
\chapter[#2%
% We want a point in the ToC between title and subtitle
\if\relax\detokenize{#3}\relax%
\else%
% But not if there is a ?
\if\lastchar? %
\else%
% Or a !
\if\lastchar! %
% If not set a point
\else%
. %
\fi%
\fi%
\fi%
#3]{#2\\[2ex]\Large\Large#3}
\chaptermark]{\markboth{%
\if\relax\detokenize{#1}\relax  #2%
\else  #1\fi }{}%
}}


This works as expected, but not with a line break:

\Chapter[A Long title]{A Long title that has to be broken,\\ how sad is this!}{Some boring subtitles}


With the line break I get an error:

! Paragraph ended before \@firstoftwo was complete.
\par
I suspect you've forgotten a }', causing me to apply this


How ever, the line break works, but on the page before there appears the part after the line break on an extra page. What did I wrong?

Here is a complete working example:

\documentclass[10pt,twoside]{memoir}
\usepackage{xstring}

\usepackage{fontspec}
\usepackage{xltxtra}

\newcommand{\chapterauthor}

\renewcommand*{\cftchapterdotsep}{\cftdotsep}
\settocdepth{chapter}
\renewcommand{\cftchapterfont}{\normalfont}
\renewcommand{\cftchapterpagefont}{\normalfont}

\makeatletter
\DeclareRobustCommand\authortoctext[1]{%
\rightskip \@tocrmarg\relax
\makeatother
\newcommand\authortoc[1]{\needspace{3\baselineskip}%
\gdef\chapterauthor{#1}%
\needspace{2\baselineskip}%   Vgl. http://tex.stackexchange.com/questions/134016
#1}}}

\renewcommand{\afterchaptertitle}{%

\vskip\onelineskip\begin{large}\normalfont\textit{\Large\chapterauthor}\end{large}
\vspace{4ex}
\vskip\onelineskip}

\newcommand\testchain[1]{%
\StrRight{#1}{1}[\lastchar]%
\IfStrEq{\lastchar}{?}{false}{true}%
}

\newcommand\Chapter[3][]{%
% Last char of the title
\StrRight{#2}{1}[\lastchar]%
% Put to \
\chapter[#2%
% We want a point in the ToC between title and subtitle
\if\relax\detokenize{#3}\relax%
\else%
% But not if there is a ?
\if\lastchar? %
\else%
% Or a !
\if\lastchar! %
% If not set a point
\else%
. %
\fi%
\fi%
\fi%
#3]{#2\\[2ex]\Large\Large#3}
\chaptermark]{\markboth{%
\if\relax\detokenize{#1}\relax  #2%
\else  #1\fi }{}%
}}

\begin{document}
\tableofcontents*

\Chapter[A Long title]{A Long title that has to be broken,\\ how sad is this!}{Some boring subtitles}
\end{document}


One possibility: using

% settings to redefine \newline and then recover its original meaning
\let\oldnewline\newline
% redefinition: \newline is \relax
\newcommand\AnnCL{%
\def\newline{\relax}}
% recovering the original meaning of \newline
\newcommand\RecCL{%
\def\newline{\oldnewline}}


I defined two commands \AnnCL and \RecCL to redefine \newline as \relax and then to recover its original meaning. Using these commands in the proper locations in your definition of \Chapter allows you now to use \newline inside the first mandatory argument of \Chapter. A line break will occur in the document body, but no effect will be present in the ToC.

\documentclass[10pt,twoside]{memoir}
\usepackage{xstring}

\newcommand{\chapterauthor}

\renewcommand*{\cftchapterdotsep}{\cftdotsep}
\settocdepth{chapter}
\renewcommand{\cftchapterfont}{\normalfont}
\renewcommand{\cftchapterpagefont}{\normalfont}

\makeatletter
\DeclareRobustCommand\authortoctext[1]{%
\rightskip \@tocrmarg\relax
\makeatother
\newcommand\authortoc[1]{\needspace{3\baselineskip}%
\gdef\chapterauthor{#1}%
\needspace{2\baselineskip}%   Vgl. http://tex.stackexchange.com/questions/134016
#1}}}

\renewcommand{\afterchaptertitle}{%

\vskip\onelineskip\begin{large}\normalfont\textit{\Large\chapterauthor}\end{large}
\vspace{4ex}
\vskip\onelineskip}

\newcommand\testchain[1]{%
\StrRight{#1}{1}[\lastchar]%
\IfStrEq{\lastchar}{?}{false}{true}%
}

% settings to redefine \newline and then recover its original meaning
\let\oldnewline\newline
% redefinition: \newline is \relax
\newcommand\AnnCL{%
\def\newline{\relax}}
% recovering the original meaning of \newline
\newcommand\RecCL{%
\def\newline{\oldnewline}}

\newcommand\Chapter[3][]{\AnnCL%
% Last char of the title
\StrRight{#2}{1}[\lastchar]%
% Put to \
%
\chapter[#2%
% We want a point in the ToC between title and subtitle
\if\relax\detokenize{#3}\relax%
\else%
% But not if there is a ?
\if\lastchar? %
\else%
% Or a !
\if\lastchar! %
% If not set a point
\else%
.%
\fi%
\fi%
\fi%
#3]{\RecCL#2\\[2ex]\Large\Large#3}
\chaptermark]{\markboth{%
\if\relax\detokenize{#1}\relax  #2%
\else  #1\fi }{}%
}\RecCL}

\begin{document}
\tableofcontents*

\Chapter[A Long title]{A Long title that has to be\newline broken,  how sad is this!}{Some boring subtitles}

\end{document}


An image of the ToC:

An image of the page containing the chapter heading:

Your problem is that \StrRight tries full expansion of the argument and \\ doesn't survive it.

\newcommand\Chapter[3][]{%
% Last char of the title
\saveexpandmode\noexpandarg
\StrRight{#2}{1}[\lastchar]%
\restoreexpandmode
% Put to \
\chapter[#2%
<...the rest is the same...>


solves your problem. Otherwise, just define \? to be a line break in the text and locally define it to be a no op in the table of contents:

\protected\def\?{\\}


and say

\begingroup\let\?\relax
\tableofcontents*
\endgroup

\Chapter[A Long title]
{A Long title that has to be broken,\? how sad is this!}
{Some boring subtitles}


I would keep the \noexpandarg when doing \StrRight` anyway.