从预生成的 .toc 文件中获取格式化列表

从预生成的 .toc 文件中获取格式化列表

我有一个从另一个 LaTeX 文件(例如 A)预先生成的 .toc 文件memoir。除其他外,此 LaTeX 文件使用hyperref(即,\contentsline现在有四个参数)。从此 .toc 文件中,我需要生成一个格式化列表其他LaTeX 文件(例如 B)。章节和部分名称将不显示任何页码。部分名称将以逗号分隔的列表。

为了实现上述目标,我尝试调整各种参数和宏。请参见下文,希望评论能够展示我迄今为止所做的尝试:

\documentclass{memoir}

\usepackage{filecontents}

% Hide the links
\usepackage[colorlinks=false,hidelinks]{hyperref}

\begin{filecontents*}{\jobname.tocin.tex}
\contentsline {chapter}{Contents}{1}{section*.1}
\contentsline {chapter}{\chapternumberline {1}One Chapter}{3}{chapter.1}
\contentsline {section}{\numberline {1.1}A Section}{3}{section.1.1}
\contentsline {subsection}{AA}{3}{section*.2}
\contentsline {section}{\numberline {1.2}B}{3}{section.1.2}
\contentsline {section}{\numberline {1.3}C}{3}{section.1.3}
\contentsline {chapter}{\chapternumberline {2}Another Chapter}{5}{chapter.2}
\contentsline {section}{\numberline {2.1}D}{5}{section.2.1}
\contentsline {section}{\numberline {2.2}E}{5}{section.2.2}
\contentsline {section}{\numberline {2.3}G}{5}{section.2.3}
\contentsline {section}{\numberline {2.4}H}{5}{section.2.4}
\end{filecontents*}

% Suppres dots
\renewcommand*{\cftsectionleader}{\hfill}

% Suppress section numbers 
\renewcommand{\numberline}[1]{}

% Suppress page numbers
\renewcommand*{\cftchapterformatpnum}[1]{}
\renewcommand*{\cftsectionformatpnum}[1]{}

% Show upto sections
\maxtocdepth{section}

% Redefine \l@section to control appearance of section names
\makeatletter
\def\l@section#1#2{\ifnum \@nameuse{c@tocdepth} > 0\relax \vskip \@nameuse {cftbeforesectionskip} 
  {\newcommand*\cftwhatismyname {section}\memRTLleftskip 
    \@nameuse {cftsectionindent}\relax\memRTLrightskip \@tocrmarg \parfillskip -\memRTLrightskip \parindent 
    \@nameuse {cftsectionindent}\relax\@afterindenttrue \interlinepenalty \@M \leavevmode 
    \settowidth {\@tempdima }{\@nameuse {cftsectionfont}{\@nameuse {cftsectionname}}}
    \addtolength {\@tempdima }{\@nameuse {cftsectionnumwidth}}
    \expandafter \let \expandafter \@cftbsnum \csname cftsectionpresnum\endcsname 
    \expandafter \let \expandafter \@cftasnum \csname cftsectionaftersnum\endcsname 
    \expandafter \let \expandafter \@cftasnumb \csname cftsectionaftersnumb\endcsname 
    \expandafter \let \expandafter \@cftn@me \csname cftsectionname\endcsname 
    \advance \memRTLleftskip \@tempdima \null \nobreak \hskip -\memRTLleftskip 
    {\@nameuse {cftsectionfont}{#1,}}\nobreak \@nameuse {cftsectionfillnum}{#2}}
  \fi}
\makeatother

\begin{document}

\makeatletter
\@input {\jobname.tocin.tex}
\makeatother

\end{document}

输出是一个没有章节或页码的列表。

在此处输入图片描述

但是,我想让部分名称列表以逗号分隔,水平排列,如下所示。

1 One Chapter
  A Section, B, C

2 Another Chapter
  D, E, G, H

如您所见,我尝试的解决方案至少存在两个问题:

  1. 该列表不是水平的。
  2. 我需要删除列表中最后一个部分名称后的逗号。

由于目前的情况,修改 LaTeX 文件 A 的 .toc 不是一个选择。

答案1

好吧,现在又多了一些巫术……为了把逗号放在我们想要的位置,我们将章节标题保存在宏中\lastsection,并且只有在读取下一个标题后才用逗号将它们推出;最后一个标题在下一行被推出时没有逗号chapter

这就要求我们\lastsection在文档末尾插入一个来排版最后的\contentsline{section}{...},否则它会被遗忘。

\documentclass{article}

\usepackage{ifthen, hanging}

\setlength{\parskip}{0.5\baselineskip}
\newlength{\chapindent}
\setlength{\chapindent}{0.25in}
\setlength{\parindent}{\chapindent}

\newcommand{\lastsection}{}
\newcommand{\chapternumberline}[1]{\makebox[\chapindent][l]{#1}}
\renewcommand{\numberline}[1]{}

\renewcommand{\contentsline}[4]{%
\ifthenelse{\equal{#1}{chapter}}{\lastsection\renewcommand{\lastsection}{}%
\par\noindent{#2}}%
     {\ifthenelse{\equal{#1}{section}}{%
     \ifthenelse{\equal{\lastsection}{}}{\par\hangpara{\chapindent}{-100}}{\lastsection,\hspace{\chapindent}}%
     \renewcommand{\lastsection}{#2}}{}}%
\ignorespaces}

\begin{document}

\contentsline {chapter}{Contents}{1}{section*.1}%
\contentsline {chapter}{\chapternumberline {1}One Chapter}{3}{chapter.1}
\contentsline {section}{\numberline {1.1}A Section}{3}{section.1.1}
\contentsline {section}{\numberline {1.2}A Section}{3}{section.1.1}
\contentsline {section}{\numberline {1.3}A Section}{3}{section.1.1}
\contentsline {section}{\numberline {1.4}A Section}{3}{section.1.1}
\contentsline {section}{\numberline {1.5}A Section}{3}{section.1.1}
\contentsline {section}{\numberline {1.6}A Section}{3}{section.1.1}
\contentsline {section}{\numberline {1.7}A Section}{3}{section.1.1}
\contentsline {subsection}{AA}{3}{section*.2}
\contentsline {section}{\numberline {1.8}B}{3}{section.1.2}
\contentsline {section}{\numberline {1.9}C}{3}{section.1.3}
\contentsline {chapter}{\chapternumberline {2}Another Chapter}{5}{chapter.2}
\contentsline {section}{\numberline {2.1}D}{5}{section.2.1}
\contentsline {section}{\numberline {2.2}E}{5}{section.2.2}
\contentsline {section}{\numberline {2.3}G}{5}{section.2.3}
\contentsline {section}{\numberline {2.4}H}{5}{section.2.4}
% push out the last section heading 
\lastsection

\end{document}

在此处输入图片描述

答案2

解决方案是将章节名称文本存储在宏中,并在每章之后一起呈现。切换开关帮助我们跟踪,包括元素间的逗号。


\documentclass{memoir}

\usepackage{filecontents}

\begin{filecontents*}{\jobname.tocin.tex}
\contentsline {chapter}{Contents}{1}{section*.1}
\contentsline {chapter}{\chapternumberline {1}One Chapter}{3}{chapter.1}
\contentsline {section}{\numberline {1.1}A}{3}{section.1.1}
\contentsline {subsection}{AA1}{3}{section*.2}
\contentsline {subsection}{AA2}{3}{section*.3}
\contentsline {section}{\numberline {1.2}B}{3}{section.1.2}
\contentsline {section}{\numberline {1.3}C}{3}{section.1.3}
\contentsline {chapter}{\chapternumberline {2}Another Chapter}{5}{chapter.2}
\contentsline {chapter}{\chapternumberline {3}Yet Another Chapter}{7}{chapter.3}
\contentsline {chapter}{\chapternumberline {4}And Yet Another Chapter}{9}{chapter.4}
\contentsline {section}{\numberline {4.1}Definitely a Long Section Name: Does It Work Correctly?}{9}{section.4.1}
\contentsline {section}{\numberline {4.2}Definitely We Make It a Longer Section Name: Does It Also Work Correctly?}{9}{section.4.2}
\contentsline {section}{\numberline {4.3}E}{9}{section.4.3}
\contentsline {section}{\numberline {4.4}G}{9}{section.4.4}
\contentsline {section}{\numberline {4.5}H}{9}{section.4.5}
\end{filecontents*}

% Suppres dots
\renewcommand*{\cftsectionleader}{\hfill}

% Suppress section numbers 
\renewcommand{\numberline}[1]{}

% Suppress page numbers
\renewcommand*{\cftchapterformatpnum}[1]{}
\renewcommand*{\cftsectionformatpnum}[1]{}

% Show upto sections
\maxtocdepth{section}

% Section indents
\cftsetindents{section}{0.0mm}{0.0mm}

% Redefine \contentsline. Retain the original definition somewhere for
% later use
\makeatletter
\RequirePackage{etoolbox}
\newtoggle{sectionst@rted}
\togglefalse{sectionst@rted}
\let\memoir@contentsline\contentsline
\gdef\s@vedsections{}
\def\contentsline#1#2#3#4{%
  % Chapter?
  \ifstrequal{#1}{chapter}
  {
    % If saved section name contents is not empty, render these. These
    % texts are actually from previous chapter
    \ifx\s@vedsections\empty\else
    \memoir@contentsline{section}{\s@vedsections}{}{}\fi
    % Render chapter name text
    \memoir@contentsline{#1}{#2}{}{}
    % Toggle switch for sections previously started
    \global\togglefalse{sectionst@rted}
    % Empty the section name contents repository
    \gdef\s@vedsections{}}{}
  % Section?
  \ifstrequal{#1}{section}
  {
    % Are we already rendering sections?
    \iftoggle{sectionst@rted}
    % Already rendering section true
    {\g@addto@macro\s@vedsections{, #2}}
    % Already rendering section false
    {\global\toggletrue{sectionst@rted}
      \g@addto@macro\s@vedsections{#2}}}{}}
\makeatother


\begin{document}

\makeatletter
\@input {\jobname.tocin.tex}
% Render section text list for the very last chapter
\ifx\s@vedsections\empty\else
\memoir@contentsline{section}{\s@vedsections}{}{}\fi
\makeatother

\end{document}

在此处输入图片描述

答案3

强制性埃托克解决方案。

\documentclass{memoir}

\usepackage{filecontents}
\begin{filecontents*}{\jobname.tocin.tex}
\contentsline {chapter}{Contents}{1}{section*.1}
\contentsline {chapter}{\chapternumberline {1}One Chapter}{3}{chapter.1}
\contentsline {section}{\numberline {1.1}A Section}{3}{section.1.1}
\contentsline {subsection}{AA}{3}{section*.2}
\contentsline {section}{\numberline {1.2}B}{3}{section.1.2}
\contentsline {section}{\numberline {1.3}C}{3}{section.1.3}
\contentsline {chapter}{\chapternumberline {2}Another Chapter}{5}{chapter.2}
\contentsline {section}{\numberline {2.1}D}{5}{section.2.1}
\contentsline {section}{\numberline {2.2}E}{5}{section.2.2}
\contentsline {section}{\numberline {2.3}G}{5}{section.2.3}
\contentsline {section}{\numberline {2.4}H}{5}{section.2.4}
\end{filecontents*}

% Hide the links
\usepackage[colorlinks=false,hidelinks]{hyperref}

% Show upto sections
\maxtocdepth{section}

\usepackage{etoc}

\begin{document}

\tableofcontents

\chapter{First chapter of master doc}

\section{A section in master doc}

\chapter{Second chapter of master doc}

\section{A section in second chapter of master doc}

Here is now the table of contents of the other file. The first heading
``Contents'' comes from that file, actually.

\begingroup
\makeatletter

% Suppress dots
\renewcommand*{\cftsectionleader}{\hfill}

% Suppress section numbers 
\renewcommand{\numberline}[1]{}

% Suppress page numbers
\renewcommand*{\cftchapterformatpnum}[1]{}
\renewcommand*{\cftsectionformatpnum}[1]{}

\let\original@l@chapter\l@chapter
\let\original@chapternumberline\chapternumberline

\etocsetstyle{chapter}
   {}
   {\original@l@chapter
    {\original@chapternumberline{\etocthenumber}\etocthename}{\etocthepage}}
   {}
   {}
\etocsetstyle{section}
   {\normalfont}
   {\etociffirst{\noindent}{, }\etocthename}
   {}
   {\par}

% Trick etoc into using other file
{\newread\Etoc@tf
 \openin\Etoc@tf \jobname.tocin.tex
 \global\Etoc@toctoks{}%
 \endlinechar=\m@ne
 \Etoc@readtoc
 \global\Etoc@toctoks=\expandafter{\the\Etoc@toctoks}%
 \closein\Etoc@tf}

% adjust as desired
\etocsettocstyle{}{\bigskip}

\etocsetnexttocdepth{section}

\tableofcontents* % the star is to avoid an entry in the main TOC
\endgroup

We will now proceed with our regular radio address.

\end{document}

在此处输入图片描述

相关内容