如何在编号环境中忽略某些项目?

如何在编号环境中忽略某些项目?

我有一个环境来给其中的段落编号,如果我必须忽略环境中一些不需要编号的段落,例如:我想忽略的编号\lipsum[6-7],该怎么做?

环境的完整代码如下:

 \documentclass{book}
\usepackage{mparhack}   % get marginpars to always show up on the correct side (need to compile twice)
\usepackage{lipsum}     % for dummy text

% change this to get the formatting you want
\newcommand{\parnum}{\bfseries\arabic{parcount}}
\newcounter{parcount}
\newenvironment{parnumbers}{%
\par%
   \everypar{\stepcounter{parcount}\leavevmode\marginpar[\hfill\parnum]
{\parnum}}%
\setcounter{parcount}{0}%to restart numbering in every environment
}{}

\begin{document}
\begin{parnumbers}
\lipsum[1-4]
\end{parnumbers}

\begin{parnumbers}
\lipsum[5-8]
\end{parnumbers}

\begin{parnumbers}
\lipsum[9-10]
\end{parnumbers}

\end{document}

答案1

您可以定义一个带星号的parnumbers环境版本,例如(虽然我的方法可能比较粗鲁)

\newenvironment{parnumbers*}{%
\par%
   \everypar{\stepcounter{parcount}\leavevmode\marginpar[\hfill]
{}}%
\setcounter{parcount}{0}%to restart numbering in every environment
}{}

然后

\begin{parnumbers}
\lipsum[5]
\begin{parnumbers*}
\lipsum[6-7]
\end{parnumbers*}
\lipsum[8]
\end{parnumbers}

应该可以完成这个工作。

完整的 MWE:

\documentclass{book}
\usepackage{mparhack}   % get marginpars to always show up on the correct side (need to compile twice)
\usepackage{lipsum}     % for dummy text

% change this to get the formatting you want
\newcommand{\parnum}{\bfseries\arabic{parcount}}
\newcounter{parcount}
\newenvironment{parnumbers}{%
\par%
   \everypar{\stepcounter{parcount}\leavevmode\marginpar[\hfill\parnum]
{\parnum}}%
\setcounter{parcount}{0}%to restart numbering in every environment
}{}

\newenvironment{parnumbers*}{%
\par%
   \everypar{\stepcounter{parcount}\leavevmode\marginpar[\hfill]
{}}%
\setcounter{parcount}{0}%to restart numbering in every environment
}{}

\begin{document}
\begin{parnumbers}
\lipsum[1-4]
\end{parnumbers}

\begin{parnumbers}
\lipsum[5]
\begin{parnumbers*}
\lipsum[6-7]
\end{parnumbers*}
\lipsum[8]
\end{parnumbers}

\begin{parnumbers}
\lipsum[9-10]
\end{parnumbers}

\end{document}

在此处输入图片描述

答案2

我对您的代码进行了如下改进:

\newenvironment{parnumbers*}{%to ignore some items 
\par
\everypar{\leavevmode\marginpar[\hfill]
{}}%
}{}

所以正如我想要的那样,lipsum 5 的数字是 1,lipsum 8 的数字是 2,lipsum 6-7 被忽略。

最后一个问题是如何通过命令来实现,而不是这样的环境?

在此处输入图片描述

完整的 MWE:

\documentclass{book}
 \usepackage{mparhack}   % get marginpars to always show up on the correct side (need to compile twice)
\usepackage{lipsum}     % for dummy text

% change this to get the formatting you want
\newcommand{\parnum}{\bfseries\arabic{parcount}}
\newcounter{parcount}
\newenvironment{parnumbers}{%
\par%
   \everypar{\stepcounter{parcount}\leavevmode\marginpar[\hfill\parnum]
{\parnum}}%
\setcounter{parcount}{0}%to restart numbering in every environment
}{}

\newenvironment{parnumbers*}{%
\par%
   \everypar{\leavevmode\marginpar[\hfill]
{}}%

}{}

\begin{document}
\begin{parnumbers}
\lipsum[1-4]
\end{parnumbers}

\begin{parnumbers}
\lipsum[5]
\begin{parnumbers*}
\lipsum[6-7]
\end{parnumbers*}
\lipsum[8]
\end{parnumbers}

\begin{parnumbers}
\lipsum[9-10]
\end{parnumbers}

\end{document}

相关内容