0

how to line break this \mychapter? if i use \chapter i can break line using \chapter[line 1 line 2]{line 1 \\ line 2}

\documentclass{report}
\newcommand{\mychapter}[2]{
    \setcounter{chapter}{#1}
    \setcounter{section}{0}
    \chapter*{#2}
    \addcontentsline{toc}{chapter}{#2}
}
\begin{document}
\tableofcontents
\mychapter{0}{Acknowledgments}
\mychapter{1}{Introduction}
\section{Introduction}
\section{Further Introduction}
\mychapter{2}{Experiments}
\section{Experiment One}
\section{Experiment Two}
\end{document}

and how remove paragraph/space above \mychapter?

enter image description here

1 Answers1

1

The following example updates \mychapter by doing the following:

  1. Store the second argument #2 in a command called \chaptoc.
  2. Use regexpatch's \xpatchcmd* to replace all occurrences of \\ with nothing (effectively removing them)
  3. Use \chaptoc as the addition to the ToC, while printing the original chapter title as part of \chapter*.

enter image description here

\documentclass{report}

\usepackage{regexpatch}

\newcommand{\mychapter}[2]{%
  \setcounter{chapter}{#1}% Set chapter counter
  \setcounter{section}{0}% Reset \section counter
  \def\chaptoc{#2}%
  \xpatchcmd*{\chaptoc}{\\}{}{}{}% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
  \chapter*{#2}
  \addcontentsline{toc}{chapter}{\chaptoc}
}
\begin{document}

\tableofcontents

\mychapter{0}{Acknowledgments \\ and \\ Other things}

\mychapter{1}{Introduction}
\section{Introduction}
\section{Further Introduction}

\mychapter{2}{Experiments}
\section{Experiment One}
\section{Experiment Two}

\end{document}
Werner
  • 603,163