16

I'm trying to use the minitoc package to generate a ToC for a subsection. From my reading of the minitoc documentation, this doesn't look too possible to do with minitoc, so I am open to suggestions using minitoc or any other package that can accomplish this goal:

\documentclass{article}
\usepackage[layout=letterpaper,margin=1in]{geometry}

\usepackage{fancyhdr}

\usepackage{minitoc}
\setcounter{secttocdepth}{5}

\begin{document}

\dosecttoc
\faketableofcontents

\fancyfoot[L]{Revision 0.2.3 -- Dec 18, 2012}
\pagestyle{fancyplain}

\section{Small Section}
\section{Huge Section}

\subsection{Large Subsection 1}

This should be the TOC for just this {\bf subsection}:
\secttoc

\subsubsection{Thing 1}
\subsubsection{Thing 2}
\subsubsection{Thing 3}

\subsection{Large Subsection 2}

This should be the TOC for just this {\bf subsection}:
\secttoc

\subsubsection{Thing A}
\subsubsection{Thing B}
\subsubsection{Thing C}

\end{document}

enter image description here

1 Answer 1

13

You could try etoc. I don't know if using minitoc at the same time could lead to some clash elsewhere in the document. For this test, commenting out all the minitoc commands has no impact on the result, but I left them in case the full document needs the minitoc functionality.

Edit: I have added a macro to set the right margin for table of contents as typeset by the standard document classes. Initially I had put the TOC in a minipage, but another method is needed to allow pagebreaks.

note: my edit went through successive stages, as the implementation was buggy. But I think it's essentially ok now.

Edit2: I have added another, completely different way to get margins as wished for table of contents.

\documentclass{article}
\usepackage[layout=letterpaper,margin=1in]{geometry}

\usepackage{fancyhdr}

\usepackage{minitoc}         % left in case it is needed elsewhere
\setcounter{secttocdepth}{5} % idem

\usepackage{etoc}

% macro to set up a right margin for a TOC typeset
% with the standard document classes.
% A simpler method would be to put the TOC in a minipage,
% but then it could not extend across pages.
% The hack is compatible with the use of hyperref.
% Put the definition in the preamble and use the
% command in the *body* of the document.
% example:
%     \SetTOCrightmargin{.1\linewidth}
%     \tableofcontents
\makeatletter
\newif\ifTOC@marginpatched
\newcommand{\SetTOCrightmargin}[1]{%
\ifTOC@marginpatched\else
\let\old@pnumwidth\@pnumwidth
\let\old@dottedtocline\@dottedtocline
\def\@dottedtocline##1##2##3##4##5{%
    \old@dottedtocline{##1}{##2}{##3}{##4}{##5\hskip\TOC@rightmargin}}
\let\old@l@part\l@part
\def\l@part##1##2{\old@l@part{##1}{##2\hskip\TOC@rightmargin}}
% For the book/report class, replace all three occurences of 
% section by chapter in the next two lines
\let\old@l@section\l@section
\def\l@section##1##2{\old@l@section{##1}{##2\hskip\TOC@rightmargin}}
\fi
\TOC@marginpatchedtrue
\edef\TOC@rightmargin{#1}%
\dimen0=\old@pnumwidth\relax
\advance\dimen0 by #1\relax
\edef\@pnumwidth{\the\dimen0}}
\makeatother

\begin{document}

\dosecttoc                   % idem
\faketableofcontents         % idem

\fancyfoot[L]{Revision 0.2.3 -- Dec 18, 2012}
\pagestyle{fancyplain}

\section{Small Section}
\section{Huge Section}

\subsection{Large Subsection 1}

This should be the TOC for just this {\bf subsection}:
%\secttoc

% this is a quick try (much perfectible) to emulate the style
% of the minitocs as displayed in the OP message.

\etocsettocstyle{\subsection*{\contentsname}\hrule\smallskip
\begin{minipage}{.95\linewidth}}
{\end{minipage}\medskip\hrule}

\localtableofcontents

\subsubsection{Thing 1}
\subsubsection{Thing 2}
\subsubsection{Thing 3}

\subsection{Large Subsection 2}

This should be the TOC for just this {\bf subsection}:
%\secttoc

% when using \SetTOCrightmargin, we use a simpler \etocsetstyle:
\etocsettocstyle
    {\subsection*{\contentsname}\hrule\smallskip}
    {\nobreak\medskip\hrule\bigskip}

\SetTOCrightmargin{.05\linewidth} % or any desired length

\localtableofcontents

\subsubsection{Thing A}
\subsubsection{Thing B}
\subsubsection{Thing C}

% we can then also print the complete table of contents
\tableofcontents

% with etoc we can do it many times
\SetTOCrightmargin{.5\linewidth} % the right margin is now .5\linewidth
\tableofcontents
\end{document}

I first had used a minipage to get a quick solution to the problem of setting a right margin to the TOC lines. But this has the defect that the TOC then can not extend across pages. I then proposed another way, without this defect, but it was only for the local table of contents as in the above minimal example.

\etocsettocstyle
    {\subsection*{\contentsname}\hrule\smallskip
        \everypar{\rightskip.1\linewidth}}
    {\nobreak\medskip\hrule\bigskip}

This quickly breaks for more general things, so I have added the macro \SetTOCrightmargin which gives a general solution. When using this macro, one can simply set:

\etocsettocstyle
    {\subsection*{\contentsname}\hrule\smallskip}
    {\nobreak\medskip\hrule\bigskip}

The image below is not updated but if you typeset the code you will see how the two complete tables of contents do have the correct right margin, and the second one does break at the bottom of page 1.

The macro has nothing to do with package etoc: you can use it in the standard document classes. To keep the effect of \SetTOCrightmargin local, one can put it (together with the next table of contents of course) in a group. However its first use must must be at the top level.

use of etoc

The macro above \SetTOCrightmargin can be used in the standard article class without any package. Here is now another, simpler looking macro, which however requires the etoc package. Also, it allows to set the left margin too. This macro is only to be used when etoc is in its 'compatibility' mode, which means it typesets the TOC entries exactly as they would be in its absence.

The novel \etocsettocmargins has three arguments. The first one is optional, it allows to cancel the intrinsic right shift of the entries in a local table of contents: choose 1.5em in a section, 3.8em in a subsection. The default is 0pt and is suitable for a global TOC, or for the local TOC of a document part. Then there are two mandatory arguments, the first is the desired left margin, and the second the desired right margin (meaning that the page numbers are just to the left of this right margin).

In the course testing I have discovered an incompatibility when minitoc and etoc are used together and the document contains \part commands. So here I have entirely removed all the minitoc things, just to show what happens when one uses parts in the document.

\documentclass{article}
\usepackage[layout=letterpaper,margin=1in]{geometry}
%\usepackage[linktoc=all]{hyperref}

\usepackage{fancyhdr}

\usepackage{etoc}

\newcommand{\etocsettocmargins}[3][0pt]{%
\renewcommand{\etocaftertitlehook}
  {\let\oldpar\par\def\par{%
   \advance\leftskip by -#1\relax
   \advance\leftskip by #2\relax
   \advance\rightskip by #3\relax\oldpar}}}

\newcommand{\etocresettocmargins}{%
        \renewcommand{\etocaftertitlehook}{\relax}}

\begin{document}

\fancyfoot[L]{Revision 0.2.3 -- Dec 18, 2012}
\pagestyle{fancyplain}

\etocsettocmargins{.1\linewidth}{.1\linewidth}
\etocsettocstyle
    {\section*{\hskip.1\linewidth\contentsname}}
    {}

\tableofcontents

\part{FIRST PART}
\section{Small Section but which has a long name to see if it works, actually it
has a very very long name}

\part{SECOND PART}

\section{Huge Section}

Here is the TOC for this section:

\etocsettocstyle{\centering\subsection*{Contents of this section}}{}
\etocsettocmargins[1.5em]{.275\linewidth}{.275\linewidth}

\localtableofcontents

\subsection{Large Subsection 1}

This should be the TOC for just this {\bf subsection}. Its entries are centered
with the left and right margins and equal to 1/20th of the line width.

\etocsettocmargins[3.8em]{.05\linewidth}{.05\linewidth}
\etocsettocstyle
    {\subsubsection*{Contents of this subsection}\hrule\smallskip}
    {\nobreak\medskip\hrule\bigskip}

\localtableofcontents

\subsubsection{Thing 1}
1
\subsubsection{Thing 2}
2
\subsubsection{Thing 3 also has a very very very long name even longer than
  would be reasonable let's make it even longer}
3

\subsection{Large Subsection 2}

This should be the TOC for just this {\bf subsection}. The technique of the
minipage is the simplest:

\begin{center}
\begin{minipage}[c]{0.6\linewidth}
 \etocsettocmargins[3.8em]{0pt}{0pt}
 \localtableofcontents
\end{minipage}
\end{center}

\subsubsection{Thing A also has a very very very long name even longer than
  would be reasonable and truly it is a very very long name}
A
\subsubsection{Thing B}
B
\subsubsection{Thing C}
C

\end{document}

first page of second method second page of second method

4
  • Perfect -- exactly what I needed, thanks so much!
    – Pat
    Dec 20, 2012 at 23:59
  • @Pat you're welcome! I added a better way to get the toc lines have some right margins.
    – user4686
    Dec 21, 2012 at 8:14
  • Wasn't an issue yet, but I updated things just in case -- thanks. You may want to update your post that to indicate that the new style only works for the local ToC, applying it to a \tableofcontents yeilds: imgur.com/GwxNI I'm guessing it has to do with the \parskip not being applied to every element consistently. (Or I'm leading you down a blind alley, my Latex-fu is not yet that strong..)
    – Pat
    Dec 22, 2012 at 9:13
  • @Pat: thanks for pointing out the inadequacy of the \everypar trick in a case more general than the one it initially addressed; I will try to think of a more robust way.
    – user4686
    Dec 22, 2012 at 9:59

You must log in to answer this question.

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