使用章節的自訂指令在章節標題中出現換行問題

使用章節的自訂指令在章節標題中出現換行問題

如果您必須在章節標題中強制換行,可以這樣做:

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

但在我的文件中,我必須定義自己的章節指令,以便有章節作者和副標題:

\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 }{}%  
}}

這按預期工作,但沒有換行符:

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

隨著換行符號出現錯誤:

! Paragraph ended before \@firstoftwo was complete.
<to be read again>
\par
l.2 ...how sad is this!}{}
I suspect you've forgotten a `}', causing me to apply this

然而,換行符有效,但在先前的頁面上,換行符之後的部分出現在額外的頁面上。我做錯了什麼?

這是一個完整的工作範例:

\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]{%
{\addvspace{10pt}\nopagebreak\setlength{\parindent}{0em}\relax
\rightskip \@tocrmarg\relax
\itshape#1\par\addvspace{-7pt}}}
\makeatother
\newcommand\authortoc[1]{\needspace{3\baselineskip}%
  \gdef\chapterauthor{#1}%
  \addtocontents{toc}{\authortoctext{%
    \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}

答案1

一種可能性:使用

% 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}}

我定義了兩個命令\AnnCL\RecCL重新定義\newline\relax然後恢復其原始含義。在 的定義中的正確位置使用這些命令現在允許您在 的第一個強制參數內\Chapter使用。文件正文中將出現換行符,但目錄中不會出現任何效果。\newline\Chapter

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

\newcommand{\chapterauthor}

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

\makeatletter
\DeclareRobustCommand\authortoctext[1]{%
{\addvspace{10pt}\nopagebreak\setlength{\parindent}{0em}\relax
\rightskip \@tocrmarg\relax
\itshape#1\par\addvspace{-7pt}}}
\makeatother
\newcommand\authortoc[1]{\needspace{3\baselineskip}%
  \gdef\chapterauthor{#1}%
  \addtocontents{toc}{\authortoctext{%
    \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}

ToC 的圖像:

在此輸入影像描述

包含章節標題的頁面圖片:

在此輸入影像描述

答案2

你的問題是\StrRight嘗試充分擴展論證但\\無法倖存。

如果您希望目錄中也有換行符,那麼說

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

解決你的問題。否則,只需\?在文字中定義為換行符,並在目錄中本地將其定義為 no 操作:

\protected\def\?{\\}

並說

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

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

無論如何,我會保留\noexpandarg做的時候\StrRight

相關內容