练习题在表格上自动编号

练习题在表格上自动编号

我有几百个练习题

\begin{exercise}

\end{exercise}

每个文件都是一个小文件。我通过\input{...}主文档输入这些文件。

我已经做了一些有关这些练习的其他问题,我解释了我的目的这里


这是我的问题:我最终的 .pdf 文档中显示的练习编号可能会以某种方式自动显示循环在一个表格 有三列,第一列显示练习所属的章节,第二列是练习的编号,第三列是练习所在的页面。

不幸的是,我不知道这是否可能,显然如果可能的话,如何才能成为......因此我无法举出自己的例子。

我能想到的都是通过\label \ ref,但我觉得每次我决定在课文中间引入一些练习时,这都很难做到,我必须修改整个表格


以下是我的文档的结构

\documentclass[a4paper,10pt]{book}
\usepackage[utf8x]{inputenc}

\newtheorem{exercise}{\Large \bfseries}[chapter] 

\begin{document}

\chapter{kinetic }
  \input{exercise.435.tex}

\chapter{momentum}
  \input{exercise.436.tex}

 \end{document}

以及exercise.435.texexercise.436.tex

\begin{exercise}
 Here is the text of the exercise
\end{exercise}

答案1

有了包,etoc您就可以使用目录来实现这一点。

\etocname\etocnumber是从文件中提取的\etocpage内容。这里我使用了,因为它已经包含章节编号。代码设置为练习的“名称”,写入文件时是章节编号。因此,如果需要,请提供此编号。etoc.toc\etocnumber.toc\etocname

使用 hyperref,这些内容就是链接(使用 hyperref 选项linktoc=all还可以链接页码)。

编辑 1:我添加了一些最后的润色来实现网格状排版。众所周知,这在 TeX 中总是有些困难,因为添加了所有行间胶水等。我用新的图像替换了输出的图像,并将链接着色以显示它们确实是链接。

请注意,这个看起来很花哨的更新与原始问题无关,它只是一个在所选的多列样式中确定输出外观的问题。(更新:注释掉的代码中标明了一种更简单的方法)。

编辑 2:我向宏添加了一个变体\exercisetotoc,这样就避免了为每个练习创建hyperref书签(显示在最终的 pdf 文件中)。根据具体情况,人们可能希望或不希望通过创建此类书签hyperref。不过,在这两种变体中,通过生成的列表中的练习编号etoc都正确超链接。

注意:使用来\tableofcontents显示etoc练习列表根本没有排除文档中的标准。假设我们有章节、节和小节。它们在linguo\tableofcontents中分别处于级别 0、1 和 2。然后etoc

\begin{document)
\etocsetlevel{exercise}{3} 
\setcounter{tocdepth}{2}
\tableofcontents
%....

显示一个完全标准的目录,包括章节(也可能是部分)、节和小节。在文档的后面

\setcounter{tocdepth}{1}
\etocsetlevel{exercise}{1} % 'exercise' `.toc` entries will be included
\etocsetlevel{section}{2}  % so we get rid of sections (subsections are already at level 2)
%...
%(the stuff below with \etocsetstyle{exercise} and \etocsetstyle{chapter}
%...
\tableofcontents % this table is the list of exercises as seen below
% see the code excerpts below to see how it is done

通过这种方式,我们可以etoc选择性地打印“列表”中的任何内容,所有这些都带有单身的 .toc文件...

\documentclass[a4paper,10pt]{book}
\usepackage[utf8x]{inputenc}
\usepackage{hyperref}
\hypersetup{colorlinks=true,linkcolor={blue}}  % <- UPDATE to show the links
\usepackage{etoc}


\newcount\exo % just for dummy generation of exercise numbers here

\def\exercisetotoc{\addcontentsline{toc}
         {exercise}
         {\protect\numberline{\theexerci}\thechapter}}

% actually as we will use only \etocnumber, we could have done here rather
% \def\exercisetotoc{\addcontentsline{toc}{exercise}{\theexerci}}
% and then we would have used below \etocname, rather than \etocnumber

% if one does NOT want hyperref to create bookmarks for each exercise, then 
% one should use:
%
% \makeatletter
% \def\exercisetotoc{\addtocontents {toc}{\protect\contentsline 
%       {exercise}{\theexerci }{\thepage }{\@currentHref }}} 
% \makeatother
%
% (here I am using the variant where below one would have used \etocname,
%  not \etocnumber )
%
% Note that the exercise numbers are still hyperlinked, the only difference
% is that they don't appear among the bookmarks of the PDF

\newtheorem{exerci}{\Large \bfseries}[chapter] 
\newenvironment{exercise}{\begin{exerci}\exercisetotoc}{\end{exerci}}

\begin{document}\thispagestyle{empty}

% setting up exercise for use with etoc 
\etocsetlevel{exercise}{1}

\etocsetstyle{exercise}
{}
{}
{\noindent\etocnumber\strut\leaders\etoctoclineleaders\hfill\etocpage\par}
{\pagebreak[2]\vskip\baselineskip}

% UPDATE FOR GRID LIKE LOOK:
% the following is to ensure "grid-like" typesetting, also with the chapter
% headings in the list (which are done with `\large' size hence upset 
% the line spacings)
% (it is assumed here that the chapter name fits on one or two 
% column-width lines, else the code would need some additions)
%
% or, one can just say \etocsetlevel{chapter}{2} to not have these chapter 
% headings included. Or, one can just be happy with the way `etoc` by default 
% typesets these headings in the TOC, which here however would lead to a 
% somewhat "ragged" look of the columns

\newbox\chapbox
\etocsetstyle{chapter}
{}
{}
{\setbox\chapbox=\vtop{\bfseries        % .5em would be better than 1ex here
        \noindent\strut\large\etocnumber\hskip1ex\etocname\par}%
\unvcopy\chapbox\nointerlineskip
\kern-\ht\chapbox\kern-\dp\chapbox\kern3\baselineskip\nopagebreak[3]}
{}

% update: the following would be a much simpler way to get the grid-like 
% typesetting, but the chapter titles might be a bit cramped as the 
% baselineskip is kept equal to the ones for `normalsize`, not `large`

%\etocsetstyle{chapter}
%{}
%{}
%{\noindent\strut{\bfseries\large\etocnumber\hskip.5em\etocname}\par
% \vskip\baselineskip}
%{}

% 
\setcounter{tocdepth}{1}

% 4 columns
\etocruledstyle[4]{\Large\bfseries Index to the exercises}

\setlength{\columnseprule}{.4pt}

\tableofcontents

\chapter{kinetic }

\exo300

\loop

\begin{exercise}
stuff
\end{exercise}

\ifnum\exo<399
\advance\exo by 1
\repeat

\chapter{momentum}

\exo400

\loop

\begin{exercise}
stuff
\end{exercise}

\ifnum\exo<499
\advance\exo by 1
\repeat


\chapter{energy}

\exo500

\loop

\begin{exercise}
stuff
\end{exercise}

\ifnum\exo<599
\advance\exo by 1
\repeat

\end{document}

第一页

第二页

答案2

这是一种使用的方法etoolbox的便捷宏。将\AtEndEnvironment一个宏添加到exercise环境中,将三个值存储在列表中(etoolbox也有用于此目的的工具)。然后在表中我们循环遍历此列表。

这样,在设置表格时,只有表格之前设置的练习才会存储在列表中。但你没有说你想要把表格放在哪里,所以……

\documentclass[a4paper,10pt]{book}
\usepackage[utf8]{inputenc}

\newtheorem{exercise}{\Large \bfseries}[chapter] 

\usepackage{filecontents}
\begin{filecontents}{exercise.435.tex}
\begin{exercise}
 435-1
\end{exercise}
\begin{exercise}
 435-2
\end{exercise}
\end{filecontents}

\begin{filecontents}{exercise.436.tex}
\begin{exercise}
 436-1
\end{exercise}
\begin{exercise}
 436-2
\end{exercise}
\end{filecontents}

\usepackage{etoolbox,booktabs}
\newcommand*\saveexercise{%
  \listxadd\savedexercises{%
    \thechapter&\theexercise&\thepage\noexpand\\}
}
\newcommand*\writeexercise[1]{#1}
\AtEndEnvironment{exercise}{\saveexercise}

\begin{document}

\chapter{kinetic}
  \input{exercise.435.tex}

\chapter{momentum}
  \input{exercise.436.tex}

\chapter{Overview}
\begin{tabular}{ccc}
 \toprule
  Chapter & Exercise & Page \\
 \midrule
  \forlistloop{\writeexercise}{\savedexercises}
 \bottomrule
\end{tabular}

\end{document}

在此处输入图片描述

相关内容