使用索引的解决方案

使用索引的解决方案

我正在使用 extbook 环境来创建解决方案手册。

我希望此文档有两个目录:其中一个以“传统方式”创建,即,\tableofcontents用于\section{}每个问题,并使hyperref目录可点击;另一个包含章节按主题排序。我正在编写解决方案的这组问题的问题是,问题按数字顺序排列,但具有相似数字的问题通常不是相似的主题。

举个例子,我的意思是:假设我有 3 个问题:问题 1 是关于加法的,问题 2 是关于乘法的,问题 3 是关于加法的。在传统的目录上,就像我现在拥有的那样,它会按页码对它们进行排序:问题 1 排在第一位,问题 2 排在第二位,问题 3 排在第三位。但是,我想创建另一个目录,让我可以按主题对它们进行分组:可能像这样:

添加

问题 1..... (页码)

问题 3.....(页码)

乘法

问题 2....(页码)

此外,我希望这两个目录都存在于同一个文件中。使用 LaTeX 可以实现吗?如果这是上一个问题的重复,请原谅 - 我是新手。

答案1

这个答案提供了几种可能的解决方案:第一个通过imakeidx包使用索引来按主题生成不同的划分;第二个和第三个选项使用类似 ToC 的机制:第二个用于\@starttoc生成多个 ToC(每个主题一个),第三个展示如何使用包提供的功能titletoc来实现所需的结果。

使用索引的解决方案

由于问题必须按照不同的类别进行排序,因此建议使用可用的索引功能之一。

下面我展示了一个使用imakeidx包。该示例考虑了四个不同的类别,并展示了该方法如何轻松概括。由于我们想要索引条目的特殊样式(类似于 ToC 条目),因此我们需要自定义样式,例如myright.ist下面的样式。

样式文件myright.ist

% MakeIndex style file; page numbers flushed right and dot leaders
% between entries and page numbers
delim_0  "\\dotfill "
delim_1  "\\dotfill "
delim_2  "\\dotfill "

该文件本身:

\documentclass{article}
\usepackage{amsthm}
\usepackage[
  noautomatic
]{imakeidx}

\makeatletter
\xpatchcmd{\imki@putindex}
  {\immediate\closeout}
  {\ifimki@disableautomatic\else\immediate\closeout}
  {}{}
\xpatchcmd{\imki@putindex}
  {\endcsname}
  {\endcsname\fi}
  {}{}
\makeatother

\indexsetup{level=\subsection*,noclearpage,othercode={\parskip=5pt}}

\makeindex[
  name=add,
  title=Addition,
  columns=1,
  options=-s myright
]  %% Define new index of addition
\makeindex[
  name=mul,
  title=Multiplication,
  columns=1,
  options=-s myright
]  %% Define new index of multiplication
\makeindex[
  name=exp,
  title=Exponentiation,
  columns=1,
  options=-s myright
]  %% Define new index of exponentiation
\makeindex[
  name=sub,
  title=Subtraction,
  columns=1,
  options=-s myright
]  %% Define new index of subtraction

% Definition of the auxiliary environment for the questions
\theoremstyle{definition}
\newtheorem{que}{Question}

% Definition of the mian environment for the questions
% it writes an entry to the ToC; 
% it also writes an entry in the index specified in the mandatory argument 
\let\tmpa\relax
\newenvironment{question}[2][]
  {\begin{que}[#1]
  \gdef\tmpb{#2}
  \if\relax\detokenize{#1}\relax
    \gdef\tmpa{\relax}  
  \else
    \gdef\tmpa{~#1}
  \fi
  \addcontentsline{toc}{section}{Question~\theque.\tmpa}\ignorespaces
  }
  {\index[\tmpb]{Question~\theque.\tmpa}\end{que}}

% Simply produces a heading ''List of Topics'' formatted as unnumbered section
\newcommand\listoftopics{
  \section*{List of Topics}
}

\begin{document}

\tableofcontents
\listoftopics
\printindex[add]
\printindex[mul]
\printindex[exp]
\printindex[sub]

\clearpage

\begin{question}[Commutativity]{add}
Is addition of real numbers commutative?
\end{question}

\begin{question}[Commutativity]{sub}
Is subtraction of real numbers commutative?
\end{question}

\begin{question}[Associativity]{mul}
Is multiplication of quaternions associative?
\end{question}

\begin{question}[Inverse]{mul}
Does inverses for non-negative quaternions always exist?
\end{question}

\begin{question}[General properties]{exp}
Which are the general properties for exponentiation in the field of complex numbers?
\end{question}

\begin{question}{add}
Is addition of real numbers modulative?
\end{question}

\begin{question}[Modulative]{sub}
Is subtraction of real numbers modulative?
\end{question}

\end{document}

输出显示了一般目录和按主题划分的新问题列表:

enter image description here

解释和说明

代码包含一些一般性的解释性注释。

这个想法是使用imakeidx包裹。

为了提出这些问题,我使用了一个question用辅助que定理环境定义的环境,该环境使用定义amsthm,但同样的想法也适用(予以适当修改) 到环境的任何其他实现question

编译说明:

该文档可以通过(至少)三种不同的方式处理;在所有情况下,请确保文件myright.ist位于 TeX 可以找到的地方(例如,当前工作目录):

1- 运行:以以下方式makeindex处理您的文档(我们称之为):topics.tex

    pdflatex topics
    makeindex -s myright.ist add.idx 
    makeindex -s myright.ist mul.idx 
    makeindex -s myright.ist exp.idx 
    makeindex -s myright.ist sub.idx 
    pdflatex topics

2- 使用arara:将以下几行添加到文档中并让其arara完成工作(感谢保罗塞雷达提出这个建议):

    % arara: pdflatex
    % arara: makeindex: { style: myright, files: [ add.idx, mul.idx ] }
    % arara: makeindex: { style: myright, files: [ exp.idx, sub.idx ] }
    % arara: pdflatex

3- 从编辑器本身来看:在这种情况下,代码略有变化:

    \documentclass{article}
    \usepackage{amsthm}
    \usepackage[
      noautomatic% <---- A
    ]{imakeidx}

    \makeatletter
    \xpatchcmd{\imki@putindex}
      {\immediate\closeout}
      {\ifimki@disableautomatic\else\immediate\closeout}
      {}{}
    \xpatchcmd{\imki@putindex}
      {\endcsname}
      {\endcsname\fi}
      {}{}
    \makeatother

    \indexsetup{level=\subsection*,noclearpage,othercode={\parskip=5pt}} % <-----A

    \makeindex[
      name=add,
      title=Addition,
      columns=1,
      options=-s myright
    ]  %% Define new index of addition
    \makeindex[
      name=mul,
      title=Multiplication,
      columns=1,
      options=-s myright
    ]  %% Define new index of multiplication
    \makeindex[
      name=exp,
      title=Exponentiation,
      columns=1,
      options=-s myright
    ]  %% Define new index of exponentiation
    \makeindex[
      name=sub,
      title=Subtraction,
      columns=1,
      options=-s myright
    ]  %% Define new index of subtraction

    % Definition of the auxiliary environment for the questions
    \theoremstyle{definition}
    \newtheorem{que}{Question}

    % Definition of the mian environment for the questions
    % it writes an entry to the ToC; 
    % it also writes an entry in the index specified in the mandatory argument 
    \let\tmpa\relax
    \newenvironment{question}[2][]
      {\begin{que}[#1]
      \gdef\tmpb{#2}
      \if\relax\detokenize{#1}\relax
        \gdef\tmpa{\relax}  
      \else
        \gdef\tmpa{~#1}
      \fi
      \addcontentsline{toc}{section}{Question~\theque.\tmpa}\ignorespaces
      }
      {\index[\tmpb]{Question~\theque.\tmpa}\end{que}}

    % Simply produces a heading ''List of Topics'' formatted as unnumbered section
    \newcommand\listoftopics{
      \section*{List of Topics}
    }

    \begin{document}

    \tableofcontents
    \listoftopics
    \printindex[add]% <---- A
    \printindex[mul]% <---- A
    \printindex[exp]% <---- A
    \printindex[sub]% <---- A

    \clearpage

    \begin{question}[Commutativity]{add}
    Is addition of real numbers commutative?
    \end{question}

    \begin{question}[Commutativity]{sub}
    Is subtraction of real numbers commutative?
    \end{question}

    \begin{question}[Associativity]{mul}
    Is multiplication of quaternions associative?
    \end{question}

    \begin{question}[Inverse]{mul}
    Does inverses for non-negative quaternions always exist?
    \end{question}

    \begin{question}[General properties]{exp}
    Which are the general properties for exponentiation in the field of complex numbers?
    \end{question}

    \begin{question}{add}
    Is addition of real numbers modulative?
    \end{question}

    \begin{question}[Modulative]{sub}
    Is subtraction of real numbers modulative?
    \end{question}

    \printindex[add]% <---- B
    \printindex[mul]% <---- B
    \printindex[exp]% <---- B
    \printindex[sub]% <---- B

    \end{document}
  • 首先,注释掉标记为 的行 % <---- A,并取消注释标记为 的行 % <---- B。处理文档。

  • 注释掉标记为 的行 % <---- B,并取消注释标记为 的行% <---- A。处理文档。

\makeatletter之间的代码\makeatother实现了上面描述的第一种编译方法,并由埃格尔纠正中的一个错误imakeidx;在软件包的未来版本中,这个错误将被修复,并且该代码将不再需要。

使用列表的解决方案

使用\@starttoc

这个解决方案比我之前给出的要简单得多this answer

这次的想法是使用与生成 ToC、LoF 和 LoT 相同的命令\@starttoc;编译过程比我的其他答案更简单:处理文档两次(因为任何标准 ToC、LoF 或 LoT 都需要它)。

\documentclass{article}
\usepackage{amsthm}
\usepackage{pgffor}
\usepackage{etoolbox}

% Definition of the auxiliary environment for the questions
\theoremstyle{definition}
\newtheorem{que}{Question}

% Definition of the mian environment for the questions
% it writes an entry to the ToC (optional argument); 
% it also writes an entry in the topic list specified in the mandatory argument 
\makeatletter
\let\tmpa\relax
\def\addtotoc#1{\addcontentsline{#1}{subsection}{Question~\theque.\tmpa}}
\newenvironment{question}[2][]
  {\begin{que}[#1]
  \if\relax\detokenize{#1}\relax
    \gdef\tmpa{\relax}  
  \else
    \gdef\tmpa{~#1}
  \fi
  \addcontentsline{toc}{section}{Question~\theque.\tmpa}%
  \if\relax\detokenize{#2}\relax
  \else
    \forcsvlist\addtotoc{#2}
  \fi\ignorespaces
  }
  {\end{que}}

% Produces a heading ''List of Topics'' formatted as unnumbered section
% and it also generates the list of questions sorted by topics
\newcommand\listoftopics[1]{
  \section*{List of Topics}
  \foreach \Title/\Name in {#1}
  {
    \subsection*{\Title}
    \@starttoc{\Name}
  } 
}
\makeatother

\begin{document}

\tableofcontents
\listoftopics{Addition/add,Multiplication/mul,Exponentiation/exp,Subtraction/sub}

\clearpage

\begin{question}[Commutativity]{add}
Is addition of real numbers commutative?
\end{question}

\begin{question}[Commutativity]{sub}
Is subtraction of real numbers commutative?
\end{question}

\begin{question}[Associativity]{mul}
Is multiplication of quaternions associative?
\end{question}

\begin{question}[Inverse]{mul}
Does inverses for non-negative quaternions always exist?
\end{question}

\begin{question}[Common properties]{mul,add,sub}
Which properties do addition, subtraction and multiplication of real numbers have in common?
\end{question}

\begin{question}[General properties]{exp}
Which are the general properties for exponentiation in the field of complex numbers?
\end{question}

\begin{question}{add}
Is addition of real numbers modulative?
\end{question}

\begin{question}[Modulative]{sub}
Is subtraction of real numbers modulative?
\end{question}

\begin{question}[Bonus]{}
Can you give an example of non-associative binary operation on the real numbers? 
\end{question}

\end{document}

enter image description here

解释和备注

  • 现在,按主题生成列表的主要命令带有\listoftopics一个强制参数;此强制参数是一个逗号分隔的列表,列表形式为<title>/<name>,其中<title>是用作主题标题的字符串,<name>是用于生成列表(以及相关辅助文件的扩展名)的字符串。例如,在上面的代码中,要生成主题列表,只需调用

    \listoftopics{Addition/add,Multiplication/mul,Exponentiation/exp,Subtraction/sub}
    
  • 用于排版>问题的环境具有语法

    \begin{question}[<note>]{<partial tocs>}
    contents
    \end{question}
    

    其中<note>是问题的可选注释,将在文档和一般目录中排版。是中使用的<partial tocs>一些 的逗号分隔列表;问题将列在与每个声明的 相关的主题中。例如,上例中的问题 5“常见属性”使用<name>\listoftopics<name>s

    \begin{question}[Common properties]{mul,add,sub}
    Which properties do addition, subtraction and multiplication of real numbers have in common?
    \end{question}
    

    如果强制参数为空,例如

    \begin{question}[<note>]{}
    contents
    \end{question}
    

    那么问题仅添加到一般目录中(参见示例代码中的问题 9“奖励”)。

  • 使用的附加包pgffor用于轻松生成用于生成新主题列表的循环以及etoolbox用于写入多个主题条目的循环。

使用titletoc包:

这种方法使用titletoc,与上面的类似;但是,由于所有部分 ToC 使用的辅助文件titletoc是相同的,因此必须在这里做一些额外的工作,以便在必要时停止和恢复部分 ToC:

\documentclass{article}
\usepackage{amsthm}
\usepackage{pgffor}
\usepackage{titletoc}

% Definition of the auxiliary environment for the questions
\theoremstyle{definition}
\newtheorem{que}{Question}

% Definition of the mian environment for the questions
% it writes an entry to the ToC (optional argument); 
% it also writes an entry in the topic list specified in the mandatory argument 
\makeatletter
\let\tmpa\relax
\let\tmpb\relax
\newenvironment{question}[2][]
  {\if\relax\detokenize{#2}\relax
     \gdef\tmpb{\relax}  
   \else
     \resumecontents[#2]
     \gdef\tmpb{#2}  
   \fi  
   \begin{que}[#1]
   \if\relax\detokenize{#1}\relax
     \gdef\tmpa{\relax}  
   \else
     \gdef\tmpa{~#1}
   \fi
   \addcontentsline{toc}{section}{Question~\theque.\tmpa}\ignorespaces
  }
  {\end{que}\if\relax\tmpb\relax\else\stopcontents[\tmpb]\fi}

% Produces a heading ''List of Topics'' formatted as unnumbered section
% and it also generates the list of questions sorted by topics
\newcommand\listoftopics[1]{
  \section*{List of Topics}
  \foreach \Title/\Name in {#1}
  {
    %\subsection*{\Title}
    \startcontents[\Name]
    \printcontents[\Name]{l}{1}{\subsection*{\Title}}
    \stopcontents[\Name]
  } 
}
\makeatother

\titlecontents{lsection}
  [3.8em]
  {}
  {\contentslabel{2.3em}}
  {\hspace*{-2.3em}}
  {\titlerule*[1pc]{.}\contentspage}

\begin{document}

\tableofcontents
\listoftopics{Addition/add,Multiplication/mul,Exponentiation/exp,Subtraction/sub}

\clearpage

%\resumecontents[add]
\begin{question}[Commutativity]{add}
Is addition of real numbers commutative?
\end{question}
%\stopcontents[add]

\begin{question}[Commutativity]{sub}
Is subtraction of real numbers commutative?
\end{question}

\begin{question}[Associativity]{mul}
Is multiplication of quaternions associative?
\end{question}

\begin{question}[Inverse]{mul}
Does inverses for non-negative quaternions always exist?
\end{question}

\begin{question}[General properties]{exp}
Which are the general properties for exponentiation in the field of complex numbers?
\end{question}

\begin{question}{add}
Is addition of real numbers modulative?
\end{question}

\begin{question}[Modulative]{sub}
Is subtraction of real numbers modulative?
\end{question}

\begin{question}[Bonus]{}
Can you give an example of non-associative binary operation on the real numbers? 
\end{question}

\end{document}

enter image description here

答案2

要在单个文档中创建两个目录,请使用titletoc包。这是一个最小示例:

\documentclass{book}
\usepackage{titletoc}

\begin{document}

\begin{titlepage}
        This is the Title page.
\end{titlepage}

\startlist{toc}
\printlist{toc}{}{\section*{My  toc}}

\tableofcontents

\chapter{My 1st chapter}
\chapter{My 2nd chapter}
\chapter{My 3rd chapter}

\end{document}

关于按主题排序,请尝试阅读以下两篇文章:

相关内容