将章节添加到 minitoc 中,但不将它们放在实际目录中

将章节添加到 minitoc 中,但不将它们放在实际目录中

我修复了这个问题,但我不知道它为什么会起作用。请参阅最后的评论以了解后续问题。

我找到了几个类似的问题,但没有一个是完全一致的(或似乎有效)。但是,如果这是我的 Google 功力失败了,请直接链接到相关问题。

我正在尝试编写一份指南,并在末尾包含常见问题解答。我认为编写常见问题解答的最自然方式是使用 hyperref 包和 minitoc,让人们阅读问题并单击以找到答案。不幸的是,这会使指南顶部的目录被所有常见问题解答问题弄得乱七八糟。一定有一种方法可以隐藏目录中的常见问题解答问题,但仍在 minitoc 中显示它们。我尝试了\addstarredsectionminitoc 包文档中的命令,但我甚至无法正确编译它,更不用说做我想要的事情了。有什么建议吗?

MWE(包括我现在打算使用的所有包):

\documentclass{article}

\usepackage{hyperref}
\hypersetup{%               %           Setup the coloring of the links. 
%                           %           Currently the only necessary one is "colorlinks=true" and "linkcolor=blue".
    colorlinks   = true,    %           Colours links instead of ugly boxes
    urlcolor     = blue,    %           Colour for external hyperlinks
    linkcolor    = blue,    %           Colour of internal links
    citecolor    = blue     %           Colour of citations, could be ``red''
    }

\usepackage[
    margin=1in,%            All 4 margins need to be one inch.
    paperheight=11in,%      We want 11in tall paper.
    paperwidth=8.5in%       And 8.5in wide paper.
    ]{geometry}%            Geometry package is the easiest way to deal with margins.

\usepackage{fancyhdr}
\usepackage{indentfirst}
\pagestyle{fancy}
\usepackage{titlesec}%          Use this for the actual header styling
\usepackage{titletoc}%          Use this to manipulate Table of Contents styling
\usepackage{minitoc}% To form a miniture table of contents for the FAQ.
\title{A guide to Stuff}
\author{Me}
\date{}

\begin{document}
\maketitle
\newpage
\section*{Introduction}

\newpage
\doparttoc\tableofcontents
\newpage

\part{Part 1}
\newpage

\section{Subsection of Part 1}
\newpage

\section{Second Subsection of Part 1}

\newpage
\part{Part 2}
\newpage

\section{Subsection of Part 2}
\newpage

\section{Second Subsection of Part 2}

\newpage
\part{FAQ}
% Every section (or subsection) below this should appear in the minitoc but NOT the main table of contents. Bonus points for being able to show *only* sections and not subsections, although this should simply be a matter of setting the toc depth in theory.
\parttoc
\section*{Question One}
%\addstarredsection{Question One}
    Q1
\section{Question Two}
    Q2
    \subsection{Subquestion of Question Two}
        SubQ1
    \subsection{Another subquestion of Question Two}
        SubQ2
\section{Question Three}
    Q3
\end{document}

作为脚注,我想使用 titletoc 和 titlesec 包是出于其他原因,而且我看过一些关于如何使用 titletoc 来获得 minitoc 而不使用 minitoc 包的帖子。我也尝试了这些建议,但无法让任何建议的答案进行编译;甚至复制/粘贴线程中的答案作为试金石。公平地说,那些帖子似乎很旧,所以我想这是 LaTeX 和/或 titletoc 包更新的问题。我也早早放弃了这些尝试,因为我知道 minitoc 或多或少已经做了我想要的事情,而且无论如何我都想学习如何更好地使用 minitoc。

编辑:

\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}我通过在行后插入来修复该问题\part{FAQ}。我知道这会将目录深度设置为不再与目录的部分(或更低部分)相对应,因此我期望(并且确实如此)顶部目录在该FAQ部分之后没有任何内容。

我不确定的是,为什么 minitoc 仍能正确填充所有部分,而这些部分却没有显示在顶部目录中。如果目录深度不影响 minitoc,那什么会影响呢?我认为 minitoc 解析了文件.toc以获取其内容,因此目录中没有的内容将很难出现在 minitoc 中……但显然事实并非如此。有人能提供关于这里发生了什么的见解吗?

答案1

以下是基于该帖子的一种方法:

如何在每个 \chapter 命令后添加自定义 TOC(自动“章节细分”)?

\documentclass{article}

\usepackage{hyperref}
\hypersetup{%               %           Setup the coloring of the links. 
%                           %           Currently the only necessary one is "colorlinks=true" and "linkcolor=blue".
    colorlinks   = true,    %           Colours links instead of ugly boxes
    urlcolor     = blue,    %           Colour for external hyperlinks
    linkcolor    = blue,    %           Colour of internal links
    citecolor    = blue     %           Colour of citations, could be ``red''
    }

\usepackage[
    margin=1in,%            All 4 margins need to be one inch.
    paperheight=11in,%      We want 11in tall paper.
    paperwidth=8.5in%       And 8.5in wide paper.
    ]{geometry}%            Geometry package is the easiest way to deal with margins.

\usepackage{fancyhdr}
\usepackage{indentfirst}
\pagestyle{fancy}
%\usepackage{titlesec}%          Use this for the actual header styling
%\usepackage{titletoc}%          Use this to manipulate Table of Contents styling
\usepackage{minitoc}% To form a miniture table of contents for the FAQ.


%koleygr: ADDED CODE
\usepackage{pgffor}
%\newcounter{mychapter}

%\renewcommand\thepart{\Alph{part}}
%\newcommand\thechapter{\thepart.\arabic{mychapter}}

%\renewcommand\thesection{\thechapter.\arabic{section}}
%\renewcommand\thesubsection{\thesection.\arabic{subsection}}


\let\oldpart\part
%\def\part#1{\oldpart{#1}\setcounter{mychapter}{0}\refstepcounter{mychapter}}


\let\oldsection\section
\let\oldsubsection\subsection
%\let\olldchapter\chapter
%\def\oldchapter{\stepcounter{mychapter}\olldchapter}
\makeatletter

\newcounter{mysection}
\newcounter{mysubsection}



\def\section{%
\@ifstar{\@Starred}{\@nonStarred}%
}
\def\@Starred{%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred section can not have parameters. I am going to ignore them!}\@StarredWith}%
{\@StarredWithout}%
}      
\def\@StarredWith[#1]#2{%
\oldsection*{#2}\refstepcounter{mysection}\label{Sec:\arabic{mysection}}%
}
\def\@StarredWithout#1{
\oldsection*{#1}\refstepcounter{mysection}\label{Sec:\arabic{mysection}}%
}
\def\@nonStarred{%
\@ifnextchar[%
{\@nonStarredWith}%
{\@nonStarredWithout}%
}
\def\@nonStarredWith[#1]#2{%
\oldsection[#1]{#2}\refstepcounter{mysection}\label{Sec:\arabic{mysection}}%
}
\def\@nonStarredWithout#1{%
\oldsection{#1}\refstepcounter{mysection}\label{Sec:\arabic{mysection}}%
}


\def\subsection{%
\@ifstar{\@StarredS}{\@nonStarredS}%
}
\def\@StarredS{%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred section can not have parameters. I am going to ignore them!}\@StarredSWith}%
{\@StarredSWithout}%
}      
\def\@StarredSWith[#1]#2{%
\oldsubsection*{#2}\refstepcounter{mysubsection}\label{SubSec:\arabic{mysection}:\arabic{mysubsection}}%
}
\def\@StarredSWithout#1{%
\oldsubsection*{#1}\refstepcounter{mysubsection}\label{SubSec:\arabic{mysection}:\arabic{mysubsection}}%
}
\def\@nonStarredS{%
\@ifnextchar[%
{\@nonStarredSWith}%
{\@nonStarredSWithout}%
}
\def\@nonStarredSWith[#1]#2{%
\oldsubsection[#1]{#2}\refstepcounter{mysubsection}\label{SubSec:\arabic{mysection}:\arabic{mysubsection}}%
}
\def\@nonStarredSWithout#1{%
\oldsubsection{#1}\refstepcounter{mysubsection}\label{SubSec:\arabic{mysection}:\arabic{mysubsection}}%
}

%\MakeRobust{\ref}


\newcommand{\labeltext}[2]{%
\immediate\write\@auxout{%
  \string\newlabel{#2}{{1}{\thepage}{\unexpanded{#1}}{#2}{}}%
}%
}

\newcommand\stopTOC{%
\def\section{%
\@ifstar{\@Starred}{\@nonStarred}%
}
\def\@Starred{%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred section can not have parameters. I am going to ignore them!}\@StarredWith}%
{\@StarredWithout}%
}      
\def\@StarredWith[##1]##2{%
\oldsection*{##2}\refstepcounter{mysection}\labeltext{##2}{Sec:\arabic{mysection}}%
}
\def\@StarredWithout##1{
\oldsection*{##1}\refstepcounter{mysection}\labeltext{##1}{Sec:\arabic{mysection}}%
}
\def\@nonStarred{%
\@ifnextchar[%
{\@nonStarredWith}%
{\@nonStarredWithout}%
}
\def\@nonStarredWith[##1]##2{%
\oldsection*{\arabic{section}\hspace{1em}{##2}}\refstepcounter{mysection}\labeltext{\refstepcounter{section}\arabic{section}\hspace{1em}##2}{Sec:\arabic{mysection}}%
}
\def\@nonStarredWithout##1{%
\oldsection*{\arabic{section}\hspace{1em}{##1}}\refstepcounter{mysection}\labeltext{\refstepcounter{section}\arabic{section}\hspace{1em}##1}{Sec:\arabic{mysection}}%
}


\def\subsection{%
\@ifstar{\@StarredS}{\@nonStarredS}%
}
\def\@StarredS{%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred section can not have parameters. I am going to ignore them!}\@StarredSWith}%
{\@StarredSWithout}%
}      
\def\@StarredSWith[##1]##2{%
\oldsubsection*{##2}\refstepcounter{mysubsection}\label{SubSec:\arabic{mysection}:\arabic{mysubsection}}%
}
\def\@StarredSWithout##1{%
\oldsubsection*{##1}\refstepcounter{mysubsection}\label{SubSec:\arabic{mysection}:\arabic{mysubsection}}%
}
\def\@nonStarredS{%
\@ifnextchar[%
{\@nonStarredSWith}%
{\@nonStarredSWithout}%
}
\def\@nonStarredSWith[##1]##2{%
\oldsubsection*{\arabic{section}.\arabic{subsection}\hspace{1em}##2}\refstepcounter{mysubsection}\labeltext{\refstepcounter{subsection}\arabic{section}.\arabic{subsection}\hspace{1em}##2}{SubSec:\arabic{mysection}:\arabic{mysubsection}}%
}
\def\@nonStarredSWithout##1{%
\oldsubsection*{\arabic{section}.\arabic{subsection}\hspace{1em}##1}\refstepcounter{mysubsection}\labeltext{\refstepcounter{subsection}\arabic{section}.\arabic{subsection}\hspace{1em}##1}{SubSec:\arabic{mysection}:\arabic{mysubsection}}%
}
}


%SectionBreakDown

\newcounter{secc}
\newcounter{ssecc}
\newcounter{tssecc}
\xdef\OverC{0}


\newcommand\SBD[2][1,-1]{%
  \xdef\OverC{0}%
  \def\Continued\empty%
  \def\ToBeContinued{-1}%
  \foreach \opt[count=\ll from 1] in {#1}{%
    \ifnum\ll=1
    \xdef\Continued{\opt}%
    \else
    \xdef\ToBeContinued{\opt}%
    \fi
  }%
  \ifx\ToBeContinued\empty
  \xdef\ToBeContinued{-1}%
  \fi
  \ifx\Continued\empty
  \xdef\Continued{1}%
  \fi
%
%\section{Section breakdown}

\ifnum\Continued>0
\else
\noindent{\large\bfseries Contents}%

\noindent\rule{\textwidth}{2pt}\vspace{0.5cm}
\setcounter{secc}{0}%
\setcounter{ssecc}{0}%
\setcounter{tssecc}{0}%
\fi
%
{\noindent #2}\par
%
%
\ifnum\ToBeContinued<0
\def\ContinueUntil{100}%
\else
\def\ContinueUntil{\ToBeContinued}%
\fi
%
\foreach \mk in {\Continued,...,\ContinueUntil}{%
  \@ifundefined{r@Sec:\mk}{%
    \breakforeach%
  }{%
    \setcounter{secc}{\mk}%
    \ifnum\mk<0
      \breakforeach%
    \else
    \ifnum\mk>\ContinueUntil
    \\\breakforeach%
    \else
         \noindent{\bfseries{\nameref{Sec:\mk}\dotfill\pageref{Sec:\mk}}\\[1.2ex]}
         \foreach \ml in {1,...,100}{%
            \@ifundefined{r@SubSec:\mk:\ml}{%
               \breakforeach%
            }{%
               \setcounter{ssecc}{\ml}%
               \stepcounter{tssecc}%
               \ifnum\mk>0
                     \hspace{0.3cm}\parbox[t]{\textwidth}{\setlength{\baselineskip}{4pt}\nameref{SubSec:\mk:\ml}\dotfill\pageref{SubSec:\mk:\ml}\\[0.9ex]}\\
                \fi
            }%
        }%
        \fi
    \fi
  }%
}
%\begin{enumerate}[label={\arabic{enumi}.},itemsep=0pt]
%\foreach \i in {#3,...,#4}
%{\item \nameref{\thechapter:\i}}
%\end{enumerate}

\ifnum\ToBeContinued>0
\relax%
\else
\vspace{-0.cm}
\par
\noindent\rule{\textwidth}{2pt}
\fi
}
\makeatother

%koleygr: END ADDED CODE

\title{A guide to Stuff}
\author{Me}
\date{}

\begin{document}
\maketitle
\newpage
\section*{Introduction}

\newpage
\doparttoc\tableofcontents
\newpage

\part{Part 1}
\newpage

\section{Subsection of Part 1}
\newpage

\section{Second Subsection of Part 1}

\newpage
\part{Part 2}
\newpage

\section{Subsection of Part 2}
\newpage

\section{Second Subsection of Part 2}

\newpage
\part{FAQ}
\stopTOC
\SBD[0,1]{}
\SBD[7,-1]{}
%\parttoc
\section*{Question One}
%\addstarredsection{Question One}
    Q1
\section{Question Two}
    Q2
    \subsection{Subquestion of Question Two}
        SubQ1
    \subsection{Another subquestion of Question Two}
        SubQ2
\section{Question Three}
    Q3
\end{document}

目录:

在此处输入图片描述

第 3 部分:

在此处输入图片描述

相关内容