0

I would like to use the following fancy toc in my memoir document. However, it seems that \usepackage{tocloft} is incompatible with memoir. Is it possible that I use it with memoir? What modifications should be made?

I cannot change to book since I use fancy chapter styles of memoir.

2
  • 2
    From its documentation, memori has by default emulated the functionality of tocloft, so as my understanding you can directly use macros from that package in memoir class. Perhaps you can provide a complete example to show the actual issue. Aug 18, 2020 at 13:48
  • The answer can certainly be adapted to memoir, but not completely trivially. The tocloft commands \cftchap... need to be replaced with \cftchapter... for use with memoir, and the patching of \@chapter will need to be adapted to the memoir version of \@chapter.
    – Alan Munn
    Aug 18, 2020 at 14:12

1 Answer 1

3

Here's a version of the linked answer that uses memoir. The main changes are renaming the tocloft commands (which are slightly different in memoir), and redfining the memoir \@chapter command rather than just patching it.

\documentclass[10pt,a4paper,openany]{memoir}
% Original code from https://tex.stackexchange.com/a/301026/2693
% adapted to work with memoir instead of book+tocloft
\usepackage[x11names,dvipsnames,svgnames]{xcolor}

\usepackage[most]{tcolorbox}
\usepackage{xparse}
\usepackage{array}

\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\makeatletter


\tcbset{%
  chapternumberbox/.style={%
    colframe=black,
    colback={blue!60!white},
    colupper={white},
    fontupper={\bfseries\normalsize},
    halign=center,
    valign=center,
    size=small,
    nobeforeafter,
    equal height group=chaptertoc,
    left skip=0pt,
    right skip=0pt,
  },
  chapternamebox/.style={%
    left skip=0pt,
    right skip=0pt,
    enhanced jigsaw,
    valign=center,
    interior style={left color={blue!60!white},right color={blue!15!white}},
    colframe={black},
    boxrule={1pt},
    colupper={black},
    size=small,
    fontupper={\bfseries\normalsize},
    nobeforeafter,
    equal height group=chaptertoc,
  }
}

\newtcolorbox{chapternumberboxinternal}[1][]{%
  chapternumberbox,
  #1
}

\newtcolorbox{chapternamebox}[1][]{%
  chapternamebox,
  #1,
}

\NewDocumentCommand{\chapternumberbox}{O{}m}{%
  \begin{chapternumberboxinternal}[#1,width={\cftchapternumwidth}]%-\kvtcb@left@rule-\kvtcb@right@rule}]
    #2%
  \end{chapternumberboxinternal}%
}



\NewDocumentCommand{\mychapternamebox}{O{}+mm}{%
  \begin{chapternamebox}[#1,width={\dimexpr\tocwidth-\cftchapternumwidth+\marginparsep}]
    \begin{tabular}{@{}p{\kvtcb@leftupper}@{}@{}p{\dimexpr\tocwidth-\cftchapternumwidth-\@pnumwidth-\kvtcb@leftupper-\marginparsep}@{}@{}R{\dimexpr\@pnumwidth}@{}@{}p{\kvtcb@rightupper}@{}}
      &  #2&#3
    \end{tabular}
  \end{chapternamebox}%
}


\usepackage{xpatch}
\usepackage{xprintlen}

\newlength\tocwidth

\renewcommand{\cftchapternumwidth}{2.5cm}
\setlength{\cftchapterindent}{0pt}



\AtBeginDocument{%
  \setlength{\tocwidth}{\linewidth}
}

\def\@chapter[#1]#2{%
  \m@mpn@new@chaptrue%
  \m@mpn@new@schapfalse%
  \def\f@rbdy{#2}%
  \ifx\ch@pt@c\@empty % no optional args
    \def\f@rtoc{#2}%
    \def\f@rhdr{#2}%
  \else                  % at least one opt arg
    \let\f@rtoc\ch@pt@c
    \ifx\@empty#1\@empty
      \let\f@rhdr\ch@pt@c
    \else
      \def\f@rhdr{#1}%
    \fi
  \fi
  \m@m@Andfalse
  \ifnum \c@secnumdepth >\m@ne
    \if@mainmatter
      \m@m@Andtrue
    \fi
  \fi
  \ifm@m@And
    \refstepcounter{chapter}%
  \fi
  \ifartopt
    \@makechapterhead{#2}%
    \@afterheading
    \chaptermark{\f@rhdr}%
  \else
    \chaptermark{\f@rhdr}
    \insertchapterspace
    \if@twocolumn
      \@topnewpage[\@makechapterhead{#2}]%
    \else
      \@makechapterhead{#2}%
    \fi
    \@afterheading
  \fi
  \@nameuse{chapter@f@rtoc@before@write@hook}%
  \mempreaddchaptertotochook%
  \ifm@m@And
    \ifanappendix
      \addcontentsline{toc}{appendix}{%
        \protect\chapternumberline{\thechapter}\f@rtoc}%
      \memappchapinfo{\thechapter}{\f@rtoc}{\f@rhdr}{#2}%
    \else
      \addtocontents{toc}{\protect\cftpagenumbersoff{chapter}}
      \addcontentsline{toc}{chapter}{\chapternumberbox{\protect\chaptername\ \thechapter}%
      \protect\mychapternamebox[]{\f@rtoc}{\thepage}}%
      \addtocontents{toc}{\protect\cftpagenumberson{chapter}}%
      \memchapinfo{\thechapter}{\f@rtoc}{\f@rhdr}{#2}%
    \fi
  \else
    \addtocontents{toc}{\protect\cftpagenumbersoff{chapter}}
    \addcontentsline{toc}{chapter}{\chapternumberbox{\protect\chaptername\ \thechapter}
    \protect\mychapternamebox[]{\f@rtoc}{\thepage}}%%
    \addtocontents{toc}{\protect\cftpagenumberson{chapter}}%
    \ifanappendix
      \memappchapinfo{}{\f@rtoc}{\f@rhdr}{#2}%
    \else
      \memchapinfo{}{\f@rtoc}{\f@rhdr}{#2}%
    \fi
  \fi
  \mempostaddchaptertotochook%
  \ifheadnameref\M@gettitle{\f@rhdr}\else\M@gettitle{\f@rtoc}\fi%
  \memendofchapterhook%
}


\makeatother

\begin{document}

\tableofcontents

\chapter{First Chapter}
\section{First Section}‎‎
\section{Second Section}
\chapter{Second Chapter}
\section{First Section}
\section{Second Section}
\chapter[Theory on Brontosaurs]{Third Chapter with an excessively long title that should be avoided!}‎‎
\end{document}

output of code

1
  • Thanks Allan. You're a savior.
    – Tolaso
    Aug 18, 2020 at 15:40

You must log in to answer this question.

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