如何在部分级别创建自定义“...列表”?

如何在部分级别创建自定义“...列表”?

我有一篇很长的文档,在需要按部分进行索引的环境中经常使用。我知道tocloft包允许创建自定义“列表...”我还知道minitoc包允许按部分、按章或按节列出目录。

但是我需要一个基于每个部分出现的自定义“列表”,即两个功能。我想我有一个想法如何实现这一点:环境的每个实例都会将一些内容写入特定于该部分的辅助文件,并在该部分的开头读取自定义辅助文件,写入该列表。但在我开始破解之前,我想知道这个轮子是否以前被发明过,而我不知道。

答案1

参见包中的“7.4.部分列表” titletoc,它可能就是您想要的。

我猜也许minitoc这个问题本身也能解决。但我不确定。


对于用户定义的浮点数,我只能破解titletoc。幸运的是,这并不难。我认为应该将此功能添加到中titletoc

\documentclass{article}
\usepackage{float,titletoc}
\newfloat{myflt}{htbp}{mine}
\makeatletter
\def\ttl@partialmine{pmine}
\def\ttl@writepartial#1#2{%
  \ttl@topartial{toc}{#1}{#2}%
  \ttl@topartial{lof}{#1}{#2}%
  \ttl@topartial{lot}{#1}{#2}%
  \ttl@topartial{mine}{#1}{#2}%
  \ttl@writefile{#1}{#2}}
\makeatother

\newcommand{\listofmyflt}{\listof{myflt}{Full list}}

\newcommand{\partiallistofmyflt}{
  \startlist{mine}%
  \printlist{mine}{}{\subsection*{Partial list}}}

\begin{document}
\listofmyflt

\section{foo}
\partiallistofmyflt

\begin{myflt}\caption{foo}\end{myflt}


\section{bar}
\partiallistofmyflt


\begin{myflt}\caption{bar}\end{myflt}
\begin{myflt}\caption{baz}\end{myflt}

\stoplist{mine}
\end{document}

答案2

我在下面提供了一个版本,说明如何在非浮动环境中实现这一点。它将写入文件 \jobname.prb;有些人可能认为这是一个缺点,因为它使用了 TeX 的一个写入寄存器。它需要 2 次编译(与任何其他基于 toc 的命令一样)。我欢迎任何反馈。

\documentclass{article}
\usepackage{ifthen}
\usepackage{hyperref}
\usepackage{xstring}

\newcounter{probSectCounter}
\setcounter{probSectCounter}{-1}
\newcounter{echo}

% renew the section command so that it writes 
% to the \jobname.prb file
\let\oldsection\section
\renewcommand\section{%
            \stepcounter{probSectCounter}%
            \addcontentsline{prb}{section}{heading\theprobSectCounter}%
            \oldsection}

% custom environment- whatever you want
\newcounter{problem}
\newenvironment{problem}{%
    \refstepcounter{problem}%
    \textbf{Problem \theproblem }\par%
    \addcontentsline{prb}{subsection}{\theproblem}%
}%
{%
    \par\vspace{\belowdisplayskip}\noindent\ignorespacesafterend%
}

% define the \listproblems command
\newread\File
\def\listproblems{%
% open the appropriate file do everything in a 
% new environment so that we can renew the 
% \contentsline command locally 
\begin{problemlist}
\openin\File=\jobname.prb%
\loop\unless\ifeof\File%
\read\File to\fileline%
\fileline%
\repeat%
\closein\File%
\end{problemlist}
}%

\makeatletter
% need a command to bring the \contentsline
% into the problemlist environment
\let\orig@contents\contentsline%
\newenvironment{problemlist}%
{%
    % renew the \contentsline command locally in this environment
    % 
    % if we renewed the command outside of the environment, it would
    % affect \tableofcontents, \minitoc, and perhaps more- bad!
    \let\contentsline\orig@contents%
    \let\Contentsline\contentsline%
    \renewcommand\contentsline[4]{%
    \StrCount{##2}{heading}[\heading]%
    \ifthenelse{\heading>0}%
    {%
        \StrCount{##2}{heading\theprobSectCounter}[\heading]%
        \ifthenelse{\heading>0}%
        {%
            \setcounter{echo}{1}%
        }%
        {%
            \setcounter{echo}{0}%
        }%
        % the command is empty
    }%
    {%
        \ifthenelse{\equal{\theecho}{1}}%
        {%
                \Contentsline{##1}{##2}{##3}{##4}%
        }%
        {}%
    }%
    }%
}%
{}

% enable the \jobname.prb file
\def\prb@enablelistofproblems{%
\begingroup%
\makeatletter%
\if@filesw%
\expandafter\newwrite\csname tf@prb\endcsname%
\immediate\openout \csname tf@prb\endcsname \jobname.prb\relax%
\fi%
\@nobreakfalse%
\endgroup}%

% enable the \jobname.prb files at the end
% of the document
\AtEndDocument{\prb@enablelistofproblems}

\makeatother


\begin{document}

\section{intro}
Welcome to this section. 
\listproblems

\begin{problem}
In environment
\end{problem}

\begin{problem}
 In environment
\end{problem}

\section{another section}
\listproblems

\begin{problem}
 In environment
\end{problem}

\begin{problem}
 In environment
\end{problem}

\begin{problem}
 In environment
\end{problem}

\end{document}

相关内容