删除枚举前的换行符

删除枚举前的换行符

我在尝试更改环境时遇到了一些麻烦enumerate。我希望练习编号与问题在同一行开始。请考虑以下代码。

\documentclass{article}
\usepackage{enumitem}

\newcounter{exercise}
\newenvironment{exercise}[1][]{\refstepcounter{exercise}
\noindent\textbf{\theexercise.#1 }}{}


\begin{document}
\begin{exercise}
\begin{enumerate}[label=(\alph*)]
\item If $A$ is a subset of $B$ and $B$ is a subset of $C$, prove that $A$ is a subset of $C$.
\item If $B\subset A$, prove that $A\cup B=A$, and conversely.
\item If $B\subset A$, prove that for any set $C$ both $B\cup C\subset A\cup C$ and $B\cap C\subset A\cap C$.
\end{enumerate}
\end{exercise}

\begin{exercise}
Prove that $A\cup (B\cap C)=(A\cup B)\cap (A\cup C)$.
\end{exercise}

\end{document}

其结果为:

rendered_mwe

我想要这样的东西:

desired output

我不想只为作为练习开始的枚举删除文档中所有枚举的换行符。

答案1

\documentclass{article}
\usepackage{MinionPro}%optional
\usepackage{enumitem}

\makeatletter
\newcounter{exercises}
\let\ltx@endenumerate\endenumerate
\def\exercise{\futurelet\@next@item\@xercise}
\def\@xercise{%
  \refstepcounter{exercises}%
  \settowidth\@tempdima{\textbf{\theexercises.}}%
  \@tempdimb5pt%
  \description[leftmargin=\@tempdima,labelwidth=0pt,labelsep=0pt]
  \item[\textbf{\theexercises.}]%
  \ifx\@next@item\item
    \let\endenumerate\ltx@endenumerate
    \enumerate[labelsep=\@tempdimb,label=(\alph*)]
  \else
    \let\endenumerate\relax
    \hskip\@tempdimb
  \fi
}
\def\endexercise{\endenumerate\enddescription}
\makeatother

\begin{document}
\begin{exercise}%
  \item If $A$ is a subset of $B$ and $B$ is a subset of $C$, prove that $A$ is a subset of $C$.
  \item If $B\subset A$, prove that $A\cup B=A$, and conversely.
  \item If $B\subset A$, prove that for any set $C$ both $B\cup C\subset A\cup C$ and $B\cap C\subset A\cap C$.
\end{exercise}

\begin{exercise}%
  Prove that $A\cup (B\cap C)=(A\cup B)\cap (A\cup C)$.
\end{exercise}
\end{document}

example_rendered

请注意,\item在此定义中,exercise是可选命令(在此环境内)。但是,您可能希望有一个不允许使用 的环境\item和一个允许使用 的星号版本:

\documentclass{article}
\usepackage{MinionPro}%optional
\usepackage{enumitem}

\makeatletter
\newcounter{exercises}
\def\exercise{%
  \refstepcounter{exercises}%
  \settowidth\@tempdima{\textbf{\theexercises.}}%
  \description[leftmargin=\@tempdima,labelwidth=0pt,labelsep=0pt]
  \item[\textbf{\theexercises.}]
  \hspace\p@
}
\let\endexercise\enddescription
\newenvironment{exercise*}{%
  \let\hspace\@gobble
  \exercise\enumerate[labelsep=3.5pt,label=(\alph*)]}{\endenumerate}
\makeatother

\begin{document}
\begin{exercise*}
  \item If $A$ is a subset of $B$ and $B$ is a subset of $C$, prove that $A$ is a subset of $C$.
  \item If $B\subset A$, prove that $A\cup B=A$, and conversely.
  \item If $B\subset A$, prove that for any set $C$ both $B\cup C\subset A\cup C$ and $B\cap C\subset A\cap C$.
\end{exercise*}

\begin{exercise}
  Prove that $A\cup (B\cap C)=(A\cup B)\cap (A\cup C)$.
\end{exercise}
\end{document}

如果您想要更花哨一些,您可以向定义添加如下警告 - 但是,除了花哨之外,它可能也会有所帮助(用于调试您的手稿)。

\makeatletter
\newcounter{exercises}
\let\ltx@item\item
\def\exercise{%
  \refstepcounter{exercises}%
  \settowidth\@tempdima{\textbf{\theexercises.}}%
  \description[leftmargin=\@tempdima,labelwidth=0pt,labelsep=0pt]
  \item[\textbf{\theexercises.}]
  \def\item{%
    \@latex@warning{You can't use \noexpand\item inside exercise.
      You might want to use the starred version of exercise.}}
  \hspace\p@
}
\let\endexercise\enddescription
\newenvironment{exercise*}{%
  \let\hspace\@gobble
  \exercise
  \let\item\ltx@item
  \enumerate[labelsep=3.5pt,label=(\alph*)]}{\endenumerate}
\makeatother

答案2

如果您希望保留当前输入(根据需要enumerate在 内嵌套exercise),那么最简单的方法是转换exercise为列表,默认情况下\item会在同一行设置嵌套的 :

enter image description here

\documentclass{article}

\usepackage{enumitem,etoolbox}

\newlist{exercise}{enumerate}{1}
\setlist[exercise]{label={\bfseries \arabic*.},resume}
\makeatletter
\patchcmd{\enit@enumerate@i}{\fi}{\fi\ifnum\pdfstrcmp{\@currenvir}{exercise}=0 \item\fi}{}{}
\makeatother

\begin{document}

\begin{exercise}
  \begin{enumerate}[label=(\alph*)]
    \item If~$A$ is a subset of~$B$ and~$B$ is a subset of~$C$, prove that~$A$ is a subset of~$C$.
    \item If $B \subset A$, prove that $A \cup B = A$, and conversely.
    \item If $B \subset A$, prove that for any set~$C$ both $B \cup C \subset A \cup C$ and $B \cap C \subset A \cap C$.
  \end{enumerate}
\end{exercise}

\begin{exercise}
  Prove that $A \cup (B \cap C) = (A \cup B) \cap (A \cup C)$.
\end{exercise}

\end{document}

该补丁仅在环境\item开始时添加了一个exercise

相关内容