使用 enumitem 的练习列表:如何控制子列表的缩进和标签?

使用 enumitem 的练习列表:如何控制子列表的缩进和标签?

对于一本书的项目,我想定义每一章的练习,并打印出如下标签 练习 1.1类似这样的 TeX 标签lab:1.1可以引用为"see Exercise \ref{lab:1.1}"。[我读过一些类似问题的讨论,但无法根据我的需要进行调整。]

其中很多内容已经使用普通的嵌套枚举列表编写。我想使用以下类型的标记来简化转换:

\begin{Exercises}
  \exercise Here's the first exercise: Make it longer to see line wrap. Now the
   question: How many angels can dance on the head of a pin?
   \begin{enumerate}
      \item Why are they laughing?
      \item In a random sample of $n=30$ pins, how many angels do you expect, on average?
   \end{enumerate}
\end{Exercises}

产生如下输出:

Exercise 1.1: Here's the first exercise: Make it longer to see line wrap. Now the
    question: How many angels can dance on the head of a pin?

    (a) Why are they laughing?
    (b) In a random sample of $n=30$ pins, how many angels do you expect, on average?

我尝试使用 enumitem 包来执行此操作,如下面的示例测试文件所示,并且我已经获得了可以工作的标签和引用,但是练习的各个部分标记为 1.、2. 等,我得到的格式如下所示:

Exercise 1.1: Here's the first exercise: Make it longer to see line wrap. Now the
              question: How many angels can dance on the head of a pin?

              1. Why are they laughing?
              2. In a random sample of $n=30$ pins, how many angels do you expect, on average?

我可以通过明确使用\begin{enumerate}[label=(\alph*)]每个练习的各个部分来获得标记为(a),(b)...的部分,但我希望它能够自动发生。

这是我当前的测试文件:

\documentclass{report}
\usepackage{enumitem}
\usepackage{lipsum}

% Test a list style for exercises in chapters, allowing references.

\newlist{exercises}{enumerate}{2}
\setlist[exercises]{label=\textbf{Exercise \thechapter.\arabic*:},leftmargin=*}
% why doesn't this work for exercise parts?
\setenumerate[2]{label=(\alph*)}

\newcounter{exercise}[chapter]
\renewcommand{\theexercise}{\thechapter.\arabic{exercise}}
\newenvironment{\Exercises}{%
  \begin{exercises}[leftmargin=*]
  }%
  {%
  \end{exercises}
  }

\newcommand{\exercise}{%
  \refstepcounter{exercise}%
  \label{lab:\theexercise}%
  \item%
    }
\newcommand{\labref}[1]{Exercise~\ref{#1}}

\begin{document}

\chapter{One}\label{ch:one}
This is chapter \ref{ch:one}.
\lipsum[2]

\begin{\Exercises}

\exercise Here's the first exercise: Make it longer to see line wrap. Now the question: How many angels can dance on the head of a pin?
    \begin{enumerate}
        \item Why are they laughing?
        \item In a random sample of $n=30$ pins, how many angels do you expect, on average?
    \end{enumerate}

\exercise How many chucks can a woodchuck chuck?
    \begin{enumerate}[label=(\alph*)]
        \item Discuss: Can a woodchuck really chuck wood?
        \item If a woodchuck \emph{can} chuck 3 cubits per hour, how many hours would it take
        to chuck a mega-cubit forest?
    \end{enumerate}

\exercise Why can't I make the parts of exercises labeled as (a), (b), ... without setting [label=] manually?

\end{\Exercises}

We can reference Exercise \ref{lab:1.1}.
What about Exercise \ref{lab:1.2}?
And also using the shorthand, \labref{lab:1.2}?

\chapter{Two}\label{ch:two}
This is chapter \ref{ch:two}. That's all there is.

\begin{\Exercises}

\exercise Here's the first exercise: How many angels can dance on the head of a pin?
    \begin{enumerate}
        \item Why are they laughing?
        \item In a random sample of $n=30$ pins, how many angels do you expect, on average?
    \end{enumerate}

\exercise How many chucks can a woodchuck chuck?  Refer to \labref{lab:1.2} for details.
\end{\Exercises}

Can we reference Exercise \ref{lab:2.1}?
Why not \labref{lab:2.2}?

\end{document}

答案1

前言

您当前文档中出现错误:您定义了一个环境,其名称是一个命令(\newenvironment{\Exercises})。

我有两个想法来实现这一点,基本上就是你所做的。但不知何故,我觉得你把它们混淆了。

第一个想法:手动计数器

您为练习创建一个手动计数器,并为它们创建一个自制标签,这也会创建标签。这基本上就是您所做的。对于标签((a)、(b)、...),我只需使用\setlist[enumerate]{label=(\alph*)},这对我来说很有效。

\documentclass{report}
\usepackage{enumitem}
\usepackage{lipsum}

\newcounter{exercisecounter}[chapter]
\renewcommand{\theexercisecounter}{\thechapter.\arabic{exercisecounter}}

\newcommand{\exercise}{%
  \medskip
  \refstepcounter{exercisecounter}%
  \noindent \textbf{Exercise~\theexercisecounter:}~
  \label{lab:\theexercisecounter}%
}

\newcommand{\labref}[1]{Exercise~\ref{#1}}

\setlist[enumerate]{label=(\alph*)}


\begin{document}

\chapter{One}\label{ch:one}
This is chapter \ref{ch:one}.
\lipsum[2]

\exercise Here's the first exercise: Make it longer to see line wrap. Now the question: How many angels can dance on the head of a pin?
    \begin{enumerate}
        \item Why are they laughing?
        \item In a random sample of $n=30$ pins, how many angels do you expect, on average?
    \end{enumerate}

\exercise How many chucks can a woodchuck chuck?
    \begin{enumerate}
        \item Discuss: Can a woodchuck really chuck wood?
        \item If a woodchuck \emph{can} chuck 3 cubits per hour, how many hours would it take
        to chuck a mega-cubit forest?
    \end{enumerate}

\exercise Why can't I make the parts of exercises labeled as (a), (b), ... without setting [label=] manually?

We can reference Exercise \ref{lab:1.1}.
What about Exercise \ref{lab:1.2}?
And also using the shorthand, \labref{lab:1.2}?

\chapter{Two}\label{ch:two}
This is chapter \ref{ch:two}. That's all there is.

\exercise Here's the first exercise: How many angels can dance on the head of a pin?
    \begin{enumerate}
        \item Why are they laughing?
        \item In a random sample of $n=30$ pins, how many angels do you expect, on average?
    \end{enumerate}

\exercise How many chucks can a woodchuck chuck?  Refer to \labref{lab:1.2} for details.

Can we reference Exercise \ref{lab:2.1}?
Why not \labref{lab:2.2}?

\end{document}

这里,您不需要对练习标签和您想要定义的环境进行第二次枚举\newenvironment{\Exercises}

练习 - 第一个想法

第二个想法:嵌套枚举(推荐并更新)

经过一番尝试,enumitem我可以为您的问题创建以下解决方案。它只使用enumitem,您不必处理自创的计数器。它对我来说很好用,我会推荐这种替代方案,因为我认为它更优雅。我添加了一些评论,所以我认为你应该明白我做了什么。

评论:水平长度/边距/缩进的配置enumitem不太容易理解(至少对我来说不是:o))我参考多行项目缩进enumitem文档更多细节。

\documentclass{report}
\usepackage{enumitem}
\usepackage{lipsum}


% New list for exercises
\newlist{exercises}{enumerate}{2}

\setlist[exercises]{%
  label=\textbf{Exercise \thechapter.\arabic*}~,  % Label: Exercise C.E
  ref=\thechapter.\arabic*, % References: C.E (important!)
  align=left,               % Left align labels
  labelindent=0pt,          % No space betw. margin of list and label
  leftmargin=0pt,           % No space betw. margin of list and following lines
  itemindent=!,             % Indention of item computet automatically
}

\setlist[enumerate, 1]{label=(\alph*)}            % Label for subexercises

\newcommand{\exercise}{%
  \item \label{lab:\arabic{chapter}.\arabic{exercisesi}}  % Append label to item
}


\newcommand{\labref}[1]{Exercise~\ref{#1}}

\begin{document}

\chapter{One}\label{ch:one}
This is chapter \ref{ch:one}.
\lipsum[2]

\begin{exercises}

    \exercise Here's the first exercise: Make it longer to see line wrap. Now the question: How many angels can dance on the head of a pin?
        \begin{enumerate}
            \item Why are they laughing?
            \item In a random sample of $n=30$ pins, how many angels do you expect, on average?
        \end{enumerate}

    \exercise How many chucks can a woodchuck chuck?
        \begin{enumerate}
            \item Discuss: Can a woodchuck really chuck wood?
            \item If a woodchuck \emph{can} chuck 3 cubits per hour, how many hours would it take
            to chuck a mega-cubit forest?
        \end{enumerate}

    \exercise Why can't I make the parts of exercises labeled as (a), (b), ... without setting [label=] manually?

\end{exercises}

We can reference Exercise \ref{lab:1.1}.
What about Exercise \ref{lab:1.2}?
And also using the shorthand, \labref{lab:1.2}?

\chapter{Two}\label{ch:two}
This is chapter \ref{ch:two}. That's all there is.

\begin{exercises}

    \exercise Here's the first exercise: How many angels can dance on the head of a pin?
        \begin{enumerate}
            \item Why are they laughing?
            \item In a random sample of $n=30$ pins, how many angels do you expect, on average?
        \end{enumerate}

    \exercise How many chucks can a woodchuck chuck?  Refer to \labref{lab:1.2} for details.

\end{exercises}

Can we reference Exercise \ref{lab:2.1}?
Why not \labref{lab:2.2}?

\end{document}

练习 - 第二个想法

相关内容